Useful One-Liners (PowerCLI)

From vwiki
Jump to navigation Jump to search


This page is now depreciated, and is no longer being updated.
The page was becoming too large - all content from this page, and newer updates, can be found via the Category page link below.

This page and its contents will not be deleted.

See PowerCLI

Virtual Centre

Get vCentre software version

Get vCentre software version etc

 (Get-View ServiceInstance).Content.About

Get all permissions for a user

Get all permissions for a particular user

 Get-VIPermission -Principal "DOMAIN\user" | Select @{Name="Folder"; Expression={ (Get-Folder -Id $_.EntityId).Name}}, Role

Virtual Machine

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")

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}

ESX Host

Get ESX software version etc

Get ESX software version (inc build no)

 get-vmhost -name MyESX* | %{(Get-View $_.ID).Config.Product}

Get VLANs

Get VLANs configured on ESX

 Get-VMHost MyESX* | Get-VirtualPortGroup | Sort VLanId | Select VLanId, Name

Get Disk VML ID's

Get ESX disk VML ID's (SAN ID are a part of the VML - useful for identifying LUN's with SAN team). See ESX SAN LUN ID

 Get-ScsiLun -VMHost MyESX* -LunType disk

Get SCSI paths

Get ESX SCSI paths info

 Get-ScsiLun -VMHost MyESX* -LunType disk | Get-ScsiLunPath

Get Fibre Channel HBA info

Get ESX Fibre Channel HBA info

 Get-VMHostHba -VMHost MyESX* | Where {$_.Type -eq "FibreChannel"} | Format-List *

Get HA Cluster Primaries

Display ESX hosts that are HA Primaries

 Get-Cluster | Get-HAPrimaryVMHost | Select Name

Recent Storage Errors

Display recent ESX storage errors

 Get-VIEvent -Entity (Get-VMHost MyESX*) -Type error | Where {$_.FullFormattedMessage -like "*storage*"} | Select CreatedTime, FullFormattedMessage

ESXi Logs

Get Log

Generate diagnostic bundle

 Get-Log -VMHost (Get-VMHost "MyESX*") -Bundle -DestinationPath C:\Users\name

VPXA

Get vpxa log

 Get-Log vpxa -VMHost (Get-VMHost "MyESX*") | Select -ExpandProperty Entries

Messages

Get messages log

 Get-Log messages -VMHost (Get-VMHost "MyESX*") | Select -ExpandProperty Entries

HostD

Get hostd log from line 1000

 Get-Log hostd -VMHost (Get-VMHost "MyESX*") -StartLineNum 1000 -NumLines 100 | Select -ExpandProperty Entries

Filtered HostD

Get hostd log entries that include FindMe

 Get-Log hostd -VMHost (Get-VMHost "MyESX*") | Select -Expand Entries | Select-String FindMe

Grid View

Dump hostd log into GridView

 Get-Log hostd -VMHost (Get-VMHost "MyESX*") | Select -Expand Entries | Out-GridView

Storage

Get VMDKs

Get the virtual HD (VMDK) details for a particular VM

 Get-VM -Name "MyVM" | Get-HardDisk | Format-List

Get the VMDK details for a particular VM (as seen from within guest OS)

 (Get-VM -name "MyVM" | Get-VMGuest).Disks

Get the VMDK details for all VM’s in a folder (sorted by VMDK path)

 Get-VM -Location (Get-Folder -Name "MyFolder") | Get-HardDisk | sort-object -Property Filename

Create PowerShell drive

Create a PowerShell drive for the MyDatastore datastore

 New-PSDrive -Name DS -PSProvider ViMdatastore -Root '\' -location (Get-Datastore MyDatastore)

Upload file to datastore

Upload file to datastore (a PowerShell drive needs to be create 1st - see above)

 Copy-DatastoreItem C:\Temp\FileToUpload.vmdk -Destination DS:\VMFolder\

List VM RDMs

List VM Raw Device Mapping (RDM) disks

 Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select Parent,Name,CapacityKB,Filename,ScsiCanonicalName,DeviceName

List LUN Multipathing

List all disk multipathing

 Get-VMHost | Get-ScsiLun | Select VMHost,Vendor,Model,LunType,CapacityMB,RuntimeName,MultipathPolicy,CanonicalName | Export-Csv lunpath.csv

Set LUN Multipathing

Set all SCSI disks to use round robin multipathing

 Get-VMHost | Get-ScsiLun -LunType disk |  Set-ScsiLun -MultipathPolicy "roundrobin"

The above came from Troy Clavell's post to be found at http://communities.vmware.com/thread/339480

Note that you should also set the IOPS per path to 1 (default is 1000), there is some debate as to whether you should do this or not, HP recommend it for their SAN's, though some worry about the overhead on the ESX caused by switching paths for every IO. The choice is yours, though for further reading and how to do it...