2

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()}
4

1 に答える 1

1

あなたが何を達成しようとしているのかわかりませんが (スクリプトからの出力を処理するという単純な目的のためにあまりにも複雑に見えます)、PowerShell スクリプトのこの小さな変更により、少なくとも出力が得られます:

` $scriptBlock = {
    param([int]$一時停止時間 = 1)
    書き込み出力「テスト 1」
    Start-Sleep -Seconds $pauseTime
    書き込み出力「テスト 2」
   }

    # オブジェクトの作成とセットアップ
    $initialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
    $runspacepool = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspacePool(1, 4, $initialSessionState, $Host)
    $runspacepool.Open()
    $pipeline = [powershell]::Create().AddScript($scriptBlock).AddParameter("pauseTime", 5)
    $pipeline.RunspacePool = $runspacepool

    $outputStream = New-Object -Typename System.Management.Automation.PSDataCollection[PSObject]

    $async = $pipeline.BeginInvoke()
    1..10 | Foreach { "待っています..."; Start-Sleep -Milliseconds 500 }

    $outputStream = $pipeline.EndInvoke($async)

    # クリーンアップ コード    
    $pipeline.Dispose()
    $async = $null
    $パイプライン = $null
    if ($runspacepool -ne $null) {$runspacepool.Close()}

    $outputStream
`
于 2012-11-17T12:10:01.190 に答える