PowerShell & SCCM – Getting Started with WMI

Did you know that you can use Windows PowerShell to automate SCCM 2007?
There are no native cmdlets, however you can use WMI to automate almost everything in your SCCM 2007 environment. In this post we’ll take a look at how to use WMI in Windows PowerShell.

There are a couple of ways to work with the WMI Classes using PowerShell. In this post we’ll look at some of the cmdlets available.

Invoke-WmiMethod

The Invoke-WmiMethod cmdlet calls Windows Management Instrumentation methods. The example below demonstrates how start calc.exe using Win32_Process.

PS > Invoke-WmiMethod -path win32_process -name create -argumentlist calc.exe

Get-WmiObject

The Get-WmiObject cmdlet gets instances of WMI classes or information about the available classes. We can use the Get-WmiObject cmdlet to retrieve the process we started in an earlier example.

PS > $calc = Get-WmiObject -Query "Select * From Win32_Process WHERE Name = 'calc.exe'"

If we type $calc in Windows PowerShell, information about the process is returned. We can also display the Methods and Properties supported by using the Get-Member cmdlet.

PS > $calc | Get-Member

Processes are fun, but aren’t we supposed to talk about SCCM? well, of course! The Get-WmiObject cmdlet supports the -List switch parameter that displays all the available classes. If we want to display all the WMI classes available in SCCM we can start PowerShell on the SCCM server and type the following type:

PS > Get-WmiObject -Namespace "root\SMS\Site_LAB" -List

Notice how we also set the Namespace to “root\SMS\Site_LAB”. In the example above the SCCM sitenameis “LAB”, simply replace “LAB” with your sites name.

Remove-WmiObject

The Remove-WmiObject cmdlet deletes an instance of an existing WMI class. In the example above we stored an instance of Win32_Process in a variable, if we want to delete the process we can type:

PS > $calc | Remove-WmiObject

When we pipe $calc to Remove-WmiObject the calc.exe process is terminated.

Set-WmiInstance

The Set-WmiInstance cmdlet creates or updates an instance of an existing wmi class. We can use the -Arguments parameter to change property values. The Arguments parameter accepts a hashtable as input. In the upcoming posts we’ll see alot of examples on how to use the Set-WmiInstance cmdlet.

PowerShell & SCCM

The following posts demonstrate how you can work with Collections, Packages, Programs, Distribution Points, Advertisements and finally how you can manage the Client.

Collections

Packages

Program

Distribution Point

Advertisement

Client

Rating 3.00 out of 5
[?]

Get-PrinterInformation

Gathering Printerinformation can be done through WMI. Win32_Printer contains information about printers that are used by a computer and Win32_PrinterDriver contains information about the printer drivers. We can combine these 2 WMI classes and retrieve information about both the printer and it’s drivers. This script checks for the following properties:

Computer             : Client1
Name                 : \\SERVER\SRV-FLOOR1-SV01
DefaultPrinter       : True
DriverName           : HP LaserJet 4250 PCL 6
DriverPath           : C:\Windows\system32\spo...
Driverdll            : UNIDRV.DLL
HorizontalResolution : 600
VerticalResolution   : 600
LocalPrinter         : False
PrintProcessor       : HPZPP4wm
Location             : GOT
Comment              : Company Printer
Description          : Printer on Floor 1

I’ve included a switch that let’s you pipe the information to a csv instead of displaying it to the host, and if you pipe an array of computer names to the script you can retrieve information from multiple computers.

Running the Script:


PS > .\Get-PrinterInformation.ps1 -Computer Client1 -ToCsv

PS > "Client1","Client2","Client3" |
>> ForEach { .\Get-PrinterInformation.ps1 -Computer $_ }

Here’s a link to the script.
Get-PrinterInformation.ps1

Rating 4.00 out of 5
[?]