Friday 15 July 2011

vmware - Round a number in PowerShell one-liner -



vmware - Round a number in PowerShell one-liner -

i'm using vmware's powercli run command output inventory of sorts vcenter.

get-vm | select-object name,memorygb,numcpu,usedspacegb,@{n="totalhdsizegb"; e={(get-harddisk -vm $_ |measure-object -sum capacitygb).sum}},@n="network"; e={(get-networkadapter -vm $_ |select -unique -expand networkname)}}sort-object network|export-csv -path vms.csv

i'd round usedspacegb , list of networkname values, not one. i've seen how utilize [math]::round() round numbers in scripts, haven't been able find illustration on command line , attempts have failed accomplish desired results. how do that?

try using next instead of usedspacegb:

@{ n="spaceusedgb"; e={[math]::round( $_.usedspacegb, 3 )}}

that'll round usedspacegb 3 decimal places , give field name spaceusedgb. round nearest whole number, either alter 3 0, or use:

@{ n="spaceusedgb"; e={[math]::round( $_.usedspacegb )}}

if don't want homecoming networknames array, sorted string, alter look like:

@{n="network"; e={(get-networkadapter -vm $_ | sort-object networkname | select -unique -expand networkname) -join ', '}}

because totalhdsizegb had bunch of decimal places in cases well, final version of command looks like:

get-vm | select-object name, memorygb, numcpu, @{ n="diskusedgb"; e={[math]::round( $_.usedspacegb )}}, @{ n="totalhdsizegb"; e={[math]::round((get-harddisk -vm $_ | measure-object -sum capacitygb).sum)}}, @{n="network"; e={(get-networkadapter -vm $_ | sort-object networkname | select -unique -expand networkname) -join ', '}} | export-csv -path vms.csv

powershell vmware rounding powercli vcenter

No comments:

Post a Comment