VMware Cloud Community
thb_
Enthusiast
Enthusiast
Jump to solution

retrive multiple information for a vm

i need an overview of our vm's. Powershell and the VIToolkit is new for me. So i'll try to explain what i want to do.

with get-vm i want to collect name,cpunum and MemoryMB get-harddisk should get all harddisk space per vm.

That all should come up formated as table so that i can see

Name CpuNum MemoryMB Harddisk

vm1 2 1024 50GB

to get the values for one cmdlet no problem but to JOIN them is where i resignate.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can use the foreach (alias is %) statement for looping through several values.

First you get all the guests, then for each guests you get all the harddisks (cna be more than 1) and you store all the properties in a variable ($row).

Each $row is added to the array $report.

At the end you just display the complete content of the $report array

$report = @()
Get-VM | % {
    $vm = $_
	$vm | Get-HardDisk | % {
		$row = "" | select Name, CpuNum, MemoryMB, Harddisk
		$row.Name = $vm.Name
		$row.CpuNum = $vm.NumCpu
		$row.MemoryMB = $vm.MemoryMB
		$row.Harddisk = $_.CapacityKB
		$report += $row
	}
}
$report


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

You can use the foreach (alias is %) statement for looping through several values.

First you get all the guests, then for each guests you get all the harddisks (cna be more than 1) and you store all the properties in a variable ($row).

Each $row is added to the array $report.

At the end you just display the complete content of the $report array

$report = @()
Get-VM | % {
    $vm = $_
	$vm | Get-HardDisk | % {
		$row = "" | select Name, CpuNum, MemoryMB, Harddisk
		$row.Name = $vm.Name
		$row.CpuNum = $vm.NumCpu
		$row.MemoryMB = $vm.MemoryMB
		$row.Harddisk = $_.CapacityKB
		$report += $row
	}
}
$report


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
thb_
Enthusiast
Enthusiast
Jump to solution

wow, that was fast!

and if i would like to add another query like Get-Stat -Stat mem.overhead.average

by the way is there a book you would recommend for learning powershell?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It depends what you want to combine in a report.

In the previous code we had 1 row per harddisk per guest.

Now you could add the statistical value to each row but that is not really logical (in my opinion).

I would go for 2 separate reports for this.

In the end it is a matter of deciding what is to be combined in a row in report and if it makes sense.

Another remark, there were some issues with the Get-Stat cmdlet in VITK 1.

Several of these have been solved in VITK v1.5.So it's advisable to go for VITK 1.5 if you want to include statistics in your report.

In any case this is a sample with the memory overhead included.

$report = @()
Get-VM | % {
    $vm = $_
	$vm | Get-HardDisk | % {
		$row = "" | select Name, CpuNum, MemoryMB, Harddisk, MemOverheadAvg
		$row.Name = $vm.Name
		$row.CpuNum = $vm.NumCpu
		$row.MemoryMB = $vm.MemoryMB
		$row.Harddisk = $_.CapacityKB
		$row.MemOverheadAvg = ($vm | Get-Stat -Stat mem.overhead.average -Realtime -MaxSamples 1).Value
		$report += $row
	}
}
$report | ft


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

LucD
Leadership
Leadership
Jump to solution

For your book question, have a look at my reply in .

When you do a find on "book" in this Community you will find other references to PowerShell books.

And for a book on PowerShell and the VITK you will have to wait for Hal's soon-to-be-released masterpiece.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
packetboy
Contributor
Contributor
Jump to solution

Hi thb,

I would also recommend checking out powergui it is a free tool from quest.. you use "powerpacks" which are like GUI front ends for powershell objects. You also get a nice powershell script editor which helped me alot trying to learn how to write scripts.

DL powergui from here :

Then get the latest VMware powerpack from here :

This will easily get you the report you want and you can export it right from powergui... there is also a powershell code tab so you can see whats happening under the covers.

Hope this helps

Packetboy.

0 Kudos
Mllii
Enthusiast
Enthusiast
Jump to solution

any way to get this to show the free space on the VMs disk?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

To get the free disk space is not as straight-forward as it sounds.

It depends on several conditions:

  • is the guest powered on ?

  • are the VMware tools installed on the guest

  • for a Linux guest the free space reported is for the /boot volume, not for the / volume

The best (and most complete) implementation that I know of is done by Hugo Peeters.

See his blog entry Get VMware Disk Usage with Powershell for the script.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos