Virtual Machine PowerCLI One-Liners

From vwiki
Jump to navigation Jump to search

{{#widget:Widget:GoogleAdSenseSkyscraper}}

Get list of VM's

Get list of VM's

 Get-VM

Sort by Name

Get list of VM's, sorted by name

 Get-VM | Sort -Property Name

Within a Folder

Get VM within a specific folder (for when you've VM's with identical names if different folders)

 Get-VM -Name MyVM -Location (Get-Folder -Name "MyFolder")

Within a vApp

Get VM within a specific vApp

 Get-VM -Location (Get-vApp "vApp name")

{{#widget:Widget:GoogleAdSenseSkyscraper}}

Get VM guest Info

Get VM guest OS info (IP address, etc)

 Get-VMGuest -VM (Get-VM -Name MyVM)

Get VM's UUID

Get list of VM's and their UUID's

 Get-VM MyVM | %{(Get-View $_.Id).config.uuid}

Get VM's VMX Path

Get list of virtual machine config file paths in a particular vApp (for VM's in a folder use Get-Folder instead of Get-vApp)

 Get-VM -Location (Get-vApp "vApp Name") | Get-View | %{$_.Config.Files.VmPathName} | Sort

Get VM Power On/Off Events

Get Power On/Off times and events for VM

 get-VM MyVM | Get-VIEvent | Where {$_.fullFormattedMessage -like "*Power*"}

Migration

Storage vMotion

Storage vMotion (SvMotion) a VM to a different datastore, most commonly used on ESX3 / VI3 where storage vMotion is supported by the infrastructure, but not the VI Client!

 Get-VM MyVM | Move-VM -datastore (Get-datastore "DestinationDatastore")

Move VMs to New PortGroup Network

The following will reconfigure all VM in the specified folder to move from network A to network B

 Get-VM -Location (Get-Folder "InThisFolder") | Get-NetworkAdapter | Where {$_.NetworkName -eq "Network A"} | Set-NetworkAdapter -NetworkName "Network B" -Confirm:$false

Borrowed in part from http://www.virtu-al.net/2009/10/19/powercli-mass-vm-portgroup-change/

Gracefully Shutdown a VM

Sends OS shutdown command via VM Tools

 Shutdown-VMGuest -VM (Get-VM -name "vserver1")

Snapshots

Get Snapshot info for a VM

Get Snapshot size, date created, and other info for a VM

 Get-VM MyVM | Get-Snapshot | Select-Object -Property Name, Description, Created, SizeMB | Format-List *

Start Snapshot on Multiple VMs

Assumes you've already got an array object of VMs containing the VM's you want to snapshot (eg $VMs = Get-VM -Location (Get-Folder -Name "MyFolder") for VM's in a particular folder, see Get_list_of_VM's for further info)

New-Snapshot -Name "Pre change XYZ snapshot" -Description "Requested by TJ Hooker" -Memory -Quiesce -VM $vms

Roll-back to Snapshot on Multiple VMs

Rolls back VM's to snapshots previously taken (assumes you've already got an array object of VMs containing the VM's you want to rollback for)

foreach ($vm in $vms) {Set-VM -VM $vm -Snapshot (Get-Snapshot -VM $vm -Name "Pre change XYZ snapshot") -Confirm:$false}

Delete/Merge Snapshot on Multiple VMs

Deletes snapshots previously taken so that VMs continue as they are currently, but lose the ability to fall-back to the snapshot (assumes you've already got an array object of VMs containing the VM's you want to delete snapshots for)

foreach ($vm in $vms) {Remove-Snapshot -Snapshot (Get-Snapshot -VM $vm -Name "Pre change XYZ snapshot") -Confirm:$false}