Useful One-Liners (PowerCLI)

From vwiki
Jump to navigation Jump to search

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

Get VM guest Info

Get VM guest OS info (IP address, etc)

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

Get VM's UUID

Get VM's UUID

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

Storage vMotion

Storage vMotion (SvMotion) a VM to a different datastore

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

Gracefully Shutdown a VM

Sends OS shutdown command via VM Tools

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

Get VM Power On/Off Events

Get Power On/Off times and events for VM

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

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 *

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/

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...