воскресенье, 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.



Find Datastore Id/Uuid/MoRef in vCenter

Was it you searched to which Datastore the error is connected?
Maybe you couldn't create a VM because of lacking free space on a Datastore, and the system got you only some Id or Moref (if you are using vCloud Director) instead of a normal Datastore name. 

If you hated these moments, so do I.
Because of that I have made some script that can give us additional info and bound Datastore names to their Id/Moref, etc.

Here it is.

===BEGINNING===

cls

Write-Host "=Datastores IDs=" -BackgroundColor DarkRed -ForegroundColor Yellow

$host.UI.RawUI.WindowTitle = "=Datastores IDs="

Get-Date -Format "dd-MM-yyyy HH:mm"

Write-Host

Write-Host "Defining Configuration:" -BackgroundColor Green -ForegroundColor Blue

$ErrorActionPreference = 'SilentlyContinue'

$WarningActionPreference = 'SilentlyContinue'

$ErrorPreference = 'SilentlyContinue'

$WarningPreference = 'SilentlyContinue'

Import-Module VMware.PowerCLI | Out-Null

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:$false | Out-Null

#$vCenter = "vCenter" ## Change as you need

#$User = "GeniousAdmin" ## Change as you need

#$Pass = "P@ssw0rd" ## Change as you need

Disconnect-VIServer * -Confirm:$false -ErrorAction SilentlyContinue | Out-Null

Connect-VIServer -Server $vCenter -User $User -Password $Pass@ | Out-Null

#####=====#####

$outputfile = "D:\Datastores_Uuid.csv"

$DStores = Get-Datastore | select * | Where-Object { $_.Name -like "Datastore_*" }

$TableArray = @()

foreach($DS in $DStores)

    {

    $DSName = $DS.Name

    $DSUuid = $DS.ExtensionData.Info.Vmfs.Uuid

    $DSId = $DS.Id

    $DSMoRef =- $DS.ExtensionData.MoRef

    $OneLineArray = New-Object "PSCustomObject"

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "Datastore" -Value $DSName

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "Datastore Uuid" -Value $DSUuid

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "Datastore Id" -Value $DSId

    $OneLineArray | Add-Member -MemberType NoteProperty -Name "Datastore MoRef" -Value $DSMoRef

    $TableArray += $OneLineArray

    }

$TableArray | OGV -Title "=Datastores IDs="

$TableArray | Export-Csv -path $outputfile -NoTypeInformation

ii $outputfile

 ===FINISH===

This the end result: