18 Apr 2015

Windows PowerShell and VMware PowerCLI One-Liners

I've decided to create this dedicated page where I'll place "one line scripts". I sometimes use these one line commands to run reports on vSphere or SCVMM inventories, if I'm not permitted or able to run full length scripts in an environment.

 

VMware PowerCLI

One-liner command 1: Outputs the VM Name, the Disk Name, VMDK Datastore and FileName and Capacity in GB to C:\Temp\VMVHDs.csv for all VMs associated with the PowerCLI-connected vSphere server:

$report = @(); $vms = get-vm; foreach($vm in $vms) {$vhds = Get-HardDisk $vm ; foreach ($vhd in $vhds){ $row = "" | select VMName, Name, FileName, CapacityGB; $row.VMName = $vhd.Parent; $row.Name = $vhd.Name; $row.FileName = $vhd.FileName; $row.CapacityGB = $vhd.CapacityGB; $report += $row;} } $report | export-csv C:\Temp\VMVHDs.csv -NoTypeInformation

Sample Output:

One-liner command 2: Outputs the VM Name, Number of CPUs, Amount of Memory and Cluster to C:\Temp\VMBasicInventory.csv for all VMs associated with the PowerCLI-connected vSphere server:

$report = @(); $vms = get-vm; foreach($vm in $vms) {$cluster = Get-Cluster -VM $vm; $row = "" | select VMName, NumCPU, MemoryGB, Cluster; $row.VMName = $vm.Name; $row.NumCPU = $vm.NumCPU; $row.MemoryGB = $vm.MemoryGB; $row.Cluster = $cluster; $report += $row;} $report | export-csv C:\Temp\VMBasicInventory.csv -NoTypeInformation

Sample Output:

One-liner command 3: Outputs the VM Name and .vmx file path to C:\Temp\VMxList.csv for all VMs associated with the PowerCLI-connected vSphere server:

$report = @(); $vms = Get-VM; foreach ($vm in $vms) {$row = "" | select VMName, VMXPath; $row.VMName = $vm.Name; $row.VMXPath = $vm.ExtensionData.Config.Files.VmPathName; $report += $row;} $report | export-csv C:\Temp\VMxList.csv -NoTypeInformation

Sample Output:

 

Windows PowerShell (SCVMM)

SCVMM One-liner command 1: Outputs the VMName, VM Configuration File path, VHDCount, VHD Location, VHDSize, MaxVHDSize, Cluster for all VMs managed by the SCVMM server connected by PowerShell

$report = @(); $vms = get-vm; foreach ($vm in $vms){foreach ($vhd in $vm.VirtualHardDisks){$maxvhdsize = [math]::Round($vhd.MaximumSize/1024/1024/1024,1); $vhdsize = [math]::Round($vhd.Size/1024/1024/1024,1); $row = "" | select VMName, VMCPath, VHDCount, VHDLocation, VHDSize, MaxVHDSize, VMCluster; $row.VMName = $vm.Name; $row.VMCPath = $vm.VMCPath; $row.VHDCount = $vm.VirtualHardDisks.Count; $row.VHDLocation = $vhd.Location; $row.VHDSize = $vhdSize; $row.MaxVHDSIze = $MaxVHDSize; $row.VMCluster = $vm.VMHost.HostCluster.Name; $report += $row;};};$report | export-csv C:\Temp\Hyper-V-VMs.csv -NoTypeInformation

 

Written by  0 comment
Last modified on Wednesday, 22 April 2015 13:39
Rate this item
(1 Vote)

Comments (0)

There are no comments posted here yet

Leave your comments

Posting comment as a guest. Sign up or login to your account.
0 Characters
Attachments (0 / 3)
Share Your Location

@simoneady I had to get out and push it past the sign
Follow Rynardt Spies on Twitter