【发布时间】:2021-10-15 13:28:45
【问题描述】:
我正在处理跨远程服务器启动某些服务的过程,但是服务器 2 无法启动,直到在 server1 日志中找到一条消息,服务器 3 直到服务器 2 中出现相同消息后才能启动,等等.
我的问题是,是否可以在“循环”中读取文件并且在找到该消息之前不继续我的初始循环(然后继续前进)?我想我可以在下面做,但是虽然它确实识别到日志文件中的字符串已找到,但它只是重复它找到它,直到内置计时器完成然后向前移动。 因此,该过程看起来像“读取此文件,如果找到字符串,则继续。如果未找到字符串,请等待 30 秒并重新扫描文件。如果找到,请继续,如果未找到,请再等待 30 秒,然后重新扫描(我可以连续重复)enter image description here
任何建议都将不胜感激,因为我认为我可能从错误的角度来处理这个问题......
-- 我省略了该脚本之前的大部分脚本,只包含了 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
}
}
}
【问题讨论】:
标签: powershell