WMI versus CIM (Advanced)
In earlier versions, Windows PowerShell's inventory mechanism was strongly dependent on Windows Management Instrumentation (WMI). With the release of PowerShell Version 3.0, we have Common Information Model known as CIM. PowerShell Version 3.0 has ample number of CMDLETs under the default module named CimCmdlets
.
Getting ready
Moreover, CIM
CMDLETs work with WSMan to manage Windows as well as other operating systems. To ensure all the CMDLETs are available with CimCmdlets
, use the following code:
PS C :\> Get-Comman d -Module CimCmdlets CommandType Name ModuleName ----------- ---- ---------- Cmdlet Get-CimAssociatedInstance CimCmdlets Cmdlet Get-CimClass CimCmdlets Cmdlet Get-CimInstance CimCmdlets Cmdlet Get-CimSession CimCmdlets Cmdlet Invoke-CimMethod CimCmdlets Cmdlet New-CimInstance CimCmdlets Cmdlet New-CimSession CimCmdlets Cmdlet New-CimSessionOption CimCmdlets Cmdlet Register-CimIndicationEvent CimCmdlets Cmdlet Remove-CimInstance CimCmdlets Cmdlet Remove-CimSession CimCmdlets Cmdlet Set-CimInstance CimCmdlets
How to do it...
Execute the following lines of code, by performing the following steps:
The following command statement retrieves CIM instances from the
Win32_ComputerSystem
CIM class.PS C:\> Get-CimInstance -ClassName Win32_ComputerSystem
The following command statement executes a query for services starting with the
win
keyword.PS C:\> Get-CimInstance -Query "SELECT * from Win32_Service WHERE name LIKE 'win%'"
The following two command statements subsequently create a remote
CimSession
on thePSTest
computer and retrieves CIM instances for all the processes from the PSTest CIM server.PS C:\> $S = New-CimSession -ComputerName PSTest PS C:\> Get-CimInstance -ClassName Win32_Process -CimSession $S
How it works...
The Get-CimInstance
CMDLET retrieves CIM instances from the specified computer and the CIM class name. This CMDLET omits one or more output instance object, providing information from the specified class. The following are the parameters available with this CMDLET:
-CimSession <CimSession[]>
: This parameter suppliesCimSession
that can be retrieved in a variable by using theNew-CimSession
orGet-CimSession
CMDLET.-ClassName <String>
: This parameter provides the CIM class name for which we need to retrieve CIM instances.Tip
You can use tab completion to browse for the
ClassName
parameter's values.-ComputerName <String[]>
: This parameter supplies a list of computer names to retrieve the respective CIM instances for them. This CMDLET creates a temporary session on specified computers using the WSMan protocol to retrieve the information.-Filter <String>
: This parameter specifies a condition to filter the output data.-InputObject <CimInstance>
: You can also specify theCimInstance
object asInputObject
for this CMDLET using this parameter.-KeyOnly [<SwitchParameter>]
: This parameter helps to get the output object with only the key properties, which in fact reduces the load over the network.-Namespace <String>
: This parameter specifies the namespace on which the CIM class resides. The default namespace value is defined asroot/cimv2
.Tip
You can use tab completion to browse for namespaces.
-OperationTimeoutSec <UInt32>
: This parameter explicitly specifies the time during which CMDLET can wait for the output from the computer.-Property <String[]>
: This parameter gives us a choice to select the required properties from the property set. It reduces the output data size.-Query <String>
: This parameter allows us to supply theSELECT
query to the CMDLET. It accepts all the operators supported by WMI Query Language (WQL).-QueryDialect <String>
: This parameter provides information on the query language that is used with theQuery
parameter. By default, it takes WQL as the query language.-ResourceUri <Uri>
: This parameter allows us to provide an external Uri to getCimInstance
objects. By default, the CMDLET searches for the specified class name at http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/.-Shallow [<SwitchParameter>]
: This parameter, if supplied, avoids retrieving information of a child class. By default, CMDLET retrieves information from both the class and child classes.
There's more…
There is also the Invoke-CimMethod
CMDLET available with the CimCmdlets
module. Instead of using Invoke-WmiMethod
, consider using Invoke-CimMethod
.