Powercli Basics Part 2

Welcome to part 2 we are going to expand on our last code slice:

get-vm | select Name, CpuUsageMhz, MemoryUsageGB

Which produces a nice list of the CpuUsageMhz and MemoryUsageGB per node.   Now we are going to expand that command to produce a report that displays the percent of Memory used and Cpu used across your whole cluster.   First of all we need variables

Variables

Variables in powershell are very flexable.  They are all defined with a dollar $ sign in front of them.  An variable can be an array via $variable_name[number] or a multidementional  array via $variable_name[number].field_name for example

To create variable that holds the number 5 we do the following

$variable=5

To display we do the following:

$variable

In order to load multiple items into an variable making it an array we just add more elements on the command line separated by a comma

$variable=5,6
$variable

To address individual elements of the array you use [number]

$variable[0]

With Vmware you almost always have multiple fields as show by the following command from part 1.

get-vmhost | fl

So when you put the output from get-vmhost into a variable it creates a multidemential array which can be addressed like this:

$output=get-vmhost
$output

So we can choose a single field via the following pattern $variablename[num].fieldname for example

$output[0].NumCpu

What do we do if we have multiple hosts that we want to query .. lets use a for loop:

[sourcecode language="powershell"]
$hosts=get-vmhost

foreach($vmhost in $hosts){

  $vmhost.Name

  $vmhost.CpuTotalMhz

  $vmhost.CpuUsageMhz

  $vmhost.MemoryTotalGB

  $vmhost.MemoryUsageGB

}

[/sourcecode]

Pretty simple stuff… but we are missing a critical part… I think it would be better to understand the totals from my whole cluster not each individual host.  For that we need some math operators:

= Equals

+= Increases by value and appends variable

-= Decreases by value and appends to variable

*= Multiplies by value and appends to variable

/= Divides by value and appends to variable

%= Divides and assigns the remainder to variable (modulus)

So back to our code… lets assume we want to add up all our used CPU and used Ram and figure out what percentage we are using on our whole cluster:

 

[sourcecode language="powershell"]
$hosts=get-vmhost

foreach($vmhost in $hosts){

  $cputotal+=$vmhost.CpuTotalMhz

  $cpuusage+=$vmhost.CpuUsageMhz

  $memorytotal+=$vmhost.MemoryTotalGB

  $memoryused+=$vmhost.MemoryUsageGB

}

 $percpuused=(($cpuusage / $cputotal) * 100)

 Write-Host "You are currently using $percpuused% of your cpu."

 $permemused=(($memoryused / $memorytotal) * 100)

 Write-Host "You are currently using $permemused% of your memory."
[/sourcecode]

One problem with this output… no one cares how many decimal points of a percent you are using. We need to edit the numbers to drop the decimal points:

[sourcecode language="powershell"]
$hosts=get-vmhost

foreach($vmhost in $hosts){

  $cputotal+=$vmhost.CpuTotalMhz

  $cpuusage+=$vmhost.CpuUsageMhz

  $memorytotal+=$vmhost.MemoryTotalGB

  $memoryused+=$vmhost.MemoryUsageGB

}

 $percpuused="{0:N0}" -f (($cpuusage / $cputotal) * 100)

 Write-Host "You are currently using $percpuused% of your cpu."

 $permemused="{0:N0}" -f (($memoryused / $memorytotal) * 100)

 Write-Host "You are currently using $permemused% of your memory."
[/sourcecode]

The addition of “{0:N0}” -f knocks off the decimal points .. you can also keep to places after the decimal point by this command:
“{0:N2}” -f

That’s it for now. In part three I will show you how to make your report a nice HTML email.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.