对于某些脚本,我需要一个由计算属性组成的输出。
例如,对于ip.txt中的ip地址列表,我想知道它们是否响应ping。因此,我尝试使用以下命令:
Get-Content .\ip.txt | Select-Object $_,@{Name="ping?";Expression={Test-Connection $_ -Quiet -Count 1}}但是不管我在scriptblock表达式中做什么,我都会得到一个错误。
错误(法语,抱歉):
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
我以前在一些脚本中使用了“计算属性”,但使用的是目录对象。为什么它不能与字符串一起工作?
发布于 2013-06-04 22:50:38
请尝试执行此操作,您需要为每个值创建计算属性:
Get-Content .\ip.txt | Select-Object @{n="Server IP";e={$_}},@{n="ping?";e={[bool](Test-Connection $_ -Quiet -Count 1)}}代码中的问题是$_,而不是计算的属性。Select-object accept和属性数组,如果在传递的数组中,$_不作为属性计算。如果只选择(as select-object $_ -object -prop $null ),管道项目就是输出。
https://stackoverflow.com/questions/16920689
复制相似问题