PowerShell スクリプト内でパイプラインからの出力を処理しようとしています。現在、出力を確認する唯一のEndInvoke
方法は、パイプラインのメソッドを発行したときですが、一部の呼び出しは長時間実行される可能性があり、多くの出力を持つ可能性があるため、表示できるようにしたいと考えていますプロセスの実行時に出力されます。
入力パラメーターと出力パラメーターをBeginInvoke
メソッドに渡すことでこれを行うことができるように見えますが、正しい構文を取得できないようです。助言がありますか?私が試していることの例を以下に示します。
$scriptBlock = {param([int]$pauseTime = 10); Write-Output "Test"; Start-Sleep -Seconds $pauseTime; Write-Output "Test 2"}
# Create objects and set stuff up
$initialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$runspacepool = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(1, 4, $initialSessionState, $Host)
$runspacepool.Open()
$pipeline = [powershell]::Create().AddScript($scriptBlock).AddParameter("pauseTime", 30)
$pipeline.RunspacePool = $runspacepool
# These two lines are not correct
$inputStream = New-Object [System.Management.Automation]
$outputStream = New-Object [System.Management.Automation.PSDataCollection]
$async = $pipeline.BeginInvoke($inputStream, $outputStream)
# Do something with the $outputStream here???
$pipeline.EndInvoke($async)
# Clean-up code
$pipeline.Dispose()
$async = $null
$pipeline = $null
if ($runspacepool -ne $null) {$runspacepool.Close()}