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.
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-MemberProcesses 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" -ListNotice 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-WmiObjectWhen we pipe $calc to Remove-WmiObject the calc.exe process is terminated.