function Invoke-ExternalTool { param( [Parameter(Mandatory=$true)][string]$FileName = "wsl.exe", [string]$Arguments = "-l -v" ) $psi = [System.Diagnostics.ProcessStartInfo]::new() $psi.FileName = $FileName $psi.Arguments = $Arguments $psi.UseShellExecute = $false $psi.RedirectStandardOutput = $true $psi.RedirectStandardError = $true # Read Output as Unicode(UTF-16LE) $psi.StandardOutputEncoding = [System.Text.Encoding]::Unicode $psi.StandardErrorEncoding = [System.Text.Encoding]::Unicode $p = [System.Diagnostics.Process]::new() $p.StartInfo = $psi [void]$p.Start() $out = $p.StandardOutput.ReadToEnd() $err = $p.StandardError.ReadToEnd() $p.WaitForExit() if ($p.ExitCode -ne 0 -and $err) { throw $err } # Within PowerShell, UTF-16 is converted to its internal Unicode representation, and operations can then be performed as UTF-8 if needed. return $out -split "`r?`n" } # Usage: # Invoke-ExternalTool "外部ツールの実行ファイル" "引数" $lines = Invoke-ExternalTool "wsl.exe" "-l -v" $lines $lines[1].Contains("Ubuntu")