2

#PSTip Converting .NET types to CIM types and vice versa

Note: This tip requires PowerShell 3.0 or above.

If you get a list of CIM class properties you’ll notice the CimType property.

PS> $class = Get-CimClass -ClassName Win32_Process
$class.CimClassProperties | Format-Table Name,CimType

Name                        CimType
----                        -------
Caption                      String
Description                  String
InstallDate                DateTime
Name                         String
Status                       String
CreationClassName            String
CreationDate               DateTime
CSCreationClassName          String
CSName                       String
ExecutionState               UInt16
Handle                       String
KernelModeTime               UInt64
OSCreationClassName          String
OSName                       String
Priority                     UInt32
TerminationDate            DateTime
UserModeTime                 UInt64
WorkingSetSize               UInt64
CommandLine                  String
ExecutablePath               String
HandleCount                  UInt32
(...)

The CimType property specifies a CIM type, such as integer, string, or datetime. The CimConverter class allows us to convert CIM types to the equivalent .NET type using the GetDotNetType static method. The following snippet lists the values of the CimType enum and creates a table of the CIM type and its .NET equivalent.

[Microsoft.Management.Infrastructure.CimType].GetEnumNames() | ForEach-Object {
    [PSCustomObject]@{
        CimType = $_
        NetType = [Microsoft.Management.Infrastructure.CimConverter]::GetDotNetType($_)
    }
}

CimType        NetType
-------        -------
Unknown
Boolean        System.Boolean
UInt8          System.Byte
SInt8          System.SByte
UInt16         System.UInt16
SInt16         System.Int16
UInt32         System.UInt32
SInt32         System.Int32
UInt64         System.UInt64
SInt64         System.Int64
Real32         System.Single
Real64         System.Double
Char16         System.Char
DateTime
String         System.String
Reference      Microsoft.Management.Infrastructure.CimInstance
Instance       Microsoft.Management.Infrastructure.CimInstance
BooleanArray   System.Boolean[]
UInt8Array     System.Byte[]
SInt8Array     System.SByte[]
UInt16Array    System.UInt16[]
SInt16Array    System.Int64[]
UInt32Array    System.UInt32[]
SInt32Array    System.Int32[]
UInt64Array    System.UInt64[]
SInt64Array    System.Int64[]
Real32Array    System.Single[]
Real64Array    System.Double[]
Char16Array    System.Char[]
DateTimeArray
StringArray    System.String[]
ReferenceArray Microsoft.Management.Infrastructure.CimInstance[]
InstanceArray  Microsoft.Management.Infrastructure.CimInstance[]

The GetCimType static method allows us to go the other direction and convert a .NET type to CIM type. A value of ‘Unknown’ is returned if the mapping is ambiguous.

[Microsoft.Management.Infrastructure.CimConverter]::GetCimType('int')
SInt32
Filed in: Columns, Tips and Tricks Tags: , , ,

2 Responses to "#PSTip Converting .NET types to CIM types and vice versa"

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: