VMware Cloud Community
lElOUCHE_79
Enthusiast
Enthusiast
Jump to solution

Show data in Percent

I'm trying to improve my PowerCLI skills, so I'm beginning to do a few basic scripts.
The script below lets you retrieve CPU and memory metrics over one week.
My concern is that the numbers returned have multiple numbers after the comma.
Who can assist me in rounding these numbers?

 

$AllHosts = @()
Get-VM * | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Name -notmatch "vCLS"} | ForEach-Object {
    $hoststat = "" | Select-Object VMHost, HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
    $hoststat.VMHost =$_.VMHost.Name.Split('.')[0]
    $hoststat.HostName = $_.Name
    $statcpu = Get-Stat -Entity $_ -Start (Get-Date).AddDays(-7) -Finish (Get-Date)-MaxSamples 100 -Stat cpu.usage.average
    $statmem = Get-Stat -Entity $_ -Start (Get-Date).AddDays(-7) -Finish (Get-Date)-MaxSamples 100 -Stat mem.usage.average
    $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
    $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum
    $hoststat.CPUMax = $cpu.Maximum
    $hoststat.CPUMin = $cpu.Minimum
    $hoststat.CPUAvg = $cpu.Average
    $hoststat.MemMax = $mem.Maximum
    $hoststat.MemMin = $mem.Minimum
    $hoststat.MemAvg = $mem.Average
    $AllHosts += $hoststat
}
$AllHosts | Select-Object VMHost, HostName, MemMax_"%", MemMin_"%", MemAvg_"%", CPUMax_"%", CPUMin_"%", CPUAvg_"%" | Export-Csv .\CPURAMHost.csv -NoTypeInformation
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Use the [Math]::Round() method.

Not sure what the idea with the "%' is on the Select, but if you want to have a percent sign in the column header use a calculated property and change the N(ame) entry in the has table.

 

$AllHosts = @()
Get-VM * | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Name -notmatch "vCLS"} | ForEach-Object {
    $hoststat = "" | Select-Object VMHost, HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
    $hoststat.VMHost =$_.VMHost.Name.Split('.')[0]
    $hoststat.HostName = $_.Name
    $statcpu = Get-Stat -Entity $_ -Start (Get-Date).AddDays(-7) -Finish (Get-Date)-MaxSamples 100 -Stat cpu.usage.average
    $statmem = Get-Stat -Entity $_ -Start (Get-Date).AddDays(-7) -Finish (Get-Date)-MaxSamples 100 -Stat mem.usage.average
    $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
    $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum
    $hoststat.CPUMax = [math]::Round($cpu.Maximum,0)
    $hoststat.CPUMin = [math]::Round($cpu.Minimum,0)
    $hoststat.CPUAvg = [math]::Round($cpu.Average,0)
    $hoststat.MemMax = [math]::Round($mem.Maximum,0)
    $hoststat.MemMin = [math]::Round($mem.Minimum,0)
    $hoststat.MemAvg = [math]::Round($mem.Average,0)
    $AllHosts += $hoststat
}
$AllHosts | Select-Object VMHost, HostName, 
   @{N='MemMax%';E={$_.MemMax}}, 
   @{N='MemMin%';E={$_.MemMin}}, 
   @{N='MemAvg%';E={$_.MemAvg}}, 
   @{N='CPUMax%';E={$_.CPUMax}}, 
   @{N='CPUMin%';E={$_.CPUMin}}, 
   @{N='CPUAvg%';E={$_.CPUAvg}} |
Export-Csv .\CPURAMHost.csv -NoTypeInformation

 




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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

Use the [Math]::Round() method.

Not sure what the idea with the "%' is on the Select, but if you want to have a percent sign in the column header use a calculated property and change the N(ame) entry in the has table.

 

$AllHosts = @()
Get-VM * | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.Name -notmatch "vCLS"} | ForEach-Object {
    $hoststat = "" | Select-Object VMHost, HostName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
    $hoststat.VMHost =$_.VMHost.Name.Split('.')[0]
    $hoststat.HostName = $_.Name
    $statcpu = Get-Stat -Entity $_ -Start (Get-Date).AddDays(-7) -Finish (Get-Date)-MaxSamples 100 -Stat cpu.usage.average
    $statmem = Get-Stat -Entity $_ -Start (Get-Date).AddDays(-7) -Finish (Get-Date)-MaxSamples 100 -Stat mem.usage.average
    $cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
    $mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum
    $hoststat.CPUMax = [math]::Round($cpu.Maximum,0)
    $hoststat.CPUMin = [math]::Round($cpu.Minimum,0)
    $hoststat.CPUAvg = [math]::Round($cpu.Average,0)
    $hoststat.MemMax = [math]::Round($mem.Maximum,0)
    $hoststat.MemMin = [math]::Round($mem.Minimum,0)
    $hoststat.MemAvg = [math]::Round($mem.Average,0)
    $AllHosts += $hoststat
}
$AllHosts | Select-Object VMHost, HostName, 
   @{N='MemMax%';E={$_.MemMax}}, 
   @{N='MemMin%';E={$_.MemMin}}, 
   @{N='MemAvg%';E={$_.MemAvg}}, 
   @{N='CPUMax%';E={$_.CPUMax}}, 
   @{N='CPUMin%';E={$_.CPUMin}}, 
   @{N='CPUAvg%';E={$_.CPUAvg}} |
Export-Csv .\CPURAMHost.csv -NoTypeInformation

 




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

lElOUCHE_79
Enthusiast
Enthusiast
Jump to solution

@LucD correct the idea behin '%' is that to show  a percent sign in the column header :slightly_smiling_face:

Reply
0 Kudos