New CIM Cmdlets in PowerShell V3

New CIM Cmdlets in PowerShell V3
Hi Guys, Today i was going through the new CIM cmdlets present in  PowerShell V3 in Windows 8 and Windows Server 2012, they are a set of new cmdlets introduced in conjunction with the traditional WMI cmdlets.I just ran a comparison check on the number of cmdlets present for WMI and CIM and there was a quite of difference, WMI cmdlets are part of Microsoft.PowerShell.Management module and the new CIM cmdlets are a part of module CimCmdlets.

 

The New CIM cmdlets allow for communication to devices which have some instances of CIM running in them so now we can access devices beyond Windows for example we can interact with Linux devices and also may be some firewall devices etc which fully run on Linux.

CIM cmdlets communicate via both the WSMan and DCOM protocols and WMI communicates only via DCOM.

WSMan protocol is also much faster and also more secure in comparison to DCOM.

Also by using WSMan as a common protocol to communicate with remote devices we can bypass most of firewall issue which we may encounter which are not the case with DCOM as they are prone to firewall issues.

So lets take a look at  some of the new cmdlets present in cimcmdlet module, the rest im leaving for you to explore.

Get-CimAssociatedInstance :-  If you run a get help on the cmdlet here’s what you will see in the description

 

The Get-CimAssociatedInstance cmdlet retrieves the CIM instances connected to a specific CIM instance, called the source instance, by an association.
Lets illustrate the same using an example, let me extract the CIM instance of Win32_Diskpartition for disks by associating a connection with Win32_LogicalDisk, this feature is not available in WMI.

Lets first extract the CIM instance of Win32_Logicaldisk for all drives on my server, im passing a keyonly parameter this Returns objects with only key properties populated also this reduces the amount of data that is transferred over the network.

PS C:UsersAdministrator> $disk = Get-CimInstance -ClassName Win32_LogicalDisk -KeyOnly

Now let me extract the CIM associated instances for the first disk in $disks

PS C:UsersAdministrator> Get-CimAssociatedInstance -InputObject $disk[0]

 

Now let me extract the CIM instances of Win32_DiskPartition class name using the already extracted value.

PS C:UsersAdministrator> $disk | ForEach-Object {Get-CimAssociatedInstance -InputObject $_ -ResultClass Win32_DiskPartition} | ft -AutoSize

 

Also now i can extract the Computer System related CIM instance information from the extracted value using Win32_ComputerSystem classname.

 

Get-CimClass :-  The Get-CimClass cmdlet retrieves a list of CIM classes in a specific namespace.

The below command would help me list out all CIM classes in the namespace “root/cimv2” whose class name matches win32

PS C:UsersAdministrator> Get-CimClass -Namespace "root/cimv2" -ClassName win32*

 

Get-CimInstance :-  The Get-CimInstance cmdlet gets the CIM instances of a class from a CIM server. You can specify either the classname or a query for this cmdlet.

Lets retrieve CIM instances of a class named Win32_Logicaldisk.

PS C:UsersAdministrator> Get-CimInstance -ClassName Win32_logicaldisk

 

Now let me create CIM session using New-CIMsession cmdlet on servers and get associated CIMInstance values from them, here im just trying it localhost as the computername

PS C:UsersAdministrator> $s = New-CimSession –ComputerName localhost,localhost
PS C:UsersAdministrator> Get-CimInstance -ClassName Win32_ComputerSystem -CimSession $s

 

Get-CimSession :-  Gets the CIM session objects from the current session.

Now if i run a get sim session for the previous cim session i created using New-CimSession

i get the below output.

PS C:UsersAdministrator> Get-CimSession

 

Invoke-CimMethod :-  The Invoke-CimMethod cmdlet invokes a method of a CIM class or CIM instance using the name-value pairs specified  by the Arguments parameter.

Invoke-CIMMethod allows to pass arguments as hashtables too 🙂

Let me illustrate the cmdlet with starting a notepad process and also killing the same

PS C:UsersAdministrator> Invoke-CimMethod -ClassName Win32_Process -MethodName "Create" -Arguments @{ CommandLine = 'notepad.exe'; CurrentDirectory = "C:windowssystem32" }
PS C:UsersAdministrator> Invoke-CimMethod –Query 'select * from Win32_Process where name like "notepad%"' –MethodName "Terminate"

 

New-CimSession :- The New-CimSession cmdlet creates a CIM session. A CIM session is a client-side object representing a connection to a local computer or a remote computer. The CIM session contains information about the connection, such as ComputerName, the protocol used for the connection, session ID and instance ID.

New-CimSession is similar to something like New-PSSession for creating remote pssessions

Now let me create CIM session using New-CIMsession cmdlet on servers and get associated CIMInstance values from them, here im just trying it localhost as the computername

PS C:UsersAdministrator> $s = New-CimSession –ComputerName localhost,localhost
PS C:UsersAdministrator> Get-CimInstance -ClassName Win32_ComputerSystem -CimSession $s

 

Remove-CimSession :- The Remove-CimSession cmdlet removes one or more CIM session objects from the local Windows PowerShell session.

Now let me remove the CIMsession i created previously using New-CimSession by piping the output of Get-CimSession to Remove-CimSEssion

PS C:UsersAdministrator> Get-CimInstance | Remove-CimSession

 

 

Share this post

Post Comment