Difference between revisions of "Useful One-Liners (PowerCLI)"

Jump to navigation Jump to search
m
Updated to use Depreciated template, removed categories, and Meta robot override
(→‎Storage: Added "List VM RDMs" and "Datastore MultiPathing Policy")
m (Updated to use Depreciated template, removed categories, and Meta robot override)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
__NOINDEX__
{{Depreciated|category=PowerCLI}}
== Virtual Centre ==
== Virtual Centre ==
=== Get vCentre software version ===
=== Get vCentre software version ===
Line 20: Line 24:
Get VM within a specific folder (for when you've VM's with identical names if different folders)
Get VM within a specific folder (for when you've VM's with identical names if different folders)
<source lang="powershell"> Get-VM -Name MyVM -Location (Get-Folder -Name "MyFolder") </source>
<source lang="powershell"> Get-VM -Name MyVM -Location (Get-Folder -Name "MyFolder") </source>
==== Within a vApp ====
Get VM within a specific vApp
<source lang="powershell"> Get-VM -Location (Get-vApp "vApp name") </source>


=== Get VM guest Info ===
=== Get VM guest Info ===
Line 26: Line 34:


=== Get VM's UUID ===
=== Get VM's UUID ===
Get VM's UUID
Get list of VM's and their UUID's
<source lang="powershell"> Get-VM MyVM | %{(Get-View $_.Id).config.uuid} </source>   
<source lang="powershell"> Get-VM MyVM | %{(Get-View $_.Id).config.uuid} </source>   


=== Storage vMotion ===
=== Get VM's VMX Path ===
Storage vMotion (SvMotion) a VM to a different datastore
Get list of virtual machine config file paths in a particular vApp (for VM's in a folder use <code>Get-Folder</code> instead of <code>Get-vApp</code>)
<source lang="powershell"> Get-VM -Location (Get-vApp "vApp Name") | Get-View | %{$_.Config.Files.VmPathName} | Sort </source>
 
=== Get VM Power On/Off Events ===
Get Power On/Off times and events for VM
<source lang="powershell"> get-VM MyVM | Get-VIEvent | Where {$_.fullFormattedMessage -like "*Power*"} </source>
 
=== 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!
<source lang="powershell"> Get-VM MyVM | Move-VM -datastore (Get-datastore "DestinationDatastore") </source>   
<source lang="powershell"> Get-VM MyVM | Move-VM -datastore (Get-datastore "DestinationDatastore") </source>   
==== Move VMs to New PortGroup Network ====
The following will reconfigure all VM in the specified folder to move from network A to network B
<source lang="powershell"> Get-VM -Location (Get-Folder "InThisFolder") | Get-NetworkAdapter | Where {$_.NetworkName -eq "Network A"} | Set-NetworkAdapter -NetworkName "Network B" -Confirm:$false </source>
Borrowed in part from http://www.virtu-al.net/2009/10/19/powercli-mass-vm-portgroup-change/


=== Gracefully Shutdown a VM ===
=== Gracefully Shutdown a VM ===
Line 37: Line 59:
<source lang="powershell"> Shutdown-VMGuest -VM (Get-VM -name "vserver1") </source>   
<source lang="powershell"> Shutdown-VMGuest -VM (Get-VM -name "vserver1") </source>   


=== Get VM Power On/Off Events ===
=== Snapshots ===
Get Power On/Off times and events for VM
==== Get Snapshot info for a VM ====
<source lang="powershell"> get-VM MyVM | Get-VIEvent | Where {$_.fullFormattedMessage -like "*Power*"} </source>
Get Snapshot size, date created, and other info for a VM
<source lang="powershell"> Get-VM MyVM | Get-Snapshot | Select-Object -Property Name, Description, Created, SizeMB | Format-List * </source>
 
==== Start Snapshot on Multiple VMs ====
Assumes you've already got an array object of VMs containing the VM's you want to snapshot (eg <code>$VMs = Get-VM -Location (Get-Folder -Name "MyFolder")</code> for VM's in a particular folder, see [[#Get_list_of_VM.27s|Get_list_of_VM's]] for further info)
<source lang="powershell">New-Snapshot -Name "Pre change XYZ snapshot" -Description "Requested by TJ Hooker" -Memory -Quiesce -VM $vms </source>
 
==== 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)
<source lang="powershell">foreach ($vm in $vms) {Set-VM -VM $vm -Snapshot (Get-Snapshot -VM $vm -Name "Pre change XYZ snapshot") -Confirm:$false}</source>


=== Get Snapshot info for a VM ===
==== Delete/Merge Snapshot on Multiple VMs ====
Get Snapshot size, date created, and other info for a VM
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)
<source lang="powershell"> Get-VM MyVM | Get-Snapshot | Select-Object -Property Name, Description, Created, SizeMB | Format-List * </source>  
<source lang="powershell">foreach ($vm in $vms) {Remove-Snapshot -Snapshot (Get-Snapshot -VM $vm -Name "Pre change XYZ snapshot") -Confirm:$false}</source>


== ESX Host ==
== ESX Host ==
Line 126: Line 157:
<source lang="powershell"> Get-VMHost | Get-ScsiLun | Select VMHost,Vendor,Model,LunType,CapacityMB,RuntimeName,MultipathPolicy,CanonicalName | Export-Csv lunpath.csv </source>
<source lang="powershell"> Get-VMHost | Get-ScsiLun | Select VMHost,Vendor,Model,LunType,CapacityMB,RuntimeName,MultipathPolicy,CanonicalName | Export-Csv lunpath.csv </source>


[[Category:PowerCLI]]
=== Set LUN Multipathing ===
[[Category:Virtual Centre]]
Set all SCSI disks to use round robin multipathing
[[Category:Virtual Machine]]
<source lang="powershell"> Get-VMHost | Get-ScsiLun -LunType disk |  Set-ScsiLun -MultipathPolicy "roundrobin" </source>
[[Category:ESX]]
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...
* http://h20195.www2.hp.com/V2/GetDocument.aspx?docname=4AA1-2185ENW&cc=us&lc=en - HP's best practice
* http://www.yellow-bricks.com/2010/03/30/whats-the-point-of-setting-iops1/

Navigation menu