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

Jump to navigation Jump to search
→‎Virtual Machine: Re-orged and added mass snapshot start-stops
(→‎Virtual Machine: Added "Get VM's VMX Path")
(→‎Virtual Machine: Re-orged and added mass snapshot start-stops)
Line 20: Line 20:
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 31: Line 35:
=== Get VM's VMX Path ===
=== Get VM's VMX Path ===
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>)
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>  
<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>  


=== Storage vMotion ===
=== Migration ===
Storage vMotion (SvMotion) a VM to a different datastore
==== 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 41: Line 55:
<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 info for a VM ===
Get Snapshot size, date created, and other info for a VM
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>
<source lang="powershell"> Get-VM MyVM | Get-Snapshot | Select-Object -Property Name, Description, Created, SizeMB | Format-List * </source>


=== Move VMs to New PortGroup Network ===
==== Start Snapshot on Multiple VMs ====
The following will reconfigure all VM in the specified folder to move from network A to network B
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.27s]] for further info)
<source lang="powershell"> Get-VM -Location (Get-Folder "InThisFolder") | Get-NetworkAdapter | Where {$_.NetworkName -eq "Network A"} | Set-NetworkAdapter -NetworkName "Network B" -Confirm:$false </source>
<source lang="powershell">New-Snapshot -Name "Pre change XYZ snapshot" -Description "Requested by TJ Hooker" -Memory -Quiesce -VM $vms </source>
Borrowed in part from http://www.virtu-al.net/2009/10/19/powercli-mass-vm-portgroup-change/
 
==== 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>
 
==== 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)
<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 ==

Navigation menu