Question

I have the following script written in Powershell that looks for snapshots on servers and reports an email based on its findings:

Connect-ViServer server
$body = Get-Folder -name vm | Get-VM | Get-Snapshot | Select Name, VM, SizeMb, Created | Out-String 
send-mailmessage -From "myemail.com" -To "myemail.com" -SmtpServer "myserver.com" -Body $body -Subject "Snapshot Report"

The output that I get from this looks like this:

Name                VM                       SizeMB Created            
----                --                       ------ -------            
snapshot name       server       ...3507232666015625 11/6/2013 11:53...
snapshot name       server       ...4918060302734375 11/6/2013 10:30...
snapshot name       server...    ...0483551025390625 11/6/2013 10:32:...
snapshot name       server       ...2000579833984375 11/6/2013 3:58:34 PM

I figure there has to be a way to format the output to where it will accurately show the size in megabytes with a max of just 2 decimals and display the entire time created. My problem I know is somewhere here:

| Select Name, VM, SizeMb, Created | out-string

Does anyone know how to format output from Select in order to specify just a max of 2 digit decimals?

Was it helpful?

Solution

Use "{0:N2} -f" before the size. I think you can do something like that:

| Select Name, VM, @{n="Size";e{"{0:N2}" -f $_.SizeMb}}, Created | out-string

More infos for number formatting in Powershell here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top