Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Comparing CIM and WMI in PowerShell

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, talks about comparing CIM and Windows PowerShell.

Hey, Scripting Guy! Question Hey, Scripting Guy! When I query using a CIM function, I get back different stuff than when I query WMI. I don’t get it. Can you help?

—BW

Hey, Scripting Guy! Answer Hello BW,

Microsoft Scripting Guy, Ed Wilson, is here. Yesterday in Working with Volumes in CIM, I began talking about WMI and CIM. Let’s continue by looking more in depth at what happens with Get-Volume and Win32_Volume.

Anyone who has done a lot of work with WMI and with Windows PowerShell knows about types and type data. For example, when I query the WIN32_BIOS WMI class, only a certain amount of information returns. This is shown here:

PS C:\> Get-WmiObject win32_Bios

SMBIOSBIOSVersion : Hyper-V UEFI Release v1.0

Manufacturer      : Microsoft Corporation

Name              : Hyper-V UEFI Release v1.0

SerialNumber      : 6591-2614-0518-3297-7423-5669-65

Version           : VRTUAL - 1

If I want to look at more in-depth information, I can pipe the output to the Format-List cmdlet, and it will return all of the information, as shown here:

PS C:\> Get-WmiObject win32_Bios | fl *

PSComputerName        : C1

Status                : OK

Name                  : Hyper-V UEFI Release v1.0

Caption               : Hyper-V UEFI Release v1.0

SMBIOSPresent         : True

__GENUS               : 2

__CLASS               : Win32_BIOS

__SUPERCLASS          : CIM_BIOSElement

__DYNASTY             : CIM_ManagedSystemElement

__RELPATH             : Win32_BIOS.Name="Hyper-V UEFI Release

                                   v1.0",SoftwareElementID="Hyper-V UEFI Release v1.0",
                                  SoftwareElementState=3,TargetOperatingSystem=0,Version="VRTUAL - 1"

__PROPERTY_COUNT      : 27

__DERIVATION          : {CIM_BIOSElement, CIM_SoftwareElement, CIM_LogicalElement, CIM_ManagedSystemElement}

__SERVER              : C1

__NAMESPACE           : root\cimv2

__PATH                : \\C1\root\cimv2:Win32_BIOS.Name="Hyper-V UEFI Release
      
                                v1.0",SoftwareElementID="Hyper-V UEFI Release v1.0",

                                 SoftwareElementState=3,TargetOperatingSystem=0,Version="VRTUAL - 1"

BiosCharacteristics   : {3, 9, 15, 16...}

BIOSVersion           : {VRTUAL - 1, Hyper-V UEFI Release v1.0, EDK II - 10000}

BuildNumber           :

CodeSet               :

CurrentLanguage       :

Description           : Hyper-V UEFI Release v1.0

IdentificationCode    :

InstallableLanguages  :

InstallDate           :

LanguageEdition       :

ListOfLanguages       :

Manufacturer          : Microsoft Corporation

OtherTargetOS         :

PrimaryBIOS           : True

ReleaseDate           : 20121126000000.000000+000

SerialNumber          : 6591-2614-0518-3297-7423-5669-65

SMBIOSBIOSVersion     : Hyper-V UEFI Release v1.0

SMBIOSMajorVersion    : 2

SMBIOSMinorVersion    : 4

SoftwareElementID     : Hyper-V UEFI Release v1.0

SoftwareElementState  : 3

TargetOperatingSystem : 0

Version               : VRTUAL - 1

Scope                 : System.Management.ManagementScope

Path                  : \\C1\root\cimv2:Win32_BIOS.Name="Hyper-V UEFI Release

                        v1.0",SoftwareElementID="Hyper-V UEFI Release v1.0",
                        SoftwareElementState=3,TargetOperatingSystem=0,Version="VRTUAL - 1"

Options               : System.Management.ObjectGetOptions

ClassPath             : \\C1\root\cimv2:Win32_BIOS

Properties            : {BiosCharacteristics, BIOSVersion, BuildNumber, Caption...}

