Vmware 使用多线程PowerCLI并行克隆多个VM

Vmware 使用多线程PowerCLI并行克隆多个VM,vmware,powercli,Vmware,Powercli,我承担了在VMware中克隆大量虚拟机的任务。我希望尽可能地自动化克隆向导,而不是通过克隆向导点击数百次 我已经对模板机进行了配置并“密封”。一旦它被克隆并通电,新克隆的实例将启动,在那里等待一段时间,然后进行sysprep,等等。这大约需要20分钟左右 我找到了一个非常好的网站,它能做我需要的一切。我对它做了一点修改,所以我不必更改值,也不必为流程的每个步骤重新保存脚本。我没有在整个脚本中使用$Step计数器,而是简单地用一些启动睡眠延迟来代替它。此时,它工作正常,并成功地从CSV文件克隆了它

我承担了在VMware中克隆大量虚拟机的任务。我希望尽可能地自动化克隆向导,而不是通过克隆向导点击数百次

我已经对模板机进行了配置并“密封”。一旦它被克隆并通电,新克隆的实例将启动,在那里等待一段时间,然后进行sysprep,等等。这大约需要20分钟左右

我找到了一个非常好的网站,它能做我需要的一切。我对它做了一点修改,所以我不必更改值,也不必为流程的每个步骤重新保存脚本。我没有在整个脚本中使用$Step计数器,而是简单地用一些启动睡眠延迟来代替它。此时,它工作正常,并成功地从CSV文件克隆了它读入的所有机器。每台机器大约需要35分钟才能准备就绪(机器移动到不同的AD OU)

唯一的问题是它以串行方式运行,等待整个过程(克隆、更改VLAN、引导计算机并等待域加入,以及将最终计算机对象移动到其他AD OU)完成,然后再启动另一个克隆

我真正想做的是对它进行多线程处理,使整个操作更快。我在测试中发现,vSphere中的克隆一旦运行大约五个单独的克隆作业,就会开始减速,因此我想修改此脚本以同时运行四个克隆(执行整个工作流)


有什么想法吗?如果需要,我可以粘贴代码。

您最多可以从一个模板并行克隆8个虚拟机。如果使用-RunAsync运行新vm。使用-RunAsync,命令立即返回,输出由一个或多个任务对象组成

如果您想克隆多个虚拟机,以下内容应该会有所帮助。循环一下

Write-host "Deploying VM " -ForegroundColor Green -NoNewline; Write-Host $vmname -ForegroundColor Yellow
get-OScustomizationspec $cs | get-OScustomizationNicMapping | set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $vm.IP -SubnetMask $vm.subnet -DefaultGateway $vm.gateway -Dns $vm.Dns1, $vm.Dns2

$vms = New-VM -Name $vm.Name -Location $vm.cluster -VMhost $vm.vmhost -Template $vm.template -Datastore $vm.datastore -OSCustomizationSpec $cs -confirm:$false **-RunAsync**

if ($vm1.error) {
    Write-Host "Error in deploying $vmname" -ForegroundColor Red
}

下面是我在过去几天编写的一个脚本,它将VM模板部署到我们大约650台服务器上。它从全国不同数据中心的3个不同VM主机部署。它同时从欧文基地部署了5架,从普拉诺基地部署了15架,从亚特兰大基地部署了15架。周一至周五晚上7点至早上6点,周六和周日全天营业。如果它在任何其他时间运行,它将退出

当我试图弄清楚如何在使用Start Job调用函数时将多个参数传递给函数时,我已经没有时间了,所以我只是为这三个位置创建了一个单独的函数

使用此脚本,我们能够在一周内的夜间和周末将新RDC映像部署到650多个位置

param( [参数(ValueFromPipelineByPropertyName=$true)] [字符串]$InputFile=$null )

Get-Module -ListAvailable VMware* | Import-Module | Out-Null
Import-Module ActiveDirectory

$Global:CompletedHosts = @()

$MAXVMHostCount = 50
$IrvingMaxJobs = 6
$PlanoMaxJobs = 15
$AtlantaMaxJobs = 15

