From 80c4ab3c4303bfb2c7fcfe6dc29b8dfa3c11417c Mon Sep 17 00:00:00 2001 From: David Schroeder Date: Mon, 5 Feb 2024 22:38:00 -0600 Subject: [PATCH] Create offsite-vm-shutdown.ps1 --- esxi-scripts/offsite-vm-shutdown.ps1 | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100755 esxi-scripts/offsite-vm-shutdown.ps1 diff --git a/esxi-scripts/offsite-vm-shutdown.ps1 b/esxi-scripts/offsite-vm-shutdown.ps1 new file mode 100755 index 00000000..9a45d7ba --- /dev/null +++ b/esxi-scripts/offsite-vm-shutdown.ps1 @@ -0,0 +1,16 @@ +#!/usr/bin/env pwsh +. /opt/idssys/powerwall/settings.ps1 + +Connect-VIServer -Server $args[0] -Protocol https -User $ESXiUsername -Password $ESXiPassword | Out-Null + +Get-VM | Where {$_.PowerState -like '*On*' -and $_.Name -notlike '*CLS*' -and $_.Name -notlike '*iSCSI*'} +Foreach ($vm in $vms) +{ + $migrating=Get-Task | ?{$_.ObjectId -match 'VirtualMachine'} | Select @{N='VM';E={(Get-View -Id $_.ObjectId).Name }},State,Description | where {$_.VM -eq $vm -and $_.Description -like '*Relocate*' -and $_.State -eq 'Running'} + if ([string]::IsNullOrEmpty($migrating)) { + # Get-VM $vm | Shutdown-VMGuest -Confirm:$false + write-host "Shutting down: $_.Name" + } +} + +Disconnect-VIServer -Server $args[0] -Force –Confirm:$false