home
PowerCLI cheat sheet
PowerShell SDK to manage VMware vSphere environments
print

VMware PowerCLI 6.5.1

Date : April 2017

Installation

From Powershell Gallery

Install-Module ‑Name VMware.PowerCLI –Scope CurrentUser

Offline installation

Save-Module ‑Name VMware.PowerCLI ‑Path C:\Path\To\Desired\Folder

... then copy content to the target machine in folder: *"$home\Documents\WindowsPowerShell\Modules"*

Load module

Import-Module VMware.PowerCLI

Connections

Enter credentials in a windows popup Connect-VIServer ‑Server <server> ‑credential $(Get-Credential)

Host management

Add a new ESXi to the inventory Add-VMHost <fqdn or ip> ‑Location (Get-Cluster <cLustername>) ‑User root ‑Password <password>

Set a host to maintenand mode Set-VMHost ‑VMHost <host> ‑State "Maintenance"

Power operations on ESXi

  • Restart-VMHost ‑VMHost <host>
  • Stop-VMHost ‑VMHost <host>

Retrieves information about host services Get-VMHostService <host>

Restart a specific host service Get-VMHostService <host> | ? {($_.Key ‑eq "TSM-ssh")} | Restart-VMHostService

Cluster management

Get cluster information Get-Cluster ‑Name <clustername>

Enable HA on cluster Set-Cluster ‑Cluster <cluster> ‑HAEnabled:$true ‑HAAdmissionControlEnabled:$true

Enable DRS on cluster Set-Cluster ‑Cluster <cluster> ‑DRSEnabled:$true ‑DRSAutomationLevel "FullyAutomated"

Set EVC mode on cluster Set-Cluster ‑Cluster <cluster> ‑EVCMode "intel-nehalem"

vApp management

Export a vApp in OVA format Export-VApp ‑vApp <vapp> ‑Destination "c:\vapps\" ‑Format Ova

Import OVA file as vApp Import-vApp ‑Source "c:\vapps\appliance.ova" ‑Datastore <datastore> ‑VMHost <vmhost>

Configure memory ressources for a vApp Set-VApp ‑VApp <vapp> ‑MemExpandableReservation:$true ‑MemReservationGB 2 ‑MemLimitGB 4


VM management

Get VM information Get-VM <vmname>

Power operations

  • Start-VM ‑VM <vm>
  • Stop-VM ‑VM <vm>
  • Restart-VM ‑VM <vm>

Open console in the web browser Get-VM <vm> | Open-VMConsoleWindow

Convert a VM to a template Set-VM ‑VM <vm> ‑ToTemplate ‑Name <templatename>

Connect ISO file to a VM New-CDDrive ‑VM <vm> ‑ISOPath "[<datastorename>] ISO\sample.iso"

Disconnect any CD drive from a VM Get-CDDrive ‑VM <vm> | Set-CDDrive ‑connected 0 ‑StartConnected 0 ‑NoMedia

Attach a new persistent disk New-HardDisk ‑VM <vm> ‑CapacityGB 10 ‑Persistence persistent

Snapshot management

Create snapshot of a VM with active memory New-Snapshot ‑VM <vm> ‑Name "beforePatch" ‑Memory

List snapshots of a VM $snapshot = Get-Snapshot ‑VM <vm>

Remove snapshot Remove-Snapshot ‑Snapshot <snapshot>

Guest OS operations

Power operations

  • Shutdown-VMGuest ‑VM <vm>
  • Restart-VMGuest ‑VM <vm>

(dis)Mounts the VMware Tools CD installer Mount-Tools ‑VM <vm> Dismount-Tools ‑VM <vm>

Datastore

Get datastore information Get-Datastore ‑Name <dsname>

Create (NFS|VMFS) datastores New-Datastore ‑Nfs ‑VMHost <host> ‑Name <dsname> ‑Path </path/folder> ‑NfsHost <nfsserver> New-Datastore ‑Vmfs ‑VMHost <host> ‑Name <dsname> ‑Path <scsiLun.CanonicalName>

Network

Get vSphere distributed switch or port group Get-VDSwitch ‑name <name> Get-VDPortgroup ‑Name <name>

Create a vSphere distributed switch New-VDSwitch ‑Name <dvsname> ‑Location <datacenter>

Create a distributed port group New-VDPortgroup ‑VDSwitch <dvswitch> ‑Name <pgname> ‑NumPorts 8 ‑VLanId 4

Backup/Export of a vSphere distributed switch Export-VDSwitch ‑VDSwitch <dvswitch> ‑Destination "c:\mybkp.zip" ‑WithoutPortGroups