PowerCLI Examples for VMUG Presentation 7/20/2010
These are some examples that I will be reviewing at an upcoming Central Ohio VMware User’s Group.
# List all VMs Get-VM # Get one VM Get-VM ubuntu01 # Get VMs that match Get-VM ubuntu* # List VMs in a Datacenter Get-Datacenter dc001 | Get-VM # List VMs in a cluster Get-Cluster cluster001 | Get-VM # Get 2 CPU or larger VMs Get-VM | Where-Object {$_.NumCPU -ge 2} # Get 2 GB RAM or larger VMs Get-VM | Where-Object {$_.MemoryMB -ge 2048} # Getting information about what you can do Get-VICommand Get-Command Get-Help Get-VM Get-Member Get-VM | Get-Member # Save a report of VM CPU/MEM configuration information Get-VM | Select-Object Name, NumCPU, MemoryMB | Export-Csv "VM_CPU_Memory_20100720.csv" -NoTypeInformation # List VMs with connected CD-ROMs Get-VM | Where-Object {$_ | Get-CDDrive | Where-Object {$_.ConnectionState.Connected}} # Disconnect all connected CD-ROMs Get-VM | Get-CDDrive | Where-Object {$_.ConnectionState.Connected} | Set-CDDrive -Connected $false -Confirm:$false # Migrate a template to another host Get-Template ubuntu | Set-Template -ToVM | Move-VM -Destination (Get-VMHost esx001.lab.local) | Set-VM -ToTemplate -Confirm:$false # Report on Storage Get-Datastore | Select-Object Name, FreeSpaceMB, CapacityMB | Export-Csv "Datastores_20100720.csv" -NoTypeInformation # Storage VMotion ALL VMs from one LUN to another (All at the same time) COULD BE BAD Get-Datastore "cla_nonrep_01" | Get-VM | Move-VM -Datastore (Get-Datastore "cla_nonrep_00") # Storage VMotion ALL VMs from one LUN to another (One at a time) Get-Datastore "cla_nonrep_01" | Get-VM | ForEach-Object {Move-VM -VM $_ -Datastore (Get-Datastore "cla_nonrep_00")} # Set all limited CPU reservations to unlimted Get-VM | Get-VMResourceConfiguration | Where-Object { $_.CpuLimitMhz -ne -1} | Set-VMResourceConfiguration -CpuLimitMhz $null # Set all limited memory reservations to unlimited Get-VM | Get-VMResourceConfiguration | Where-Object { $_.MemLimitMB -ne -1} | Set-VMResourceConfiguration -MemLimitMB $null # Configure a new network on a host Get-VirtualSwitch -VMHost esx003.lab.local -Name vSwitch0 | New-VirtualPortGroup -Name "VGT Test" -VLanId 4095 # Configure a new network on a cluster Get-VirtualSwitch -VMHost (Get-Cluster cluster001 | Get-VMHost) -Name vSwitch0 | New-VirtualPortGroup -Name "VGT Test" -VLanId 4095 # Generate a diagnostic log bundle Get-Log -VMHost esx001.lab.local -Bundle -DestinationPath c:\hostlogs # List the entries in vmkwarning (Get-Log -VMHost esx001.lab.local -Key vmkwarning).Entries # Find something in a log (Get-Log -VMHost esx001.lab.local -Key vmkernel).Entries | Select-String "Error" # What logs are available?] Get-LogType -VMHost esx001.lab.local # Find VMs with snapshots (Pretty slow - could be optimized) Get-VM | Where-Object {$_ | Get-Snapshot} #Verify advanced settings # http://wannemacher.us/?p=135 # Change advanced settings # http://wannemacher.us/?p=104 # Change the root password across multiple hosts # http://wannemacher.us/?p=304
Hey Eric, how can i combine my powershell console, with my powercli, I want to be able to use one console for powershell, and vmware powercli, is this possible? Thanks
Ken,
This was turning into a longer comment so I just put a new post up on how to do this. Feel free to email me at eric@ if you have any questions.