我正在尝试拖动的脚本有点不知所措。Powershell运行空间将不会执行
简而言之:我想扫描我的域计算机的WinRM连接 - 我可以做到这一点很好。问题是,它需要5分钟才能完成 - 这就是为什么我想要多线程的任务。
工作的非多线程代码:
# this is normaly a textfile with lots of machine hostnames
$computers = "PC100","PC106","PC124","PC115","PC21"
function checkMachine($computers){
$ErrorActionPreference = "Stop"
foreach ($item in $computers){
#the function contest only performs a ping and returne $true or $false
$connection = ConTest($item)
if($connection){
try{
$winRM = test-wsman -ComputerName $item
if($winRM){
write-host "winRM"
[void] $objListboxLeft.Items.Add($item)
}
}catch{
write-host "NO winRM"
[void] $objListboxCenter.Items.Add($item)
}
}else{
write-host "offline"
[void] $objListboxRight.Items.Add($item)
}
}
}
这基本上是什么我不skript /会做只是一小部分,但它是需要年龄的一部分。
我失败的运行空间测试 - 我基本上没有得到任何结果。没有文本框,我的命令行没有输出,我基本上不知道我做错了什么。
多线程代码:
function MulticheckMachine($computers){
$ErrorActionPreference = "Stop"
$runspaceCollection = @()
$runspacePool = [RunspaceFactory]::CreateRunspacePool(1,5)
$runspacePool.open()
$scriptBlock = {
Param($item)
$connection = ConTest($item)
if($connection){
try{
test-wsman -ComputerName $item
$winRM = test-wsman -ComputerName $item
if($winRM){
write-host "winRM"
[void] $objListboxLeft.Items.Add($item)
}
}catch{
write-host "NO winRM"
[void] $objListboxCenter.Items.Add($item)
}
}else{
write-host "offline"
[void] $objListboxRight.Items.Add($item)
}
}
Foreach($item in $computers){
$powershell = [PowerShell]::Create().AddScript($scriptBlock).AddArgument($item)
$powershell.runspacePool = $runspacePool
[Collections.Arraylist]$runspaceCollection += New-Object -TypeName PSObject -Property @{
Runspace = $powershell.BeginInvoke()
PowerShell = $powershell
}
$runspaceCollection
}
While($runspaceCollection){
Foreach($runspace in $runspaceCollection.ToArray()){
If($runspace.Runspace.IsCompleted){
$runspace.PowerShell.EndInvoke($runspace.Runspace)
$runspace.PowerShell.Dispose()
$runspaceCollection.Remove($runspace)
}
}
}
}
运行空间代码来自这些指南的混合:
http://newsqlblog.com/2012/05/22/concurrency-in-powershell-multi-threading-with-runspaces/
我希望有人能帮助我,并告诉我在哪里/为什么我失败了。谢谢!
'$ objListboxLeft','$ objListboxCenter'和'$ objListboxRight'不运行空间存在,这可能就是它的失败。从ScriptBlock返回一个对象 –
我想你可以通过使用[powershell jobs](https://technet.microsoft.com/en-us/library/hh847805.aspx)来简化你的脚本。类似'invoke-command -computername $ computers -scriptblock {#check winrm localy} -asjob' –
@Kayasax仍然无法解决他尝试修改执行范围/ runspace/process中不存在的对象的问题 –