以下是我在过去几天编写的一个脚本,它将一个 VM 模板部署到我们大约 650 台服务器上。它从全国不同数据中心的 3 个不同的 VM 主机部署它。它同时从欧文部署 5 个,从普莱诺部署 15 个,同时从亚特兰大部署 15 个。它在周一至周五晚上 7 点至早上 6 点之间运行,周六和周日全天运行。如果它在任何其他时间运行,它就会退出。
当我试图弄清楚在使用 Start-Job 调用该函数时如何将多个参数传递给一个函数时,我没时间了,所以我只是为这三个位置创建了一个单独的函数。
使用此脚本,我们能够在大约一周内的晚上和周末将新的 RDC 映像部署到 650 多个位置。
参数([参数(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
}
}
}
}