VMware Cloud Community
ConradBotha1982
Contributor
Contributor

Rounding Numbers in PowerCLI

Hi All

Very basic question please.

I am new to PowerCLI and learning by playing around pretty much.

I've put a basic script together to report on VM's with snapshots.

The script below works fine but I need to round the numbers under the Size GB col.

Could any please assist with a one liner and perhaps a bit of an explanation.

$list = get-VM
foreach ( $computer in $list ) {
$cs = Get-Snapshot -VM $computer
$cs | Select-Object VM, Created, SizeGB, Description
}
0 Kudos
5 Replies
LucD
Leadership
Leadership

Sure.

- one of the strong points of PowerShell is that it can use a pipeline. This allows passing objects from 1 cmdlet to the next cmdlet.

- one a Select-Object you can what is called a 'calculated expression'. It consists of a Name ('N') and an EXpression ('E')

- PowerShell is built on .NET, so you can use the .NET functions and methods. The [Math] one has a Round method

Get-VM |

Get-Snapshot |

Select-Object @{N='VM';E={$_.VM.Name}}, Created,

   @{N='SizeGB';E={[math]::Round($_.SizeGB,1)}},Description


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

0 Kudos
ConradBotha1982
Contributor
Contributor

Thanks you very Much!
0 Kudos
ConradBotha1982
Contributor
Contributor

Hi All

I have the following piece of code:

Get-VM | Get-Snapshot |

Select-Object @{N='VM';E={$_.VM.Name}},

@{N='Description';E={$_.Description}},

@{N='Created';E={$_.Created.ToString("dd/MM/yyyy")}},

@{N='Age';E={( Get-Date ) - $_.Created}},

@{N='SizeGB';E={[math]::Round($_.SizeGB,1)}}

I would like to only display the differance in days under the age col.

Currently it outputs days:hrs:min:sec:ticks

0 Kudos
LucD
Leadership
Leadership

Change that line to

@{N = 'Age'; E = { (( Get-Date ) - $_.Created).Days}},


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

0 Kudos
ConradBotha1982
Contributor
Contributor

Thank you once again Sir!
0 Kudos