Custom Inventory rule with Powershell Script
Anyone know how to format ShellCommandNumberReturn() for series of Powershell commands? I am trying to get a single custom inventory item to detect the % of usable battery capacity on my laptops. We have identified an issue where some laptops batteries are swelling as they start to fail. We figure calculating the usable battery capacity against the ship date of the unit will give us an indication of units that pose a hazard.
Here are the lines:
$DesignedCapacity = (Get-WmiObject -Class "BatteryStaticData" -Namespace "ROOT\WMI").DesignedCapacity
$FullChargedCapacity = (Get-WmiObject -Class "BatteryFullChargedCapacity" -Namespace "ROOT\WMI").FullChargedCapacity
$BatteryLife = [math]::round(100*$FullChargedCapacity/$DesignedCapacity,2)
Write-Host $BatteryLife
I believe I could create a script to run this regularly and push the output to a text file, the capture that in a custom inventory rule, but I would like to keep it in one command if possible.
Answers (1)
You could simplify it, it's probably the variables causing the issue but see mine below which works and returns the same value as your code does.
ShellCommandTextReturn(cmd /q /c powershell.exe -command "[math]::round(100*(Get-WmiObject -Class "BatteryFullChargedCapacity" -Namespace "ROOT\WMI").FullChargedCapacity/(Get-WmiObject -Class "BatteryStaticData" -Namespace "ROOT\WMI").DesignedCapacity,2)")
It returns
Hope this helps.
Comments:
-
Hi Ziggi,
The . (period) did not seem to work, but the ; (semicolon) did. - JordanNolan 5 years ago