我正在處理跨遠程服務器啟動某些服務的程序,但是服務器 2 無法啟動,直到在服務器 1 日志中找到訊息,服務器 3 無法啟動,直到服務器 2 中的相同訊息等。
我的問題是,是否可以在“回圈”中讀取檔案并且在找到該訊息之前不繼續進行初始回圈(然后繼續前進)?我想我可以在下面做,但是雖然它確實識別出日志檔案中的字串已找到,但它只是重復它找到它,直到內置的計時器完成然后向前移動。因此,該程序看起來像“讀取此檔案,如果找到字串,則向前移動。如果未找到字串,則等待 30 秒并重新掃描檔案。如果找到,則向前移動,如果未找到,則再等待 30 秒并重新掃描(我可以連續重復)< 這樣做直到找到字串。示??例:在此處輸入影像描述
任何建議將不勝感激,因為我認為我可能從錯誤的角度接近這個......
-- 在此腳本之前,我省略了大部分腳本,僅包含 If/Else 陳述句,因為這是檢查檔案的位置。
$SEL = Select-String -Path \\$Server\$RootDir\folder\anotherfolder\A-Log-File.log -Pattern "Switching to status: STARTED"
if ($SEL -ne $null)
{
Write-Host "FOUND: Switching to status: STARTED" -ForegroundColor Yellow -BackgroundColor DarkGreen
}
else
{
Write-Host **** Waiting 60 seconds for cache to build ****
[int]$Time = 60
$Lenght = $Time / 100
For ($Time; $Time -gt 0; $Time--) {
$min = [int](([string]($Time/60)).split('.')[0])
$text = " " $min " minutes " ($Time % 60) " seconds left"
Write-Progress -Activity "Waiting for Started Message" -Status $Text -PercentComplete ($Time / $Lenght)
Start-Sleep 1
$SEL = Select-String -Path \\$Server\$RootDir\folder\anotherfolder\A-Log-File.log -Pattern "Switching to status: STARTED"
if ($SEL -ne $null)
{
Write-Host "FOUND: Switching to status: STARTED" -ForegroundColor Yellow -BackgroundColor DarkGreen
}
else
{
Write-Host **** A-Log-File.log Log does NOT contain a started message **** -ForegroundColor Red -BackgroundColor Yellow
Write-Host **** Investigate further or increase the int-time time on Line 54 to 180 seconds **** -ForegroundColor Red -BackgroundColor Yellow ##This part goes away once action can be taken based on reading contents of the file
}
}
}
uj5u.com熱心網友回復:
您不需要回圈,只需使用Get-Content -Wait:
$null = Get-Content "\\$Server\$RootDir\folder\anotherfolder\A-Log-File.log" -Wait |Where-Object { $_ -match 'Switching to status: STARTED' } |Select -First 1
Get-Content -Wait將繼續輸出寫入檔案的新行,直到它被中斷 - 幸運的是Select -First 1,一旦我們觀察到字串,我們就可以使用它來停止管道
uj5u.com熱心網友回復:
你說“回圈”,那你為什么不使用回圈?
while ($true) {
# (re)try
$SEL = Select-String -Path "\\$Server\$RootDir\folder\anotherfolder\A-Log-File.log" -Pattern "Switching to status: STARTED"
if ($SEL -ne $null)
{
Write-Host "FOUND: Switching to status: STARTED" -ForegroundColor Yellow -BackgroundColor DarkGreen
# exit the loop
break;
}
# wait
Write-Host "**** Waiting 60 seconds for cache to build ****"
Start-Sleep 1
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/323349.html
標籤:电源外壳
上一篇:在撰寫PowerShellcmdlet時,我應該更喜歡“Export-ModuleMember”還是“global:”(或其他)?
