Skip to main content

Suspend vSphere VMs with VMware PowerCLI

invoke-command -scriptblock {
#using script block to bypass Execution Policy massage

#install VMware.PowerCLI follow below steps
#run powershell as admin
#run the command to intall
#Install-Module -Name VMware.PowerCLI
#Install-Module VMware.PowerCLI -Scope CurrentUser
#Set-executionpolicy remotesign
#Import-Module VMware.PowerCLI
#Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

#now install steps over restart powershell

#To disable this warning and set your preference use the following command and restart PowerShell
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true -Confirm:$false

#Connect to vCenter

$Server = Connect-VIServer -Server 192.168.12.97 -User xxx@vsphere.local -Password xxxxxxxxxx


#Gets the list of all Hosts in a specific folder

$vmHosts = Get-VMHost -Location Singapore


Foreach($vmHost in $vmHosts)
{
    $vmHost.name
    $vms = Get-vmhost -name $vmHost.name | Get-VM | where { $_.PowerState -eq "PoweredOn"}

    Foreach ($vm in $vms){
        #use condition if want to suspend specific vm
        if($vm.Name -eq "xx-xxx-Win2016-DevVM" -or $vm.Name -eq "xx-xxx-Win2016-TGS" -or $vm.Name -eq "xx-xxxx-Win10-HP")
        {
            Suspend-VM $vm -Confirm:$false -RunAsync
            sleep -Seconds 5
        }
    }
}

Disconnect-VIServer -Server $Server -Confirm:$false

read-host “Press ENTER to continue...”

}

Comments

Popular posts from this blog

how to check powershell version

 Run powershell as administrator      Run below command $PSVersionTable.PSVersion  

Calculate possible ceiling max values based on denominator

                    double higherMaxValue = 100; double maxValue = 1235; int cntDigit = maxValue >= 10 ? maxValue.ToString().Length : 1; int denominator = (int)Math.Pow(10, cntDigit - 1); higherMaxValue = maxValue + denominator - (maxValue % denominator); Console.WriteLine("maxValue:"+maxValue+" higherMaxValue:"+higherMaxValue);                       // maxValue:1235 higherMaxValue:2000