function Stop-VMParallel { param([array]$VMs, [int]$Throttle = 8) ``` $VMs | ForEach-Object -Parallel { Import-Module VMware.PowerCLI -ErrorAction SilentlyContinue $vm = $_ if ($vm.PowerState -ne "PoweredOn") { return } try { Shutdown-VMGuest -VM $vm -Confirm:$false -ErrorAction Stop } catch { Write-Host "[ERROR] VM failed: $($vm.Name)" } } -ThrottleLimit $Throttle ``` } function Stop-VMsByGroup { param($VMGroups) ``` $groups = $VMGroups.Gruppe | Sort-Object -Unique foreach ($g in $groups) { $names = ($VMGroups | Where-Object Gruppe -eq $g).Server $vms = Get-VM | Where-Object { $names -contains $_.Name } if ($vms) { Stop-VMParallel -VMs $vms Start-Sleep 30 } } ``` } function Stop-UnknownVMs { param($VMGroups, $ExcludeRegex) ``` $vms = Get-VM | Where-Object { ($VMGroups.Server -notcontains $_.Name) -and ($_.Name -notmatch $ExcludeRegex) } if ($vms) { Stop-VMParallel -VMs $vms } ``` } function Stop-ClusterSafe { param([string]$Cluster, [string]$Reason) ``` Invoke-Retry -Name "Cluster shutdown" -Action { Stop-VsanCluster -Cluster $Cluster -PowerOffReason $Reason -ErrorAction Stop | Out-Null } ``` }