Useful One-Liners (PowerCLI)

From vwiki
Revision as of 21:26, 25 October 2011 by Sstrutt (talk | contribs) (Move of content from original PowerCLI/VIToolkit page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 *

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\