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 "---------------------"
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
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
Thanks, I tried it that way, but I received output like this (results truncated):
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.
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
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.
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
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
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.