Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant Windows PowerShell Guide

You're reading from   Instant Windows PowerShell Guide Enhance your knowledge of Windows PowerShell and get to grips with its latest features

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781849686785
Length 86 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Harshul Patel Harshul Patel
Author Profile Icon Harshul Patel
Harshul Patel
Arrow right icon
View More author details
Toc

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:

  1. The following command statement retrieves CIM instances from the Win32_ComputerSystem CIM class.

    PS C:\> Get-CimInstance -ClassName Win32_ComputerSystem
    
  2. 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%'"
    
  3. The following two command statements subsequently create a remote CimSession on the PSTest 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 supplies CimSession that can be retrieved in a variable by using the New-CimSession or Get-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 the CimInstance object as InputObject 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 as root/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 the SELECT 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 the Query parameter. By default, it takes WQL as the query language.

  • -ResourceUri <Uri>: This parameter allows us to provide an external Uri to get CimInstance 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.

You have been reading a chapter from
Instant Windows PowerShell Guide
Published in: Nov 2013
Publisher: Packt
ISBN-13: 9781849686785
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image