Multithreading 在运行下一个任务之前,请等待所有线程完成
我会在Multithreading 在运行下一个任务之前,请等待所有线程完成,multithreading,powershell,jobs,start-job,Multithreading,Powershell,Jobs,Start Job,我会在启动作业中将所有内容包装在foreach($computers in$computers)中,使它们同时运行。唯一的问题是,我需要等待所有作业完成,然后才能执行底部的转换为Json $sb = "OU=some,OU=ou,DC=some,DC=domain" $computers = Get-ADComputer -Filter {(Enabled -eq $true)} -SearchBase "$sb" -Properties * $hasmanufacturer = New-Obj
启动作业
中将所有内容包装在foreach($computers in$computers)
中,使它们同时运行。唯一的问题是,我需要等待所有作业完成,然后才能执行底部的转换为Json
$sb = "OU=some,OU=ou,DC=some,DC=domain"
$computers = Get-ADComputer -Filter {(Enabled -eq $true)} -SearchBase "$sb" -Properties *
$hasmanufacturer = New-Object System.Collections.Generic.List[System.Object]
foreach($computer in $computers)
{
$drives = try{@(Get-WMIObject -Class Win32_CDROMDrive -Property * -ComputerName $computer.Name -ErrorAction Stop)} catch {$null}
foreach($drive in $drives)
{
if($drive.Manufacturer)
{
$hasmanufacturer.Add($computer)
continue
}
} # inner foreach
}
ConvertTo-Json $hasmanufacturer
在执行
ConvertTo-Json
之前使用a执行ConvertTo-Json
如何使用计算机名称数组作为调用命令的参数。默认情况下,它将运行32个并发远程会话。可使用-Throttle
参数更改编号
$computers = Get-ADComputer -Filter {(Enabled -eq $true)} -SearchBase "OU=Servers,DC=xxx,DC=com" -Properties Name |
Where-Object { $_.Name -match 'LAX_*' } |
ForEach-Object { $_.Name }
$computers
$j = Invoke-Command `
-ComputerName $computers `
-ScriptBlock { Get-WMIObject -Class Win32_CDROMDrive -Property * -ErrorAction Stop } `
-AsJob
while ( (Get-Job -Id $j.Id).Status -eq 'Running') {}
Get-Job -Id $j.Id | Wait-Job
$results = Receive-Job -Id $j.Id
$results
使用计算机名称数组作为参数调用命令怎么样。默认情况下,它将运行32个并发远程会话。可使用
-Throttle
参数更改编号
$computers = Get-ADComputer -Filter {(Enabled -eq $true)} -SearchBase "OU=Servers,DC=xxx,DC=com" -Properties Name |
Where-Object { $_.Name -match 'LAX_*' } |
ForEach-Object { $_.Name }
$computers
$j = Invoke-Command `
-ComputerName $computers `
-ScriptBlock { Get-WMIObject -Class Win32_CDROMDrive -Property * -ErrorAction Stop } `
-AsJob
while ( (Get-Job -Id $j.Id).Status -eq 'Running') {}
Get-Job -Id $j.Id | Wait-Job
$results = Receive-Job -Id $j.Id
$results
foreach将创建X个作业(取决于
$computers
的长度。我认为等待作业
只等待其中一个作业?@KolobCanyon可能是自己创建的,但不会与获取作业
组合,因为这会将正在运行的作业传递给它:-)。查看链接文档中的示例1。foreach将创建X个作业(取决于$computers
的长度。我认为等待作业
只等待其中一个作业?@KolobCanyon可能是自己的,但不会与获取作业
组合,因为这会将正在运行的作业传递给它:-)。查看链接文档中的示例1。不相关,但。。。发生错误时返回$null
时,为什么要使用-ErrorAction Stop
?删除try..catch
和@()
并使用-ErrorAction SilentlyContinue
应具有完全相同的效果。不相关,但。。。发生错误时返回$null
时,为什么要使用-ErrorAction Stop
?删除try..catch
和@()
并使用-ErrorAction SilentlyContinue
应该具有完全相同的效果。