Essential PowerCLI commands & practical examples to manage VMware environments via PowerShell!
VMware
Published on
Karl Certa Systems & network administrator5 years in IT, from support to sysadmin, now Ops. Learning cloud, and writing everything down here. Focused on IaC & cloud AWS SAA, Kubernetes next LinkedIn karlcerta.fr GitHub Karl Certa
# 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 hostGet-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 VMsGet-VM | Select Name, PowerState, NumCpu, MemoryGB, @{N="UsedSpaceGB";E={[math]::Round($_.UsedSpaceGB,2)}}, @{N="VMHost";E={$_.VMHost.Name}} | Export-Csv "VM-Report.csv" -NoTypeInformation