【问题标题】:Powershell read file for text wait until found then proceedPowershell读取文本文件等到找到然后继续
【发布时间】: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


    【解决方案1】:

    您不需要循环,只需使用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 来停止管道

    【讨论】:

    • 但是你必须手动停止,对吧?
    • @marsze Select -First 1 将在找到字符串后立即stop the pipeline :)
    • 荣誉。很好的解决方案。以前从来没有遇到过-Wait,这里还在学习新东西..
    • 我什至没有想到-等等,谢谢!我喜欢这个解决方案,因为一旦脚本可以自动化而不是手动调用,肯定会把它藏起来以备将来使用。谢谢马蒂亚斯!
    【解决方案2】:

    你说的是“循环”,那你为什么不使用循环呢?

    
    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
    }
    

    【讨论】:

    • "loop",我正在为我的“if/else”被调用的措辞画一个空白。也就是说,我确实喜欢你的产品,似乎我很接近但被困在我自己的循环中(在我的脑海中)。我喜欢这个脚本的手动运行过程,特别是因为我可以向用户显示它正在等待什么。非常感谢 Marsze!
    猜你喜欢
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2015-01-30
    • 2012-01-29
    • 1970-01-01
    • 2014-03-16
    • 2014-08-07
    相关资源
    最近更新 更多