You can customize and use the following sample script to perform operations on multiple Linux virtual machines (VMs). The operations include powering on, powering off, shutting down, restarting, and deleting the VMs.

This script can delete virtual machines from vCenter Server but not from View. To delete the machines from View, see the sample script Sample Script to Delete Machines from the Connection Server LDAP Database.

To copy and paste the script content without page breaks, use the HTML version of this topic, available from the Horizon 6 documentation page at https://www.vmware.com/support/pubs/view_pubs.html.

Script Input

This script reads one input file, which is described in Input File for the Sample PowerCLI Scripts to Deploy Linux Desktops. This script also interactively asks for the following information:

  • IP address of the vCenter Server

  • Administrator login name for the vCenter Server

  • Administrator password for the vCenter Server

  • Action to perform, which can be power-on, power-off, shut down guest, restart VM, restart VM guest, or delete VM.

  • The wait time, in seconds, between operations on the VMs.

Script Content

<#
.DESCRIPTION
The Tool supports:
1. Power off VMs
2. Power on VMs
3. Shutdown VMs
4. Restart VMs
5. Restart VM guest
6. Delete VMs from Disk
.NOTES
#>

#--------------------- Functions --------------------

function GetInput
{
    Param($prompt, $IsPassword = $false)
    $prompt = $prompt + ": "
    Write-Host $prompt -NoNewLine
    [Console]::ForegroundColor = "Blue"
    if ($IsPassword)
    {
        $input = Read-Host -AsSecureString
        $input = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($input))
    }
    else
    {
        $input = Read-Host
    }
    
    [Console]::ResetColor()
    return $input
}

function IsVMExists ($VMExists)
{
	Write-Host "Checking if the VM $VMExists  Exists"
	[bool]$Exists = $false

	#Get all VMS and check if the VMs is already present in VC
	$listvm = Get-vm
	foreach ($lvm in $listvm)
	{
		if($VMExists -eq $lvm.Name )
		{
			$Exists = $true
            Write-Host "$VMExists is Exist"
		}
	}
	return $Exists
}

function Delete_VM($VMToDelete)
{
	Write-Host "Deleting VM $VMToDelete"
	Get-VM $VMToDelete | where { $_.PowerState –eq "PoweredOn" } | Stop-VM –confirm:$false
	Get-VM $VMToDelete | Remove-VM –DeleteFromDisk –confirm:$false
}

#------------------ Handle input ---------------------
"-----------------------------------------------------"
$vcAddress = GetInput -prompt "Your vCenter address" -IsPassword $false
$vcAdmin = GetInput -prompt "Your vCenter admin user name" -IsPassword $false
$vcPassword = GetInput -prompt "Your vCenter admin user password" -IsPassword $true
"-----------------------------------------------------"
$action = GetInput -prompt 'Select action: 1). Power On 2). Power Off 3) Shutdown VM Guest 4). Restart VM 5). Restart VM Guest 6). Delete VM' -IsPassword $false
$sleepTime = GetInput -prompt 'Wait time (seconds) between each VM' -IsPassword $false
"-----------------------------------------------------"
[Console]::ForegroundColor = "Yellow"
switch ($action) 
{ 
  1 
  {
    "Your selection is 1). Power On"
  } 
  2 
  {
    "Your selection is 2). Power Off"
  } 
  3 
  {
    "Your selection is 3) Shutdown"
  } 
  4 
  {
    "Your selection is 4). Restart VM"
  }
  5 
  {
    "Your selection is 5). Restart VM Guest"
  }  
  6 
  {
    "Your selection is 6). Delete VM"
  } 
  default 
  {
    "Invalid selection for action: $action"
    exit
  }
}
[Console]::ResetColor()
$csvFile = '.\CloneVMs.csv'

#check if file exists
if (!(Test-Path $csvFile))
{
write-host  -ForeGroundColor Red "CSV File not found"
exit
}
"-----------------------------------------------------"

#--------------------- Main --------------------------
#Read input CSV file
Disconnect-VIServer $vcAddress -Confirm:$false
#Connect-VIServer $vcAddress -ErrorAction Stop -user $vcAdmin -password $vcPassword
Connect-VIServer $vcAddress -user $vcAdmin -password $vcPassword
$csvData = Import-CSV $csvFile

foreach ($line in $csvData)
{
    $VMName = $line.VMName
    switch ($action) 
    { 
      1 
      {
        Get-VM $VMName | Start-VM -Confirm:$false
      } 
      2 
      {
        Get-VM $VMName | Stop-VM -Confirm:$false
      } 
      3 
      {
        Get-VM $VMName | Shutdown-VMGuest -Confirm:$false
      } 
      4 
      {
        Get-VM $VMName | Restart-VM -Confirm:$false
      }
      5
      {
        Get-VM $VMName | Restart-VMGuest -Confirm:$false
      }
      6
      {
	    if (IsVMExists ($VMName))
	       {
              Delete_VM ($VMName)
	        }   
        }
      default{}
    }
    Start-Sleep -s $sleepTime
}

Disconnect-VIServer $vcAddress -Confirm:$false
exit

Script Execution

The following messages are from an execution of the script:

PowerCLI C:\scripts> .\VMOperations.ps1
Your vCenter address: 10.117.44.17
Your vCenter admin user name: administrator
Your vCenter admin user password: *******
--------------------------------------------------
Select action: 1). Power On 2). Power Off 3) Shutdown VM Guest 4). Restart VM 5). Restart VM Guest 6). Delete VM: 1
Wait time (seconds) between each VM: 20
--------------------------------------------------
Your selection is 6). Delete VM

For the operations power on, reatart VM, and restart VM guest, specify a wait time between virtual machines of at least 20 seconds to avoid a boot storm situation, which might cause some operations to fail.