Select-Object : Cannot convert System.Management.Automation.PSObject

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
oliver.adams@itpartner.no
Posts: 2
Joined: Sat Jun 08, 2013 8:06 am

Select-Object : Cannot convert System.Management.Automation.PSObject

Post by oliver.adams@itpartner.no » Fri Mar 15, 2019 12:49 pm

I am trying to import a csv file and filter the data.

The csv file has the format shown below.

$at matches 20 ,19 or 18 for this example and hit is set to a number between 144 and 150.

$HitData = Import-Csv -Path E:\Dropbox\Rolemaster\dagger.csv | Select-Object Hitroll,$AT | Where-Object {$_.Hitroll -Like $Hit}

Hitroll 20 19 18
150 3CP 4CP 4EP
149 3BP 4CP 4DP
148 3AP 4BP 4CP
147 3AP 4BP 4CP
146 3AP 4BP 4CP
145 3AK 4AP 4CP
144 3 4AP 4BP

This is the full error I am getting.
ERROR: Select-Object : Cannot convert System.Management.Automation.PSObject to one of the following types {System.String, System.Management.Automation.ScriptBlock}.
ERROR:
RolemasterTest.pff (292): ERROR: At Line: 292 char: 90
ERROR: + $HitData = Import-Csv -Path E:\Dropbox\Rolemaster\$($cboWeapon.Text).csv | Select-Object <<<< Hitroll,$AT | Where-Object {$_.Hitroll -Like $Hit}
ERROR: + CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException
ERROR: + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand


Powershell Studio 2012 version 3.1.35 on windows 10 64bit

User avatar
davidc
Posts: 5913
Joined: Thu Aug 18, 2011 4:56 am

Re: Select-Object : Cannot convert System.Management.Automation.PSObject

Post by davidc » Fri Mar 15, 2019 1:03 pm

[TOPIC MOVED TO THE WINDOWS POWERSHELL FORUM BY THE MODERATOR]
David
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 13623
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Select-Object : Cannot convert System.Management.Automation.PSObject

Post by jvierra » Fri Mar 15, 2019 1:32 pm

What you have posted is not a CSV file. Open the CSV in notepad and post the contents or, at least, the first few rows.

User avatar
D.Leaman
Posts: 18
Joined: Thu Jan 10, 2019 4:21 pm

Re: Select-Object : Cannot convert System.Management.Automation.PSObject

Post by D.Leaman » Sun Mar 17, 2019 3:23 am

Since the data you posted was actually tab separated, I added in commas and made it a real csv data set:

Code: Select all

Hitroll,20,19,18
150,3CP,4CP,4EP
149,3BP,4CP,4DP
148,3AP,4BP,4CP
147,3AP,4BP,4CP
146,3AP,4BP,4CP
145,3AK,4AP,4CP
144,3,4AP,4BP
Using this data, you can do something like so and you'll get the following result:

Code: Select all

$At = "19"
$Hit = "144"

$Result = Get-Content -Path .\tmp.csv | ConvertFrom-Csv | Select-Object Hitroll, $At | Where-Object { $_.Hitroll -Like $Hit }
Write-Output $Result

Code: Select all

Hitroll 19
------- --
144     4AP
J.B. Hunt
Devin Leaman
Devin.Leaman@jbhunt.com

Locked