私はPowerShellでRunspacesを理解しようとしています。私はPoshRSJob-Module
について知っていますが、自分でランスペースジョブを作成したいと思います。PowerShell実行時空間、出力、および変数
これは主にこのblogから取り出し、私のコードです:
$Computer = "somename"
[runspacefactory]::CreateRunspacePool() > $null
$SessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$RunspacePool = [runspacefactory]::CreateRunspacePool(1,5)
$RunspacePool.Open()
1..2 | % {
$PowerShell = [powershell]::Create()
$PowerShell.RunspacePool = $RunspacePool
$PowerShell.AddScript({
param(
$Computer
)
$Computer
}) > $null
$PowerShell.AddParameter($Computer)
$Invoke = $PowerShell.BeginInvoke()
while (!($Invoke.IsCompleted)) {sleep -Milliseconds 2}
$Data = $PowerShell.EndInvoke($Invoke)
Write-Host $Data -f Red
}
三つの疑問が私の心にあります。
- 私は変数の値を返すことができ、それを使用します。ジョブを終了した後にスクリプト内でさらに使用するには?
- なぜ私の
$Data
変数は空ですか? - これまでのスクリプトでは、次の出力を作成していますか?私はこの
$Invoke = $PowerShell.BeginInvoke() > $null
ような呼び出しを$null
場合、スクリプトはもはや正常に動作しても、この出力
Commands : System.Management.Automation.PSCommand Streams : System.Management.Automation.PSDataStreams InstanceId : 3b91cfda-028e-4cec-9b6d-55bded5d9d3c InvocationStateInfo : System.Management.Automation.PSInvocationStateInfo IsNested : False HadErrors : False Runspace : RunspacePool : System.Management.Automation.Runspaces.RunspacePool IsRunspaceOwner : False HistoryString :
$ PowerShell.AddParameter($ Computer) ' - >' [void] $ PowerShell.AddArgument($ Computer) ' – PetSerAl