воскресенье, 29 мая 2022 г.

Discard VMs suspended state in vCloud with PowerCLI

If you have experienced difficulty in removing a VM from a suspended state within a vApp, then you will understand what I am referring to. However, if this is not something you have encountered, then you may skip this.


Unfortunately, we often encounter problematic states such as suspended states. However, this script can be very useful in completely discarding the suspended state.

==START==
cls
Write-Host "=Suspended VMs=" -BackgroundColor DarkRed -ForegroundColor Yellow
Write-Host "This script shows all suspended VMs on vCenter VCRD" -ForegroundColor Yellow
$host.UI.RawUI.WindowTitle = "=Suspended VMs to Stop="

Write-Host ""
$vCenter = ""
$User = ""
$Pass = ""
$RealPass = ""
$GetVM = ""
$Suspended = ""
$ToStop = ""
$ToStopCount = ""
$StopVM = ""

#=== Policy/Configuration ===#
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:$false | Out-Null
$ErrorActionPreference= 'SilentlyContinue'
$WarningActionPreference= 'SilentlyContinue'
Add-PsSnapin VMware.VimAutomation.Core

#=== My Login Credentials ===#
$vCenter = 'VCRD.algotec.co.il'
$User = $env:UserName
$SecurePass = Read-Host -AsSecureString "Please enter password for $UserName and wait..."
$Pass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePass))
If(-not$Pass){Write-Host "You must enter a password!" -ForegroundColor Yellow -BackgroundColor Black; Write-Host; Break}

Write-Host "I am connecting you to vCenter..." -ForegroundColor Yellow
Disconnect-VIServer * -Confirm:$false 
If (!($global:defaultviserver.IsConnected)){$VIServerConnected = Connect-VIServer -Server $vCenter -User $User -Password $Pass}
If(!($VIServerConnected)){Write-Host "$vCenter is NOT Connected! Try another password." -BackgroundColor Black -ForegroundColor Yellow; Break}
Else{Write-Host "vCenter is Connected!" -ForegroundColor Green -BackgroundColor DarkRed}
Write-Host 

Write-Host "I am getting all Suspended VMs..." -ForegroundColor Yellow
$GetVM = Get-VM
$Suspended = $GetVM | Where-Object -Property PowerState -EQ 'Suspended'
If(!$Suspended){Write-Host "Okay. There is NO Suspended VMs!" -ForegroundColor Yellow -BackgroundColor DarkRed; Break}
$Suspended | Select Name, Powerstate, VApp, VMHost
$Count = $Suspended.count
Write-Host
Write-Host "The number of Suspended VMs is $Count" -ForegroundColor Green
$ToStop = $Suspended | Out-GridView -Title "Suspended VMs to Stop" -PassThru
Write-Host
$ToStopCount = $ToStop.count
Write-Host "The number of VMs selected to Stop $ToStopCount" -ForegroundColor Green
Write-Host

foreach($StopVM in $ToStop)
    {
    $StopVM | Stop-VM -Confirm:$false | Out-Null
    $StoppedName = $StopVM.name
    Write-Host "$StoppedName is stopped" -ForegroundColor White -BackgroundColor DarkRed
    }

Write-Host "$ToStopCount VMs are stopped" -ForegroundColor Black -BackgroundColor White
==FINISH==

If there are no suspended VMs in your environment, you are lucky.



Комментариев нет:

Отправить комментарий