Tha*_*bin 4 arrays powershell tivoli
我正在尝试计算命令输出的一些行数。基本上在这个例子中所有以“Y”结尾的行。
拳头捕获命令结果:
PS> $ItsAgents = tacmd listSystems -n Primary:SomeHost:NT
PS> $ItsAgents
Managed System Name Product Code Version Status
Primary:SomeHost:NT NT 06.30.07.00 Y
SomeHost:Q7 Q7 06.30.01.00 N
Run Code Online (Sandbox Code Playgroud)
现在统计一下在线的:
PS> $AgentCount = ($ItsAgents | Select-String ' Y ').Count
PS> $AgentCount
1
Run Code Online (Sandbox Code Playgroud)
现在一切都按我的预期进行。所以我把它放在我的脚本中,如下所示:
$ItsAgents = tacmd listSystems -n $agent
Write-Host $ItsAgents
$BeforeCount = ($ItsAgents | Select-String ' Y ').Count
Run Code Online (Sandbox Code Playgroud)
当脚本运行时(在 下Set-StrictMode)我得到:
The property 'Count' cannot be found on this object. Verify that the
property exists.
At Y:\Scripts\newMoveAgents.ps1:303 char:7
+ $BeforeCount = ($ItsAgents | Select-String ' Y ').Count
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
Run Code Online (Sandbox Code Playgroud)
确实Write-Host输出了合理的结果,因此$agent设置正确并且tacmd命令运行正常那么为什么它在脚本中失败,但在命令行上工作?
使用@()运算符强制输出始终为数组:
$BeforeCount = @($ItsAgents | Select-String ' Y ').Count
Run Code Online (Sandbox Code Playgroud)
数组子表达式运算符创建一个数组,即使它包含零个或一个对象。(微软文档)
注意:据我所知,它作为脚本和在控制台内的工作方式应该相同。也许您的命令会产生不同的输出,其中控制台版本返回 2+ 结果,但由于某种原因脚本版本仅返回 1 或 0 结果,这就是没有属性的原因Count。
| 归档时间: |
|
| 查看次数: |
5017 次 |
| 最近记录: |