VMware PowerCLI is the PowerShell command-line interface for administering vSphere, vCenter, and VMware Cloud environments.
Installation
# Install PowerCLI from PowerShell Gallery
Install-Module - Name VMware.PowerCLI - Scope CurrentUser
# Verify installation
Get-Module - Name VMware.PowerCLI - ListAvailable
Common installation error
If you encounter this error when starting PowerCLI:
Could not load file or assembly 'log4net, Version=1.2.10.0, Culture=neutral,
PublicKeyToken=692fbea5521e1304' or one of its dependencies.
Cause : Conflict with SAP Crystal Reports which installs its own version of log4net
Solution :
Uninstall “SAP Crystal Reports runtime engine for .NET Framework”
More info: Broadcom Article
Connection and configuration
Action Command Connect to vCenter/ESXi Connect-VIServer server-01Connect with credentials Connect-VIServer server-01 -User admin -Password passList active connections $global:DefaultVIServersDisconnect from all Disconnect-VIServer * -Confirm:$falseIgnore SSL certificates Set-PowerCLIConfiguration -InvalidCertificateAction Ignore
VM Management
Action Command List all VMs Get-VMGet a specific VM Get-VM "vm-name"Start a VM Start-VM "vm-name"Graceful shutdown (with VMware Tools) Stop-VMGuest "vm-name"Forced shutdown (power button) Stop-VM "vm-name" -Confirm:$falseGraceful restart Restart-VMGuest "vm-name"Suspend a VM Suspend-VM "vm-name"
VM Configuration
Action Command Modify CPU and RAM Set-VM "vm-name" -NumCpu 4 -MemoryGB 8Add a hard disk New-HardDisk -VM "vm-name" -CapacityGB 50Add a network adapter New-NetworkAdapter -VM "vm-name" -NetworkName "VLAN-100"Modify notes Set-VM "vm-name" -Notes "Description"Create a new VM New-VM -Name "new-vm" -VMHost server-01 -Datastore DS1
ESXi Host Management
Action Command List all ESXi hosts Get-VMHostSpecific host information Get-VMHost server-01Enter maintenance mode Set-VMHost server-01 -State MaintenanceExit maintenance mode Set-VMHost server-01 -State ConnectedRestart an ESXi host Restart-VMHost server-01VMs on a specific host Get-VMHost server-01 | Get-VM
Datastore Management
Action Command List all datastores Get-DatastoreDatastore information Get-Datastore "DS-PROD"Available disk space Get-Datastore | Select Name, CapacityGB, FreeSpaceGBVMs on a datastore Get-Datastore "DS-PROD" | Get-VM
Snapshots
Action Command Create a snapshot New-Snapshot -VM "vm-name" -Name "snapshot-name"List snapshots Get-VM "vm-name" | Get-SnapshotRestore a snapshot Set-VM "vm-name" -Snapshot "snapshot-name"Delete a snapshot Remove-Snapshot -Snapshot "snapshot-name"Delete all snapshots Get-VM "vm-name" | Get-Snapshot | Remove-Snapshot
Migrations (vMotion)
Action Command vMotion to another host Move-VM -VM "vm-name" -Destination server-01Storage vMotion Move-VM -VM "vm-name" -Datastore "DS-PROD"Full migration Move-VM -VM "vm-name" -Destination server-01 -Datastore DS1
Practical examples
First connection and configuration
# Connect to vCenter and ignore certificates
Connect-VIServer server - 01
Set-PowerCLIConfiguration - InvalidCertificateAction Ignore - Confirm: $false
Quick resource inventory
# Environment overview
Write-Host " === VMWARE INVENTORY === "
Write-Host " ESXi Hosts: $ ( Get-VMHost | Measure-Object | Select - ExpandProperty Count) "
Write-Host " Total VMs: $ ( Get-VM | Measure-Object | Select - ExpandProperty Count) "
Write-Host " Powered On VMs: $ ( Get-VM | Where { $_ .PowerState -eq ' PoweredOn ' } | Measure | Select - ExpandProperty Count) "
Complete VM creation
# Create a new VM with configuration
$VMParams = @ {
Name = " WEB-SERVER-01 "
VMHost = " server-01 "
Datastore = " DS-PROD "
NumCpu = 2
MemoryGB = 4
DiskGB = 40
NetworkName = " VLAN-100 "
}
New-VM @VMParams
Host maintenance mode
# Put a host in maintenance (automatically migrates VMs)
Write-Host " Migrating VMs... "
Set-VMHost server - 01 - State Maintenance - VsanDataMigrationMode EnsureAccessibility
Snapshot before maintenance
# Create snapshots for all VMs on a host
Get-VMHost server - 01 | Get-VM | Where { $_ .PowerState -eq " PoweredOn " } |
New-Snapshot - Name " Before-Maintenance- $ ( Get-Date - Format ' yyyyMMdd ' ) " - Memory: $false
Resource usage report
# Generate a CSV report of VMs
Get-VM | Select Name , PowerState , NumCpu , MemoryGB ,
@ {N = " UsedSpaceGB " ;E = {[ math ]::Round( $_ .UsedSpaceGB , 2 )}} ,
@ {N = " VMHost " ;E = { $_ .VMHost.Name }} |
Export-Csv " VM-Report.csv " - NoTypeInformation