将数组传递给 Get-Job

Passing an array to Get-Job

我无法将数组传递给 Start-Job 中的脚本块。你能告诉我我可能做错了什么吗?

$bounceBlock = {
param(
[string[]]$list,
[System.Management.Automation.PSCredential]$cred
)
Add-PSSnapin VMware.VimAutomation.Core | Out-Null
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -InvalidCertificateAction Ignore -Confirm:$false | Out-Null
Connect-VIServer -Server servername -Credential $cred -AllLinked
Get-VM -Name $list
}


if ($targets) {
$activeTargets = $targets | Get-Random -Count $prodTargets.Count
$counter = [pscustomobject] @{Value = 0}
$groupSize = 50
$groups = $activeTargets | Group-Object -Property {[math]::Floor($counter.Value++ / $groupSize)}
$connection = Connect-VIServer -Server servername -Credential $cred -AllLinked
if ($connection -match "servername") {
    foreach ($group in $groups) {
        while ((Get-Job -State Running).Count -ge 5) {
            Start-Sleep -Seconds 5
            }
        Start-Job -ScriptBlock $bounceBlock -ArgumentList (,$group.Group.ServerName),$cred
        }
    Disconnect-VIServer * -Force -Confirm:$false
    }
}

我基本上将一个数组分成 50 个(工作)块,然后尝试 运行 它们作为作业。我收到的错误看起来像是在尝试 运行 为单个服务器获取 VM,将所有 50 个值附加在一起命名。

我当然不是 PS 方面的专家,但首先要解决您如何传递附加的服务器列表;我使用 Get-AzureVM 对 Azure VM 执行类似的操作,并将 System.Array 中的 VM 名称列表传递给函数或 cmdlet,例如变量 $theVMs= "MyServer1","MyServer2","MyServer3" 然后我执行一个 foreach 循环($vm in $theVMs),然后依次执行 Get-VM 等操作。我按顺序执行此操作,因为 PS 有一些低得多的限制,根据我的经验,通过并行 for 循环执行此操作为 5。

我与 VM 远程交互 并为每个 创建一个 PS 作业的典型方法是使用 $uri = Get-AzureWinRMUri -ServiceName $svc -Name $vmname

 Invoke-Command -ConnectionUri $uri -Credential $creds **-JobName 
 $jobname**    
 -ArgumentList $vmname -ScriptBlock {
 param([string]$thevm) ...
 }

这需要 InstallWinRMCertAzureVM。ps1 脚本已在 http://blogs.technet.com 上讨论并提供。我经常将它用于 30 台服务器。

只是想让每个人都知道,以防他们 运行 遇到类似的问题,这是 运行ning Get-VM 的问题,并且 运行ning 它是否存在工作或工作流程。 VMWare 已意识到此问题。