Sorting with strings/returning different data types

I have some relevance to get the battery health from laptops:

if ((value of setting "_BESClient_ComputerType" of client) is "Laptop") then ((integer value of selects ("FullChargedCapacity from BatteryFullChargedCapacity") of wmi "root/wmi")/(integer value of selects ("DesignedCapacity from BatteryStaticData") of wmi "root/wmi") as floating point * ("100.0" as floating point)) as string else "Not a laptop!"

I’d like non-laptops to return a helpful message, like “Not a laptop” or “No battery” etc. Unfortunately, if I return a string for the non-applicable cases, I need to return the health percentage as a string as well, and that means sorting gets messed up, e.g.:
image

Is there any way to return a failure message for non-applicable endpoints, while keeping the ability to sort numerically? Or an alternative to If…Then…Else?

I don’t want to return 0 for desktops, because it might get confusing if there’s also a laptop with 0% health. I’d also like to avoid just returning an error for desktops, just because I think it’s nicer to return an explicit value that you know ran correctly.

Thank you

I believe you have two options there.

One is for your else to return ‘nothing’

if ((value of setting "_BESClient_ComputerType" of client) is "Laptop") then ((integer value of selects ("FullChargedCapacity from BatteryFullChargedCapacity") of wmi "root/wmi")/(integer value of selects ("DesignedCapacity from BatteryStaticData") of wmi "root/wmi") as floating point * ("100.0" as floating point)) as string else nothing

The other is to return an error message

if ((value of setting "_BESClient_ComputerType" of client) is "Laptop") then ((integer value of selects ("FullChargedCapacity from BatteryFullChargedCapacity") of wmi "root/wmi")/(integer value of selects ("DesignedCapacity from BatteryStaticData") of wmi "root/wmi") as floating point * ("100.0" as floating point)) as string else error "Not a laptop"

I’m honestly not sure whether either version will give a numeric sort in the Console though, I’m not certain that the Console respects the data types in the column. Give it a try and let us know, we might need to zero-pad the percentages for sorting.

Thanks @JasonWalker! I didn’t know about the custom error messages, and it seems to work perfectly now.

image

The floating points sort, and the custom <Not a Laptop!> messages get grouped together separately.

Thanks again!

2 Likes