SystemProperties      : {__GENUS, __CLASS, __SUPERCLASS, __DYNASTY...}

Qualifiers            : {dynamic, Locale, provider, UUID}

Site                  :

Container             :

The default properties that are displayed are the result of type data for the specific WMI class. I can find this by using the Get-TypeData cmdlet. Here is an example that tells me that I do, in fact, have specific type data for the WMI class:

PS C:\> "*win32_bios*" | Get-TypeData

TypeName                                Members                                

--------                                -------                               

System.Management.ManagementObject#r... {}                                    

Microsoft.Management.Infrastructure.... {}              

I can look at this in more detail, by piping the results to the Format-List cmdlet:

S C:\> "*win32_bios*" | Get-TypeData | fl * 

TypeName                        : System.Management.ManagementObject#root\cimv2 \Win32_BIOS

Members                         : {}

TypeConverter                   :

TypeAdapter                     :

IsOverride                      : False

SerializationMethod             :

TargetTypeForDeserialization    :

SerializationDepth              : 0

DefaultDisplayProperty          :

InheritPropertySerializationSet : False

StringSerializationSource       :

DefaultDisplayPropertySet       : System.Management.Automation.Runspaces.PropertySetData

DefaultKeyPropertySet           :

PropertySerializationSet        : 

TypeName                        : Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_BIOS

Members                         : {}

TypeConverter                   :

TypeAdapter                     :

IsOverride                      : False

SerializationMethod             :

TargetTypeForDeserialization    :

SerializationDepth              : 0

DefaultDisplayProperty          :

InheritPropertySerializationSet : False

StringSerializationSource       :

DefaultDisplayPropertySet       : System.Management.Automation.Runspaces.PropertySetData

DefaultKeyPropertySet           :

PropertySerializationSet        : 

But, what I really want to look at is DefaultDisplayPropertySet. This property is what governs the properties that return by default when I query the Win32_Bios cmdlet. So, I look at the property:

PS C:\> ("*win32_bios*" | Get-TypeData).defaultdisplaypropertyset

ReferencedProperties                                                          

--------------------                                                          

{SMBIOSBIOSVersion, Manufacturer, Name, SerialNumber...}                      

{SMBIOSBIOSVersion, Manufacturer, Name, SerialNumber...}     

It is one more level to get the ReferencedProperties. This is shown here:

PS C:\> ("*win32_bios*" | Get-TypeData).defaultdisplaypropertyset.referencedproperties

SMBIOSBIOSVersion

Manufacturer

Name

SerialNumber

Version

SMBIOSBIOSVersion

Manufacturer

Name

SerialNumber

Version

If you look closely, you will see that there are two sets of the same five properties. This is because I have two instances of type data. One is for WMI the other is for CIM.

PS C:\> ("*win32_bios*" | Get-TypeData).typename

System.Management.ManagementObject#root\cimv2\Win32_BIOS

Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_BIOS

At the beginning of this post, I used Get-WMIObject to query Win32_Bios. As shown here, if I use Get-CimInstance, the output is the same:

PS C:\> Get-CimInstance Win32_BIOS

SMBIOSBIOSVersion : Hyper-V UEFI Release v1.0

Manufacturer      : Microsoft Corporation

Name              : Hyper-V UEFI Release v1.0

SerialNumber      : 6591-2614-0518-3297-7423-5669-65

Version           : VRTUAL - 1

What about Win32_Volume and Get-Volume? Well, we have a completely different WMI namespace. I can discover this by looking at the TypeName from Get-Member:

PS C:\> Get-Volume | gm

   TypeName: Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Win

dows/Storage/MSFT_Volume

Name                      MemberType     Definition                            

----                      ----------     ----------                           

Clone                     Method         System.Object ICloneable.Clone()     

Dispose                   Method         void Dispose(), void IDisposable.Di...

Equals                    Method         bool Equals(System.Object obj)       

GetCimSessionComputerName Method         string GetCimSessionComputerName()   

GetCimSessionInstanceId   Method         guid GetCimSessionInstanceId()       

