VMware Cloud Community
dmartushev
Contributor
Contributor
Jump to solution

VM Guest Usage Audit

Wondering if someone could give me a hand with an easy one (have searched around for other posts but no joy).

I've been playing around with the VI Toolkit trying to export some simple pieces of information to a CSV file. I'm looking to get a list of all VM Guests including what Cluster they are a part of, disk.usage.average Maximum for the last 30 days, and the memory usage average Maximum value for the last 30 days.

To keep it simple (only just started playing with Powershell) I've started with a modular approach just trying to output a CSV with VM Guest name and disk.usage.average maximum value for the last 30 days and have come up with the following:

<br /><br/>
$vmguest = Get-VM server_1<br/><br/>
$vmguestMAX = Get-Stat -Entity ($vmguest) -stat disk.usage.average -Start ([DateTime]::Now.AddDays(-1)) -Finish ([DateTime]::Now) `<br/><br/>
|Measure-object -property Value -max `<br/><br/>
|Select-Object Maximum 


So I've found that the resulting data is indeed what I seek but I'm not up to speed enough with PS to figure out how to output the results to a CSV file.

Ultimately I would like to get a full VM Guest usage audit out to a CSV file as described above so any help to get towards this would would be brilliant.

Thanks for any assistance,

dma

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

My previous code had indeed the first line missing (i.e the init of the hash array).

The easiest solution is to go for a simple array and export that to a CSV

Something like this:

$stats = @()
Get-VM | % {
    $row = "" | Select VMname, DiskUsgAvg
	$row.VMname = $_.Name
	$row.DiskUsgAvg = (Get-Stat -Entity ($_) -stat disk.usage.average -Start (Get-Date).AddDays(-30) -Finish (Get-Date) -IntervalMins 30  | `
	       Measure-Object -property Value -maximum).Maximum
    $stats += $row
} 
$stats | Export-Csv "c:\diskstats.csv" -NoTypeInformation

I don't understand why the maximum value won't correspond with what you see in the VIC for clients that have no 30 days of statistical data.

But you can leave out the -IntervalMins parameter if you want.


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

You can try the following script:

Get-VM | % {
	$stats.Add($_.Name,`
	   ((Get-Stat -Entity ($_) -stat disk.usage.average -Start (Get-Date).AddDays(-30) -Finish (Get-Date) -IntervalMins 30  | `
	       Measure-Object -property Value -maximum).Maximum))
} 
$stats | Export-Csv "c:\diskstats.csv" -NoTypeInformation

Note1: I used the Get-VM cmdlet, that way you will have a CSV file with all your guests

Note2: I use an associative array ($stats) to collect the guest's name and the maximum disk usage

Note3: I used the Get-Date cmdlet instead of the Now expression

Note4: I added the -InteralMins parameter to the Get-Stat cmdlet


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

dmartushev
Contributor
Contributor
Jump to solution

Hi LucD,

Thank you for the code you have provided so far. I've had a play with it and can't seem to get past this error:

<br /><br/>
You cannot call a method on a null-valued expression.<br/><br/>
At :line:4 char:12<br/><br/>
+ $stats.Add( &lt;&lt;&lt;&lt; $_.Name, ` 


I've had a dig around and can't find what it is I am missing here, I'm guessing it's something simple. Appreciate any additional information.

0 Kudos
dmartushev
Contributor
Contributor
Jump to solution

OK, so I dug around some more and had a play with the script and with some assistance from a friend came up with the following that seems to work pretty well:

<br /><br/>
$stats=@{}<br/><br/>
Get-VM | % {<br/><br/>
$stats.Add($_.Name,((Get-Stat -Entity ($_) -stat disk.usage.average -Start (Get-Date).AddDays(-30) -Finish (Get-Date) | Measure-Object -property Value -maximum).Maximum)) <br/><br/>
} <br/><br/>
$stats | Out-File "c:\disk_stats.txt" 





I get a result that isn't comma delimited, however I can get Excel to cope with a space delimited result. So this definately gets the job done for the disk.usage.average stat.

I removed the IntervalMins parameter as some of our VM Guests don't have stats going back 30 days and so the Max value that was returned didn't match the same value in the VC 30 day performance view.

Any thoughts on the above code? Is there a more efficient way of producing the same output in CSV format?

Also, is there an easy way of capturing the memory.usage.average maximum stat as well in the same call?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

My previous code had indeed the first line missing (i.e the init of the hash array).

The easiest solution is to go for a simple array and export that to a CSV

Something like this:

$stats = @()
Get-VM | % {
    $row = "" | Select VMname, DiskUsgAvg
	$row.VMname = $_.Name
	$row.DiskUsgAvg = (Get-Stat -Entity ($_) -stat disk.usage.average -Start (Get-Date).AddDays(-30) -Finish (Get-Date) -IntervalMins 30  | `
	       Measure-Object -property Value -maximum).Maximum
    $stats += $row
} 
$stats | Export-Csv "c:\diskstats.csv" -NoTypeInformation

I don't understand why the maximum value won't correspond with what you see in the VIC for clients that have no 30 days of statistical data.

But you can leave out the -IntervalMins parameter if you want.


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

0 Kudos
dmartushev
Contributor
Contributor
Jump to solution

Excellent! BIG thanks for the assistance here as I have used the code you provided and it works a treat.

I made an addition to the code as we need the memory usage average as well and removed the -IntervalMins parameter. Here is the final result that works:

$stats = @()
Get-VM | % {
    $row = "" | Select VMname, DiskUsgAvgPeak, MemUsgAvgPeak
	$row.VMname = $_.Name
	$row.DiskUsgAvgPeak = (Get-Stat -Entity ($_) -stat disk.usage.average -Start (Get-Date).AddDays(-30) -Finish (Get-Date) | `
	       Measure-Object -property Value -maximum).Maximum
	$row.MemUsgAvgPeak = (Get-Stat -Entity ($_) -stat mem.usage.average -Start (Get-Date).AddDays(-30) -Finish (Get-Date) | `
	       Measure-Object -property Value -maximum).Maximum
    $stats += $row
}
$stats | Export-Csv "c:\diskstats.csv" -NoTypeInformation

Again BIG THANKS for your help LucD as it is much appreciated.

0 Kudos