Reputation: 493
I'm trying to understand Runspaces in PowerShell. I know about the PoshRSJob-Module
, but I'd like to create my Runspace Jobs by myself.
This is my code, mostly taken out from this 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
}
Three question are in my mind:
$Data
Variable empty?$null
the invocation like this $Invoke = $PowerShell.BeginInvoke() > $null
, the script doesn't work properly anymore and still creates this output 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 :
Upvotes: 1
Views: 2642
Reputation: 47792
I don't understand your first question.
For the second question, I think it's because you're using $PowerShell.AddParameter($Computer)
.
Try $PowerShell.AddArgument($Computer)
instead. AddArgument
is for adding a value that gets implicitly (positionally) bound to a parameter. AddParameter
is for adding a named parameter. The overload of AddParameter
that takes just a string
is for [Switch]
parameters.
For your third question, I think it's $RunspacePool.Open()
that's giving you that output.
When trying to determine these things, look for lines, especially with method calls, that have no left-hand assignment; so things you aren't assigning to a variable, as that's generally how these values get put into the output stream.
Upvotes: 1