Function Add-VMHosts() {
    param(
        [Parameter(Mandatory=$true)][int]$MAXVMHostCount,
        [Parameter(Mandatory=$false)][string]$InputFile
    )

    $AllVMHosts = @()

    If ($InputFile) {
        $AllVMHosts = Get-Content $InputFile
    }
    Else {
        $Jobs = (Get-Job).Name
        If ($Jobs -ne $null) {
            $Jobs = $Jobs.Trim("-TXINTATL")
        }

        ForEach ($Server in (Get-ADComputer -Server *************** -SearchBase "OU=************************" -Filter { Name -like "**********" })) {
            If ($Server.Name.Substring(10,1) -eq "0") {
                $IP = "10." + $Server.Name.Substring(11,1) + "."
            }
            Else {
                $IP = "10." + $Server.Name.Substring(10,2) + "."
            }
            If ($Server.Name.Substring(12,2) -eq "00") {
                $IP += "100"
            }
            ElseIf ($Server.Name.Substring(12,1) -eq "0") {
                $IP += $Server.Name.Substring(13,1)
            }
            Else {
                $IP += $Server.Name.Substring(12,2)
            }
            $IP += ".252"

            If ($IP -notin $Global:CompletedHosts -and $IP -notin $Jobs) {
                $AllVMHosts = $AllVMHosts + $IP
            }
        }
    }

    Connect-VIServer ******************** -User "********************" -Password "********************" | Out-Null

    $CurrentVMHostCount = (Get-VMHost -Location (Get-Datacenter "Profit Centers")).Count

    $HostCount = 0
    ForEach ($VMHost in $AllVMHosts) {
        If ($HostCount -ge ($MaxVMHostCount - $CurrentVMHostCount)) {
            Break
        }
        Else {
            $AddFailed = $false
            $ConnectFailed = $false
            $Test = $null

            $Test = Get-VMHost $VMHost -ErrorAction SilentlyContinue
            If (!$Test -and (Test-Connection $VMHost -Quiet)) {
                Try {
                    Connect-VIServer $VMHost -User "********************" -Password "********************" -WarningAction SilentlyContinue | Out-Null
                }
                Catch {
                    $ConnectFailed = $true
                }

                If (!$ConnectFailed -and (Get-VMHost $VMHost -ErrorAction SilentlyContinue | Get-VM -ErrorAction SilentlyContinue | Where { $_.Name -like "*********************" }) -eq $null -and (Test-Connection $VMHost -Quiet)) {
                    Set-VMHost -VMHost $VMHost -LicenseKey "********************" | Out-Null
                    Disconnect-VIServer -Server $VMHost -Confirm:$false | Out-Null

                    Add-VMHost $VMHost -Location (Get-DataCenter -Name "Profit Centers") -User "********************" -Password "********************" -Force:$true -ErrorAction SilentlyContinue | Out-Null
                    Start-Sleep -Seconds 5
                    Write-Host "$VMHost added to vCenter successfully"

                    $myVMHost = Get-VMHost $VMHost
                    $myVMHost | Get-VirtualPortGroup | Get-NicTeamingPolicy | Set-NicTeamingPolicy -InheritLoadBalancingPolicy $true -InheritNetworkFailoverDetectionPolicy $true -InheritNotifySwitches $true -InheritFailback $true -InheritFailoverOrder $true -WarningAction SilentlyContinue | Out-Null
                    $myVMHost | Get-VirtualPortGroup | Get-SecurityPolicy | Set-SecurityPolicy -AllowPromiscuousInherited $true -ForgedTransmitsInherited $true -MacChangesInherited $true | Out-Null

                    ForEach ($PortGroup in (Get-VirtualPortGroup -VMHost $VMHost)) {
                        $netSys = Get-View (Get-VMHost -Name $VMHost).ExtensionData.ConfigManager.NetworkSystem
                        $spec = (Get-VirtualPortGroup -Name $PortGroup.Name -VMHost $VMHost).ExtensionData.Spec
                        $spec.Policy.ShapingPolicy.Enabled = $null
                        $netSys.UpdatePortgroup($PortGroup.Name,$spec)
                    }

                    $DisconnectedNICs = Get-VMHostNetworkAdapter -VMHost $VMHost -Physical | Where-Object { $_.BitRatePerSec -eq "0" }
                    If (($DisconnectedNICs.Count -gt 0)) {
                        If (($DisconnectedNICs.DeviceName).Contains("vmnic0")) {
                            $myVMHost | Get-VirtualSwitch -Name vSwitch0 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive vmnic1,vmnic0 | Out-Null
                        }
                    }
                    $HostCount++
                }
                ElseIf ($ConnectFailed) {
                    Write-Host "Failed to connect to $VMHost" -ForegroundColor Yellow
                }
                Else {
                    Write-Host "$VMHost already has RDC Image" -ForegroundColor Yellow
                    If ($VMHost.Name -notin $Global:CompletedHosts) {
                        $Global:CompletedHosts = $Global:CompletedHosts + $VMHost.Name
                    }
                }
            }
            Else {
                Write-Host "$VMHost already exists in vCenter" -ForegroundColor Yellow
            }
        }
    }

    Get-AlarmDefinition "Network uplink redundancy lost" -ErrorAction SilentlyContinue | Set-AlarmDefinition -Enabled $false | Out-Null
    Get-AlarmDefinition "Network uplink redundancy lost" -ErrorAction SilentlyContinue | Set-AlarmDefinition -Enabled $true | Out-Null

    Disconnect-VIServer -Server * -Confirm:$false
}

