0

このスクリプトが Powershell v5.1 と v7.0 で異なる結果を返すのはなぜですか?

<#
    The intention of my real script is to monitor a log file for certain lines and then send me a notification.
    This test script has been stripped down to just reading the last line of itself, and then trying to process
    the logged message. It should extract the username and server names.
    Under 5.1 it returns "turpie" and "lobby"
    However on v7 it returns "turpie" and "7mServerConnector [lobby".
#>
$sb = [scriptblock]::create("get-content -wait "+ $MyInvocation.MyCommand.Path + " -tail 1")
start-job -ScriptBlock $sb | Out-Null

while (1) { 
  $m = $(Get-Job | Receive-Job | Select-String -SimpleMatch "ServerConnector [lobby] has connected" | Get-Unique) 
  if ($null -ne $m) { 
    Write-Host $m
    $user, $server = ($m | Out-String | Select-String '(?<=\[)[^]]+(?=\])' -AllMatches).Matches.Groups[1..2].Value
    Write-Host $user "has connected to" $server
  }
  Start-Sleep 1 
}

# "09:52:04 [INFO] [turpie] <-> ServerConnector [lobby] has connected"

文字列を抽出コードにパイプするだけにそれを取り除くと、機能します。

("09:52:04 [INFO] [turpie] <-> ServerConnector [lobby] has connected" | Select-String '(?<=\[)[^]]+(?=\])' -AllMatches).Matches.Groups[1..2].Value
4

1 に答える 1