Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 奇怪的结果:很多空格而不是一个恰当的句子_Powershell_Powershell 3.0_Powershell 4.0 - Fatal编程技术网

Powershell 奇怪的结果:很多空格而不是一个恰当的句子

Powershell 奇怪的结果:很多空格而不是一个恰当的句子,powershell,powershell-3.0,powershell-4.0,Powershell,Powershell 3.0,Powershell 4.0,我用这个脚本得到奇怪的输出,用nagios监视卷影副本 如果我运行此脚本: $runs = Get-ScheduledTaskInfo -TaskName "ShadowCopyVolume{eff29848-ac44-11e8-80b1-806e6f6e6963}" | Format-Wide -Property NumberOfMissedRuns if ($runs -cmatch "0") { Write-host ok exit 0 } else { Write-Output "mi

我用这个脚本得到奇怪的输出,用nagios监视卷影副本

如果我运行此脚本:

$runs = Get-ScheduledTaskInfo -TaskName "ShadowCopyVolume{eff29848-ac44-11e8-80b1-806e6f6e6963}" | Format-Wide -Property NumberOfMissedRuns

if ($runs -cmatch "0")
{
Write-host ok
exit 0
}
else
{
Write-Output "missed" $runs "runs"
exit 1
}
其结果是:

PS C:\Program Files\NSClient++\scripts> C:\Program Files\NSClient++\scripts\check_scopy.ps1
missed


0                                                                                                                                                                                                                                                                


runs
浪费了很多空间


因此,任何让它成为一个恰当的句子而不是一个ps结果的建议???

不要使用
格式-*
来选择单个属性。或使用:

$runs = Get-ScheduledTaskInfo -TaskName "ShadowCopyVolume{eff29848-ac44-11e8-80b1-806e6f6e6963}" | 
    Select-Object -Expandproperty NumberOfMissedRunS
或更短:

$runs = (Get-ScheduledTaskInfo -TaskName "ShadowCopyVolume{eff29848-ac44-11e8-80b1-806e6f6e6963}").NumberOfMissedRuns
属性
NumberOfMissedRuns
的类型为uint32,因此简单:

if ($runs){"missed {0} runs" -f $runs} else {write-Host "ok"}
应该返回所需的结果


顺便说一句,将
-cmatch
与数字结合使用没有任何意义-数字没有大小写。

试着将这三个词放在一起。类似于这样:
Write Output$('missed{0}runs'-f$runs)
Write Output“missed$($runs.length)runs”