Thursday, September 8, 2016

Misc PowerCLI One-liners used in Day to Day operations

Here are some OneLiner commands which we can use to perform our day to day operations dealing with Virtual Machines hosted on VMWare platform.

Please note that the commands mentioned below takes input from a text file called VMList.txt which should contain list of VMs and based on the requirement it writes the output to a .CSV file. If you want the output to be displayed on screen/console then just remove "| Export-Csv -Notypeinformation OutputFileName.csv" from the very end of every command below:


#Get list of VMs and the ESX cluster names on which the VMs are hosted:
Get-Content VMList.txt | foreach {$Vmname = $_; get-vm $Vmname | get-cluster |select @{n='VMName';e={"$VMName"}},name} | Export-Csv -Notypeinformation VM-Clusters.csv

#Get list of VMs and attached Harddisks including RDM and Virtual Disks on VMs:

Get-Content VMList.txt | foreach {$VMName = $_; get-vm $VMName |Get-HardDisk |select @{n='VMName';e={"$VMName"}}, DiskType, CapacityGB, Name} | Export-Csv -Notypeinformation VM-DiskInforamtion.csv

#Get list of VMs, ClusterNames and attached Harddisks including Disk Type, CapacityGB RDM etc on VMs:
Get-Content VMList.txt | foreach {$VMName = $_; $Cluster= (get-vm $VMName |get-cluster).name; get-vm $VMName |Get-HardDisk |select @{n='VMName';e={"$VMName"}}, @{n='ClusterName';e={"$Cluster"}}, DiskType, CapacityGB, Name } | Export-Csv -Notypeinformation VM-Details.csv

#Get RDM Details from a VM along with .Naa, .VML, DiskType,Capacity etc..
get-content VMList.txt | foreach {get-vm $_ |get-harddisk | where "disktype" -Match "RAW" |select Parent, filename, Name, DeviceName, ScsiCanonicalName, DiskType, Persistence, capacityGB}| Export-Csv -Notypeinformation VM-RDMInfo.csv

#Get RDM Details from a VM along with VMname, DiskName, .Naa, DiskType.. (On Screen)
get-content VMList.txt | foreach {get-vm $_ |get-harddisk | where "disktype" -Match "RAW" |select Parent, Name, ScsiCanonicalName, DiskType} |ft -AutoSize

#Get or list Snapshots on VMs listed in Servers.txt:
gc .\Servers.txt |foreach {$VMname = $_; Get-VM $VMName |Get-Snapshot |select @{n="VM";e={$VMname}},Name,PowerState,Description}
#Take Snapshots of VMs listed in a tile named Servers.txt :
gc .\Servers.txt |foreach {$VMname = $_; Get-VM $VMName |New-Snapshot -Memory -Quiesce -Name "Initial" -Verbose}






.................Stay Tuned on this page for more useful commands.


No comments:

Post a Comment