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

Ask your 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.
This topic is 5 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
oliver.adams@itpartner.no
Posts: 2
Last visit: Fri Mar 15, 2019 12:49 pm

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

Post by oliver.adams@itpartner.no »

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
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

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

Post by davidc »

[TOPIC MOVED TO THE WINDOWS POWERSHELL FORUM BY THE MODERATOR]
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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.
4lch4_ExtraTxt
Posts: 21
Last visit: Sat Feb 24, 2024 5:20 am
Has voted: 2 times

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

Post by 4lch4_ExtraTxt »

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. 150,3CP,4CP,4EP
  3. 149,3BP,4CP,4DP
  4. 148,3AP,4BP,4CP
  5. 147,3AP,4BP,4CP
  6. 146,3AP,4BP,4CP
  7. 145,3AK,4AP,4CP
  8. 144,3,4AP,4BP
Using this data, you can do something like so and you'll get the following result:
Code: [Select all] [Expand/Collapse] [Download] (Get-CSVResult.ps1)
  1. $At = "19"
  2. $Hit = "144"
  3.  
  4. $Result = Get-Content -Path .\tmp.csv | ConvertFrom-Csv | Select-Object Hitroll, $At | Where-Object { $_.Hitroll -Like $Hit }
  5. Write-Output $Result

Code: Select all

Hitroll 19
------- --
144     4AP
J.B. Hunt
Devin Leaman
Devin.Leaman@jbhunt.com
This topic is 5 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked