VMware Cloud Community
DZ1
Hot Shot
Hot Shot
Jump to solution

Trouble outputting the results

I have some code below and I am having an issue outputting the results, here is the code:

Function PercentFree {
param($ds)
[Math]::Round(($ds.FreeSpaceMB / $ds.CapacityMB * 100),0)
}
$datastores = Get-Datacenter 'DataCenter' | Get-Datastore
$vDataStore = @()
$AllReport = @()
Foreach ($datastore in $datastores) {
$vDataStore = "" | Select Datastore, PercentageFree
$vDataStore.Datastore = $datastore.Name
$vDataStore.PercentageFree = PercentFree $datastore
#$AllReport += $vDataStore
if ($vDataStore.PercentageFree -lt 10) { write $vDataStore.Datastore $("$vDataStore.PercentageFree" + "Percent Free"), "`r" }
}
write "---------------------"
The last line (if statement) is giving me issues, I am trying to have the datastore name on 1 line, right under that line I want the actual number for the percentage amount, and on that same line I want the text "Percent Free".  I'm aware that write-host is more...I guess liberal with the text, but this is gong to be in a script report, and I thought write-host will output right on the screen?
I have been playing around with the code, so I know some things look odd, but the only issue I have is outputting the results.  I searched online and on this site for an answer, but I may not have entered the correct search term, so sorry if the answer has been posted before. 
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try {0:N0}

The P format is to display values between 0 and 1 as a percentage.

That's why it displays the value 8 as 800%


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

I suspect a newline could do that.

Is this producing what you want ?

write $vDataStore.Datastore + "`n" + $("$vDataStore.PercentageFree" + "Percent Free"), "`r" 


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

DZ1
Hot Shot
Hot Shot
Jump to solution

Thanks, I tried it that way, but I received output like this (results truncated):

eva1-vdisk01
+
+
@{Datastore=eva1-vdisk01; PercentageFree=6}.PercentageFreePercent Free
eva1-vdisk02
+
+
@{Datastore=eva1-vdisk02; PercentageFree=8}.PercentageFreePercent Free

I was playing with using "+" before, but I could not get the results that I wanted, I want something similar to:

DatastoreName

20 Percent Free

If there is a better way, then I'm all for it. 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try this

function PercentFree {
  param($ds)
  [Math]::Round(($ds.FreeSpaceMB / $ds.CapacityMB),2)
}
$datastores = Get-Datacenter 'DataCenter | Get-Datastore 
$vDataStore
= @() $AllReport = @() foreach ($datastore in $datastores) {   $vDataStore = "" | Select Datastore, PercentageFree
 
$vDataStore.Datastore = $datastore.Name
  $vDataStore.PercentageFree = PercentFree $datastore
  #$AllReport += $vDataStore   if ($vDataStore.PercentageFree -lt 50) {     write $vDataStore.Datastore ("{0:p2} Percent Free"-f $vDataStore.PercentageFree) "`r"  } } write "---------------------"

With the format operator it is easier to convert to string

Note there is no reason to multiply by 100 when you use the 'percent' format.


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

DZ1
Hot Shot
Hot Shot
Jump to solution

You are the man, I only have 1 small issue.  The .NET number formatting is adding extra digits, so if it's "8", it shows "800.0"  I tried playing with the numbers, but I'm making it worse. 

Thank you so much, it's almost as if your hovering about the earth, just waiting to answer a powershell question. 

0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

I just changed it, I tried {0:N}, and that seems to give me this - if it's "8", it shows "8.00", just trying to get it to drop that decimal 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try {0:N0}

The P format is to display values between 0 and 1 as a percentage.

That's why it displays the value 8 as 800%


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

0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

Ahh, I should hit myself, I didn't even try the zero after the "N" before.  Thank you so much, you are always a great help.  I have a very ong way to go.

0 Kudos