Product: PowerShell Universal
Version: 1.5.13
I have a function in a module that returns the used and available RAM for any server on the domain. I pass the hostname and an object is returned with two fields. AvailableRAM and UsedRAM. For an unknown reason, when I use this function in an API endpoint in Powershell Universal, the output is not at all what I expect. The number 100 is under Used RAM. I am unable to explain why the output is modified / invalid when it is passed through the API. The output and data types are exactly as expected when running the function from the server. I have created a video detailing this unusual situation in case I’m missing a vital detail in the description. (148) Incorrect data coming back from API in PowershellUniversal - YouTube
Here’s the code.
From the module:
Function Get-RAM {
param
(
[Parameter(Position = 0, Mandatory = $true, HelpMessage = “Provide a server name”)]
[string] $Server
)
[string]strComputer = $Server + “.ad.dcu.ie”
$a = 0
$b = 0
$a = Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer | fl freePhysical | Out-String
$b = Get-WmiObject Win32_OperatingSystem -ComputerName $strComputer | fl totalvisiblememory | Out-String
$a = $a -replace ‘\D+(\d+)’, ‘$1’
$b = $b -replace ‘\D+(\d+)’, ‘$1’
$FreeRam = [math]::Round($a / $b * 10000) / 100
$UsedRam = 100 - $FreeRam
$RAMSummary = New-Object -TypeName PSObject
$RAMSummary | Add-Member -Type NoteProperty -Name AvailableRam -Value $FreeRam $RAMSummary | Add-Member -Type NoteProperty
-Name UsedRam -Value $UsedRam
Return $RAMSummary
}
#API code:
$PostData = $Body | ConvertFrom-Json
import-module "C:\Program Files\WindowsPowerShell\Modules\CustomModule\CustomModule.psd1"
if ($PostData.ServerName -ne $null) {
$CommandResult = Get-RAM -Server $PostData.ServerName
}
else {
$CommandResult = 'Server name was empty'
}
$CommandResult | ConvertTo-Json
Please take a look at the video for a more indepth description. (148) Incorrect data coming back from API in PowershellUniversal - YouTube
Thanks. Your suggestions are very appreciated.