The following code works fine:
$ScriptBlock = {
Resolve-DnsName -name BELCL003000
}
$Pool = [RunspaceFactory]::CreateRunspacePool(1, 6)
$Pool.ApartmentState = 'MTA'
$Pool.Open()
$Runspaces = @()
$Runspace = [PowerShell]::Create()
$null = $Runspace.AddScript($ScriptBlock)
# $null = $Runspace.AddArgument($TestWaveGroupName)
$Runspace.RunspacePool = $Pool
$Runspaces += @{
Pipe = $Runspace
Status = $Runspace.BeginInvoke()
}
while ($Runspaces.Status -ne $null) {
$CompletedRunspaces = $Runspaces.Where( { $_.Status.IsCompleted -eq $true })
foreach ($C in $CompletedRunspaces) {
$C.Pipe.EndInvoke($C.Status)
$C.Status = $null
}
}
$Pool.Close()
$Pool.Dispose()
However, when I wrap it all in a job
there suddenly is no output anymore for the same hostname:
Start-Job -ScriptBlock {
$Pool = [RunspaceFactory]::CreateRunspacePool(1, 6)
$Pool.ApartmentState = 'MTA'
$Pool.Open()
$Runspaces = @()
$Runspace = [PowerShell]::Create()
$null = $Runspace.AddScript($ScriptBlock)
# $null = $Runspace.AddArgument($TestWaveGroupName)
$Runspace.RunspacePool = $Pool
$Runspaces += @{
Pipe = $Runspace
Status = $Runspace.BeginInvoke()
}
while ($Runspaces.Status -ne $null) {
$CompletedRunspaces = $Runspaces.Where( { $_.Status.IsCompleted -eq $true })
foreach ($C in $CompletedRunspaces) {
$C.Pipe.EndInvoke($C.Status)
$C.Status = $null
}
}
$Pool.Close()
$Pool.Dispose()
}
Get-Job | Receive-Job -Wait -AutoRemoveJob
Am I missing something obvious here? I've tried to add Import-Module DnsClient
within the ScriptBlock
but without success.
Other ScriptBlocks
return output without a problem using this method, just not the Resolve-DnsName
CmdLet. The job approach is used in a form to allow the form to stay responsive.