Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 在运行下一个任务之前,请等待所有线程完成_Multithreading_Powershell_Jobs_Start Job - Fatal编程技术网

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
应该具有完全相同的效果。