We have answer of your question!

100% solved queries, no empty question

Question: Powershell : Use a Select-Object expression with a string


0

Advertisement


For some scripts, i need to have an output composed of calculated properties.

For example, for a list of ip addresses in ip.txt, i want to know if they respond to ping. So i try the following command:

Get-Content .\ip.txt | Select-Object $_,@{Name="ping?";Expression={Test-Connection $_ -Quiet -Count 1}}

But i get an error, regardless of what i do in the scriptblock expression.

The error (in french, sorry):

Select-Object : Paramètre Null. Le type attendu doit être l'un des suivants : {System.String, System.Management.Automation.ScriptBlock}. Au niveau de ligne : 1 Caractère : 37 + Get-Content .\ip.txt | Select-Object <<<< $_,@{Name="ping?";Expression={Test-Connection $_ -Quiet -Count 1}} + CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand

I used the "calculated properties" in some scripts before, but with directories objects. Why it doesnt work with strings?

Question author Plunkets | Source

Answer


1


Advertisement


try this instead, you need to create calculated properties for each value:

Get-Content .\ip.txt | Select-Object  @{n="Server IP";e={$_}},@{n="ping?";e={[bool](Test-Connection $_ -Quiet -Count 1)}}

The problem in your code is the $_ not the calculated property. Select-object accept and array of properties, if in the array you pass $_ isn't evaluated as a property. If you do just select-object $_ (as select-object -prop $null ) piped items are the output.

Answer author Cb

Advertisement


Tickanswer.com is providing the only single recommended solution of the question Powershell : Use a Select-Object expression with a string under the categories i.e powershell , . Our team of experts filter the best solution for you.

Related Search Queries:

You may also add your answer!