You can customize and use the following sample script to perform operations on any number of Linux desktop machines.

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 machine, or delete virtual machine

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 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 = Read-Host 'Your vCenter address'
$vcAdmin = Read-Host 'Your vCenter admin user name'
$vcPassword = Read-Host 'Your vCenter admin user password' -AsSecureString
$vcPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($vcPassword))
"-----------------------------------------------------"
#$csvFile = Read-Host 'Csv File '
$action = Read-Host 'Select action: 1). Power On 2). Power Off 3) Shutdown VM Guest 4). Restart VM 5). Restart VM Guest 6). Delete VM'
$sleepTime = Read-Host 'Wait time (seconds) between each VM'
"-----------------------------------------------------"
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
  }
}

$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> .\InstallAgent.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

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.