Function CopyVMToHostIrving([string]$VMHost) {

    Connect-VIServer ******************** -User "********************" -Password "********************" | Out-Null

    If ((Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-RDC-PC*" }) -eq $null) {
        $NewVMName = "CED-RDC-PC" + (Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-DC-PC*" }).Name.Substring(9,4)

        New-VM -VMHost $VMHost -Name $NewVMName -Datastore (Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-DC-PC*" } | Get-Datastore).Name -Template "W2k19Std-Template-TX" -OSCustomizationSpec "Windows Server 2019 Customizations" | Out-Null

        Start-Sleep -Seconds 10
        Start-VM $NewVMName | Out-Null
        Start-Sleep -Seconds 5

        If ((Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-RDC-PC*" }) -eq $null -or (Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-RDC-PC*" }).PowerState -eq “PoweredOff”) {
            Exit
        }

        Write-Verbose -Message "Verifying that Customization for VM $NewVMName has started ..." -Verbose
        While($True) {
            $DCvmEvents = Get-VIEvent -Entity $NewVMName
            $DCstartedEvent = $DCvmEvents | Where { $_.GetType().Name -eq "CustomizationStartedEvent" }
            If ($DCstartedEvent) {
                Break
            }
            Else {
                Start-Sleep -Seconds 5
            }
        }

        Write-Verbose -Message "Customization of VM $NewVMName has started. Checking for Completed Status......." -Verbose
        While($True) {
            $DCvmEvents = Get-VIEvent -Entity $NewVMName
            $DCSucceededEvent = $DCvmEvents | Where { $_.GetType().Name -eq "CustomizationSucceeded" }
            $DCFailureEvent = $DCvmEvents | Where { $_.GetType().Name -eq "CustomizationFailed" }
            If ($DCFailureEvent) {
                Write-Warning -Message "Customization of VM $NewVMName failed" -Verbose
                Break
            }
            If ($DCSucceededEvent) {
                Break
            }
            Start-Sleep -Seconds 5
        }

        Write-Verbose -Message "Customization of VM $NewVMName Completed Successfully!" -Verbose

        Start-Sleep -Seconds 30
        Write-Verbose -Message "Waiting for VM $NewVMName to complete post-customization reboot." -Verbose
        Wait-Tools -VM $NewVMName -TimeoutSeconds 500 | Out-Null
        Start-Sleep -Seconds 30

        Shutdown-VMGuest $NewVMName -Confirm:$false | Out-Null
    }

    Disconnect-VIServer -Server * -Confirm:$false
}

Function CopyVMToHostPlano([string]$VMHost) {

    Connect-VIServer ******************** -User "********************" -Password "********************" | Out-Null

    If ((Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-RDC-PC*" }) -eq $null) {
        $NewVMName = "CED-RDC-PC" + (Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-DC-PC*" }).Name.Substring(9,4)

        New-VM -VMHost $VMHost -Name $NewVMName -Datastore (Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-DC-PC*" } | Get-Datastore).Name -Template "W2k19Std-Template-INT" -OSCustomizationSpec "Windows Server 2019 Customizations" | Out-Null

        Start-Sleep -Seconds 10
        Start-VM $NewVMName | Out-Null
        Start-Sleep -Seconds 5

        If ((Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-RDC-PC*" }) -eq $null -or (Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-RDC-PC*" }).PowerState -eq “PoweredOff”) {
            Exit
        }

        Write-Verbose -Message "Verifying that Customization for VM $NewVMName has started ..." -Verbose
        While($True) {
            $DCvmEvents = Get-VIEvent -Entity $NewVMName
            $DCstartedEvent = $DCvmEvents | Where { $_.GetType().Name -eq "CustomizationStartedEvent" }
            If ($DCstartedEvent) {
                Break
            }
            Else {
                Start-Sleep -Seconds 5
            }
        }

        Write-Verbose -Message "Customization of VM $NewVMName has started. Checking for Completed Status......." -Verbose
        While($True) {
            $DCvmEvents = Get-VIEvent -Entity $NewVMName
            $DCSucceededEvent = $DCvmEvents | Where { $_.GetType().Name -eq "CustomizationSucceeded" }
            $DCFailureEvent = $DCvmEvents | Where { $_.GetType().Name -eq "CustomizationFailed" }
            If ($DCFailureEvent) {
                Write-Warning -Message "Customization of VM $NewVMName failed" -Verbose
                Break
            }
            If ($DCSucceededEvent) {
                Break
            }
            Start-Sleep -Seconds 5
        }

        Write-Verbose -Message "Customization of VM $NewVMName Completed Successfully!" -Verbose

        Start-Sleep -Seconds 30
        Write-Verbose -Message "Waiting for VM $NewVMName to complete post-customization reboot." -Verbose
        Wait-Tools -VM $NewVMName -TimeoutSeconds 500 | Out-Null
        Start-Sleep -Seconds 30

        Shutdown-VMGuest $NewVMName -Confirm:$false | Out-Null
    }

    Disconnect-VIServer -Server * -Confirm:$false
}

Function CopyVMToHostAtlanta([string]$VMHost) {

    Connect-VIServer ******************** -User "********************" -Password "********************" | Out-Null

    If ((Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-RDC-PC*" }) -eq $null) {
        $NewVMName = "CED-RDC-PC" + (Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-DC-PC*" }).Name.Substring(9,4)

        New-VM -VMHost $VMHost -Name $NewVMName -Datastore (Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-DC-PC*" } | Get-Datastore).Name -Template "W2k19Std-Template-ATL" -OSCustomizationSpec "Windows Server 2019 Customizations" | Out-Null

        Start-Sleep -Seconds 10
        Start-VM $NewVMName | Out-Null
        Start-Sleep -Seconds 5

        If ((Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-RDC-PC*" }) -eq $null -or (Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-RDC-PC*" }).PowerState -eq “PoweredOff”) {
            Exit
        }

        Write-Verbose -Message "Verifying that Customization for VM $NewVMName has started ..." -Verbose
        While($True) {
            $DCvmEvents = Get-VIEvent -Entity $NewVMName
            $DCstartedEvent = $DCvmEvents | Where { $_.GetType().Name -eq "CustomizationStartedEvent" }
            If ($DCstartedEvent) {
                Break
            }
            Else {
                Start-Sleep -Seconds 5
            }
        }

        Write-Verbose -Message "Customization of VM $NewVMName has started. Checking for Completed Status......." -Verbose
        While($True) {
            $DCvmEvents = Get-VIEvent -Entity $NewVMName
            $DCSucceededEvent = $DCvmEvents | Where { $_.GetType().Name -eq "CustomizationSucceeded" }
            $DCFailureEvent = $DCvmEvents | Where { $_.GetType().Name -eq "CustomizationFailed" }
            If ($DCFailureEvent) {
                Write-Warning -Message "Customization of VM $NewVMName failed" -Verbose
                Break
            }
            If ($DCSucceededEvent) {
                Break
            }
            Start-Sleep -Seconds 5
        }

        Write-Verbose -Message "Customization of VM $NewVMName Completed Successfully!" -Verbose

        Start-Sleep -Seconds 30
        Write-Verbose -Message "Waiting for VM $NewVMName to complete post-customization reboot." -Verbose
        Wait-Tools -VM $NewVMName -TimeoutSeconds 500 | Out-Null
        Start-Sleep -Seconds 30

        Shutdown-VMGuest $NewVMName -Confirm:$false | Out-Null
    }

    Disconnect-VIServer -Server * -Confirm:$false
}

$Functions = [scriptblock]::Create(@"
    Function CopyVMToHostIrving { $Function:CopyVMToHostIrving([string]$myVMHost) }
    Function CopyVMToHostPlano { $Function:CopyVMToHostPlano([string]$myVMHost) }
    Function CopyVMToHostAtlanta { $Function:CopyVMToHostAtlanta([string]$myVMHost) }
"@)

$TotalHostNum = (Get-ADComputer -Server ******************** -SearchBase "OU=********************" -Filter { Name -like "*CED-SQL-PC*" }).Count

While ($Global:CompletedHosts.Count -lt $TotalHostNum) {
    Connect-VIServer ******************** -User "********************" -Password "********************" | Out-Null

    Write-Host "Removing completed hosts from vCenter..."
    ForEach ($VMHost in (Get-VMHost -Location (Get-Datacenter "Profit Centers") | Where-Object { $_.Parent -ne "Upgrade Failures" })) {
        If (((Get-VMHost $VMHost | Get-VM | Where { $_.Name -like "CED-RDC-PC*" }) -ne $null -and $VMHost.Name -notin (Get-Job -State Running).Name.Trim("-TXINTATL")) -or (Get-VMHost $VMHost).ConnectionState -ne "Connected") {
            Remove-VMHost $VMHost -Confirm:$false | Out-Null
            If ($VMHost.Name -notin $Global:CompletedHosts) {
                $Global:CompletedHosts = $Global:CompletedHosts + $VMHost.Name
            }
        }
    }

    Write-Host "Adding additional hosts to vCenter..."
    If ($InputFile) {
        Add-VMHosts -MAXVMHostCount $MAXVMHostCount -InputFile $InputFile
    }
    Else {
        Add-VMHosts -MAXVMHostCount $MAXVMHostCount
    }

    $VMHosts = (Get-VMHost -Location (Get-Datacenter "Profit Centers") | Where-Object { $_.Parent -ne "Upgrade Failures" })

    Disconnect-VIServer -Server * -Confirm:$false

    ForEach ($VMHost in $VMHosts) {
        Write-Host "Checking if max job count has been reached..."
        While ((Get-Job -State Running).Count -ge ($IrvingMaxJobs + $PlanoMaxJobs + $AtlantaMaxJobs)) {
            If (((Get-Date).hour -ge 6 -and (Get-Date).hour -lt 18) -and (Get-Date).DayOfWeek -ne "Saturday" -and (Get-Date).DayOfWeek -ne "Sunday") {
                Write-Host "Total count of hosts that new VM was copied to: $Global:CompletedHosts.Count"
                Exit
            }
            Else {
                Start-Sleep -Seconds 60
            }
        }

        Write-Host "Removing completed jobs..."
        ForEach ($Job in (Get-Job -State Completed)) {
            If ($Job.Name.Trim("-TXINTATL") -notin $GlobalCompletedHosts) {
                $Global:CompletedHosts = $Global:CompletedHosts + $Job.Name.Trim("-TXINTATL")
            }
            Remove-Job -Id $Job.Id
        }

        Write-Host "Starting jobs..."
        If ((Get-Job | Where { $_.Name.Trim("-TXINTATL") -eq $VMHost.Name }) -eq $null -and $VMHost.Name -notin $Global:CompletedHosts) {
            If ((Get-Job | Where { $_.Name -like "*-TX" }).Count -lt $IrvingMaxJobs) {
                Write-Host "Starting job for $VMHost using Irving Source" -ForegroundColor Yellow
                Start-Job -InitializationScript $Functions -Script { CopyVMToHostIrving($Args[0]) } -ArgumentList $VMHost.Name -Name ($VMHost.Name + "-TX") | Out-Null
            }
            ElseIf ((Get-Job | Where { $_.Name -like "*-INT" }).Count -lt $PlanoMaxJobs) {
                Write-Host "Starting job for $VMHost using Plano Source" -ForegroundColor Yellow
                Start-Job -InitializationScript $Functions -Script { CopyVMToHostPlano($Args[0]) } -ArgumentList $VMHost.Name -Name ($VMHost.Name + "-INT") | Out-Null
            }
            ElseIf ((Get-Job | Where { $_.Name -like "*-ATL" }).Count -lt $AtlantaMaxJobs) {
                Write-Host "Starting job for $VMHost using Atlanta Source" -ForegroundColor Yellow
                Start-Job -InitializationScript $Functions -Script { CopyVMToHostAtlanta($Args[0]) } -ArgumentList $VMHost.Name -Name ($VMHost.Name + "-ATL") | Out-Null
            }
        }
    }
}