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.

jvierra
Posts: 14136
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.

D.Leaman
Posts: 19
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:
  1. Hitroll,20,19,18
  2.  
  3. 150,3CP,4CP,4EP
  4.  
  5. 149,3BP,4CP,4DP
  6.  
  7. 148,3AP,4BP,4CP
  8.  
  9. 147,3AP,4BP,4CP
  10.  
  11. 146,3AP,4BP,4CP
  12.  
  13. 145,3AK,4AP,4CP
  14.  
  15. 144,3,4AP,4BP
Using this data, you can do something like so and you'll get the following result:
  1. $At = "19"
  2.  
  3. $Hit = "144"
  4.  
  5.  
  6.  
  7. $Result = Get-Content -Path .\tmp.csv | ConvertFrom-Csv | Select-Object Hitroll, $At | Where-Object { $_.Hitroll -Like $Hit }
  8.  
  9. Write-Output $Result

Code: Select all

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

Locked