GetHashCode               Method         int GetHashCode()                    

GetObjectData             Method         void GetObjectData(System.Runtime.S...

GetType                   Method         type GetType()                       

ToString                  Method         string ToString()                    

DriveLetter               Property       char DriveLetter {get;}              

FileSystem                Property       string FileSystem {get;}             

FileSystemLabel           Property       string FileSystemLabel {get;set;}    

ObjectId                  Property       string ObjectId {get;}               

Path                      Property       string Path {get;}                   

PSComputerName            Property       string PSComputerName {get;}         

Size                      Property       uint64 Size {get;}                   

SizeRemaining             Property       uint64 SizeRemaining {get;}          

DriveType                 ScriptProperty System.Object DriveType {get=switch...

HealthStatus              ScriptProperty System.Object HealthStatus {get=swi...

Notice, that the TypeName is a CimInstance, like one of the types for the Win32_Bios class. But notice that this also is in Root/Microsoft/Windows/Storage WMI namespace. This namespace is why Win32_Volume does not work on a computer running Windows 7, even though it may have Windows PowerShell 4.0 installed.

If I look for type data related to *volume*, I find four different types—but only one that is related to MSFT_Volume and none related to Win32_Volume. This is shown here:

PS C:\> ("*volume*" | Get-TypeData).TypeName

System.Management.ManagementObject#root\cimv2\Win32_VolumeQuotaSetting

Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_VolumeQuotaSetting

Microsoft.Management.Infrastructure.CimInstance#MSFT_Volume

Microsoft.FailoverClusters.PowerShell.ClusterSharedVolume

When I query Get-Volume, I get a nice clean output:

PS C:\> Get-Volume

DriveLetter FileSystemLabel FileSystem  DriveType  HealthStatus SizeRemaining  Size

----------- ----------- ----------  ---------  ---------- ----------       ----

C                       NTFS        Fixed      Healthy     108.53 GB  126.48 GB

E           NEW VOLUME  FAT32       Fixed      Healthy       1.52 GB    1.99 GB

            Recovery    NTFS        Fixed      Healthy      59.83 MB     300 MB

D                                   CD-ROM     Healthy           0 B        0 B

But, if I query Win32_Volume, I get back the WMI class, but not any formatted data. This is true even if I use Get-CimInstance. This is shown here:

PS C:\> Get-CimInstance win32_volume

Caption                      : C:\

Description                  :

InstallDate                  :

Name                         : C:\

Status                       :

Availability                 :

ConfigManagerErrorCode       :

ConfigManagerUserConfig      :

CreationClassName            :

DeviceID                     : \\?\Volume{a6325fa1-0f39-432c-a6db-e6388000463a}\

ErrorCleared                 :

ErrorDescription             :

LastErrorCode                :

PNPDeviceID                  :

PowerManagementCapabilities  :

PowerManagementSupported     :

StatusInfo                   :

SystemCreationClassName      :

SystemName                   : C1

Access                       :

BlockSize                    : 4096

ErrorMethodology             :

NumberOfBlocks               :

Purpose                      :

Automount                    : True

BootVolume                   : True

Capacity                     : 135810510848

Compressed                   : False

DirtyBitSet                  : False

DriveLetter                  : C:

DriveType                    : 3

FileSystem                   : NTFS

FreeSpace                    : 116536430592

IndexingEnabled              : True

Label                        :

MaximumFileNameLength        : 255

PageFilePresent              : True

QuotasEnabled                : False

QuotasIncomplete             : False

QuotasRebuilding             : False

SerialNumber                 : 3092510724

SupportsDiskQuotas           : True

SupportsFileBasedCompression : True

SystemVolume                 : False

PSComputerName               :

Caption                      : E:\

Description                  :

InstallDate                  :

Name                         : E:\

Status                       :

Availability                 :

ConfigManagerErrorCode       :

ConfigManagerUserConfig      :

CreationClassName            :

DeviceID                     : \\?\Volume{33ce26cc-3d92-11e3-8252-806e6f6e6963}\

ErrorCleared                 :

ErrorDescription             :

LastErrorCode                :

PNPDeviceID                  :

PowerManagementCapabilities  :

PowerManagementSupported     :

StatusInfo                   :

SystemCreationClassName      :

SystemName                   : C1

Access                       :

BlockSize                    : 4096

ErrorMethodology             :

NumberOfBlocks               :

Purpose                      :

Automount                    : True

BootVolume                   : False

Capacity                     : 2135949312

Compressed                   :

DirtyBitSet                  : False

DriveLetter                  : E:

DriveType                    : 3

FileSystem                   : FAT32

FreeSpace                    : 1628012544

IndexingEnabled              :

Label                        : NEW VOLUME

MaximumFileNameLength        : 255

PageFilePresent              : False

QuotasEnabled                :

QuotasIncomplete             :

QuotasRebuilding             :

SerialNumber                 : 3926397947

SupportsDiskQuotas           : False

SupportsFileBasedCompression : False

SystemVolume                 : False

PSComputerName               :

Caption                      : \\?\Volume{7a82aab2-29a6-4a13-9ca9-98e67bb153ef}\

Description                  :

InstallDate                  :

Name                         : \\?\Volume{7a82aab2-29a6-4a13-9ca9-98e67bb153ef}\

Status                       :

Availability                 :

ConfigManagerErrorCode       :

ConfigManagerUserConfig      :

CreationClassName            :

DeviceID                     : \\?\Volume{7a82aab2-29a6-4a13-9ca9-98e67bb153ef}\

ErrorCleared                 :

ErrorDescription             :

LastErrorCode                :

PNPDeviceID                  :

PowerManagementCapabilities  :

PowerManagementSupported     :

StatusInfo                   :

SystemCreationClassName      :

SystemName                   : C1

Access                       :

BlockSize                    : 4096

ErrorMethodology             :

NumberOfBlocks               :

Purpose                      :

Automount                    : True

BootVolume                   : False

Capacity                     : 314568704

Compressed                   : False

DirtyBitSet                  : False

DriveLetter                  :

DriveType                    : 3

FileSystem                   : NTFS

FreeSpace                    : 62734336

IndexingEnabled              : True

Label                        : Recovery

MaximumFileNameLength        : 255

PageFilePresent              : False

QuotasEnabled                : False

QuotasIncomplete             : False

QuotasRebuilding             : False

SerialNumber                 : 2387537837

SupportsDiskQuotas           : True

SupportsFileBasedCompression : True

SystemVolume                 : False

PSComputerName               :

Caption                      : D:\

Description                  :

InstallDate                  :

Name                         : D:\

Status                       :

Availability                 :

ConfigManagerErrorCode       :

ConfigManagerUserConfig      :

CreationClassName            :

DeviceID                     : \\?\Volume{04281f07-3c5a-11e3-824c-806e6f6e6963}\

ErrorCleared                 :

ErrorDescription             :

LastErrorCode                :

PNPDeviceID                  :

PowerManagementCapabilities  :

PowerManagementSupported     :

StatusInfo                   :

SystemCreationClassName      :

SystemName                   : C1

Access                       :

BlockSize                    :

ErrorMethodology             :

NumberOfBlocks               :

Purpose                      :

Automount                    : True

BootVolume                   :

Capacity                     :

Compressed                   :

DirtyBitSet                  :

DriveLetter                  : D:

DriveType                    : 5

FileSystem                   :

FreeSpace                    :

IndexingEnabled              :

Label                        :

MaximumFileNameLength        :

PageFilePresent              :

QuotasEnabled                :

QuotasIncomplete             :

QuotasRebuilding             :

SerialNumber                 :

SupportsDiskQuotas           :

SupportsFileBasedCompression :

SystemVolume                 :

PSComputerName               :

If, I want to use the Win32_Volume WMI class, and if I want the nice clean output I get from Get-Volume, I need to create TypeData for the Win32_Volume class. But that is another story...

BW, there's a good start to understanding how to use CIM. CIM Week will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles