【发布时间】:2019-05-22 11:16:50
【问题描述】:
我无法让后台线程在 Powershell 中工作。我想使用运行空间,我可以在其中插入一个脚本并在有脚本时获取更新,并允许我的主线程处理它需要的数据。我不确定为什么这段代码不返回正确的值?我测试的所有服务器在没有在运行空间中使用时都返回 true,但这里的返回值为 0。
我对使用 powershell 作业不感兴趣。
$servers = @("x1","x2","x3");
$results = New-Object Collections.Generic.List[String];
$input = New-Object 'System.Management.Automation.PSDataCollection[psobject]'
$output = New-Object 'System.Management.Automation.PSDataCollection[psobject]'
$results.Clear();
foreach ($server in $servers)
{
Write-Host $server;
$powerShell = [Management.Automation.PowerShell]::Create();
[Void]$PowerShell.AddScript({
$result = Test-Connection $server -Count 1 -Quiet;
return ($server - $result)
})
$handle = $powerShell.BeginInvoke($input,$output);
$results.Add($output);
}
$results;
或
$results = New-Object Collections.Generic.List[String];
$input = New-Object 'System.Management.Automation.PSDataCollection[psobject]'
$output = New-Object 'System.Management.Automation.PSDataCollection[psobject]'
$results.Clear();
foreach ($server in $servers)
{
Write-Host $server;
$powerShell = [Management.Automation.PowerShell]::Create();
[Void]$PowerShell.AddScript({
param ($server)
$result = Test-Connection $server -Count 1 -Quiet;
return ($server - $result)
})
$handle = $powerShell.BeginInvoke($server,$output);
#$handle = $powerShell.BeginInvoke($input,$output);
$results.Add($output);
}
$results;
【问题讨论】:
-
你看过jobs。这些旨在在后台运行任务。
-
是的,我对使用作业不感兴趣
标签: multithreading powershell runspace