【发布时间】:2021-10-22 05:48:01
【问题描述】:
我正在尝试通过搜索 Get-ComplianceSearch 的“已完成”输出来返回 TRUE。我下面的代码是一个简单的等待循环。但我认为我没有正确返回值,因为循环永远不会结束。我对 PowerShell 相当陌生。请协助或指导。
我使用的是 Powershell Core 7.1。没有错误,但 Search-String 条件永远不会返回 TRUE。
try {
$timer = [Diagnostics.Stopwatch]::StartNew()
while (($timer.Elapsed.TotalSeconds -lt $Timeout) -and (-not (Get-ComplianceSearch -
Identity $searchName | Select-String 'Completed' -SimpleMatch -Quiet))) {
Start-Sleep -Seconds $RetryInterval
$totalSecs = [math]::Round($timer.Elapsed.TotalSeconds, 0)
Write-Verbose -Message "Still waiting for action to complete after [$totalSecs]
seconds..."
}
$timer.Stop()
if ($timer.Elapsed.TotalSeconds -gt $Timeout) {
throw 'Action did not complete before timeout period.'
} else {
Write-Verbose -Message 'Action completed before timeout period.'
}
} catch {
Write-Error -Message $_.Exception.Message
}
(这是 Get-ComplianceSearch 命令的预期输出)
【问题讨论】:
标签: powershell where-clause select-string