Author Posts

January 1, 2012 at 12:00 am

by OliAdams at 2013-04-01 10:13:33

I am trying to split the number returned from querying windows security center productstate. I need to split the decimal number into 3 separate hex numbers. I am using:

#Extract the productstate code from wmi
$AntiSpywareState = Get-WmiObject -Namespace Root\SecurityCenter2 -Class AntiSpywareProduct |
Select-Object -ExpandProperty ProductState
397568

#Convert the code to a hex number
$AntiSpywareState = ([System.Convert]::ToInt32($AntiSpywareState, 16))

61100

#Add the leading 0
$AntiSpywareState = "{0:d6}" -f $AntiSpywareState

061100

Should I have to add the 0 here or am I doing something wrong? Thanks

by ps_gregg at 2013-04-01 20:42:56

Hi OliAdams,

The returned value from the Get-WmiObject line is a "System.UInt32". So with that value you only need to use .NET string formatting.

#Extract the productstate code from wmi
$AntiSpywareState = Get-WmiObject -Namespace Root\SecurityCenter2 -Class AntiSpywareProduct |
Select-Object -ExpandProperty ProductState

# Using .NET string formatting - Convert Int to a Hex number
$AntiSpywareHex = '{0:X6}' -f $AntiSpywareState

061100

the 'X' format specifier converts a number to a hexadecimal string. The '6' specifies how many characters to return in the string ( ie. 0:X8 would return '00061100' )

Hope that helps

-Gregg

by nohandle at 2013-04-02 02:32:06

Hi, just be aware that the uint is 32bit long so the maximum value takes 8 (32/4) places in hex not just 6.
('{0:X}' -f ([uint32]]
8

On side note: The conversion by the 0x prefix in powershell converts the number to integer rather than unsigned integer, another thing to be aware of.