我无法将数组传递给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个(工作中)的块,然后尝试将其作为作业运行。我收到的错误似乎是试图为单个服务器运行Get-VM,将所有50个值附加在一起。

最佳答案

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

我与虚拟机进行远程交互的典型方式是,并为每个创建一个PS Job。
$ 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台服务器之间使用它。

关于arrays - 将数组传递给Get-Job,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32142934/

10-11 22:57
查看更多