Powershell Decimal D command or manual maths
-
07-08-2022 - |
Question
Powershell script .ps1 Excel
Issue: It's in the script
#CALCULATE FREE MEMORY AS PERCENTAGE
#DECLARE VARIABLES
#MY CURRENT FREE SERVER MEM
$currentMem = ([math]::round($freemem.FreePhysicalMemory / 1024, 2))
#MY SERVER MEM LIMIT
$maxMemLimit = 16000
#MY FINAL FREE MEM VALUE PERCENTAGE VARIABLE HOLDER
$freeMemPerc = 0
#MY WORKING OUT
$freeMemPerc = ($maxMemLimit / $currentMem)
#I CAN ADD THIS LINE BELOW OR LEAVE IT OUT IF I USE THE NUMBER FORMAT TYPES (DECIMAL)
#CURRENTLY IT'S COMMENTED
#$freeMemPerc = 100 * $freeMemPerc
#ADD TO SPREADSHEET
#USING THE DECIMAL D CMD TO FORMAT THE NUMBER
"Free Memory (% of MB total): {0:D}" -f $freeMemPerc
$Sheet.Cells.Item($intRow,1) = "Free Memory (% of MB total):"
$Sheet.Cells.Item($intRow,2) = "$freeMemPerc %"
$Sheet.Cells.Item($intRow,3).Interior.ColorIndex = 20
$Sheet.Cells.Item($intRow,4).Interior.ColorIndex = 20
$Sheet.Cells.Item($intRow,5).Interior.ColorIndex = 20
Scenario: I am trying to turn server values of MB and GB into percentages based on server limit. Let's say my current server usage is: 2617.89mb, I should get the answer in my spreadsheet as: 16.3618125
Based on math: 2617.89 / 16000 * 100.
Current result I receive is 6.11. I have tried adding other decimals or decreasing decimals to D too but still don't get my answer.
I think my programming is wrong here not my maths, although in reality my maths is terrible.
Any ideas?
Source: http://technet.microsoft.com/en-us/library/ee692795.aspx
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow