This is the start of several examples for leveraging PowerShell with Systems Center Configuration Manager (ConfigMgr). We'll start slow, and work our way up to more complex tasks. A few important notes before we get started:
-
All examples will be based on the SCCM 2007 VHD, using the Server Name SCCMVHD, and the Site code 007.
-
I recommend you test your PowerShell scripts using a test environment also. Scripting and SCCM are great together, but can also be very dangerous. Test, Test, Test!
-
Most of these scripts will also work with SMS 2003 but be sure to test!
-
All examples are based on the current version of PowerShell (v 1.0)
-
Most scripts will require admin rights to the SCCM server, as well as admin rights inside of SCCM.
Here's an example of using PowerShell to access SCCM information.
get-wmiobject -Class SMS_SystemResourceList -NameSpace `
root\sms\site_007 -ComputerName sccmvhd
As you can see from the command-line, we're querying WMI on the SCCMVHD server, to the namespace root\sms\site_007, and the class SMS_SystemResourceList. Notice the tick mark ` This is a line continuation character. You will find that many of the sample commands in this article use the line continuation character to improve readability. When running in PowerShell, you can keep this continuation character, or delete it and move subsequent lines of code to be on one line to execute. We can abbreviate the previous code a bit, as follows:
gwmi -Cl SMS_SystemResourceList -Name root\sms\site_007 `
-comp sccmvhd
When you run this, you will receive a lot of output on the screen - (and if you run this on a central site from a multi-site environment, you will see the information for the central site, and all child sites). First, let's look at the properties and methods for this class - add the | get-member cmdlet to the end of the command line, as follows:
gwmi -Cl SMS_SystemResourceList -Name root\sms\site_007 `
-comp sccmvhd | get-member
As you can see, the output from the previous line displays the properties for SMS_SystemResourceList (NalPath, ResourceType, RoleName, ServerName, ServerRemoteName, and SiteCode). You also see some standard methods that we'll discuss in detail in future articles. For ease of use, assign the variable $ResList to the data obtained from SMS_SystemResourceList:
$ResList = gwmi -Cl SMS_SystemResourceList -Name `
root\sms\site_007 -comp sccmvhd
Notice that nothing is returned when running the previous command. It's actually returned to $ResList, it just doesn't display the details to the console. Run the following to display the objects in $ResList.
$Reslist
Now let's focus on displaying the output. Here are a few examples:
$ResList | ft RoleName, ServerName, ServerRemoteName, SiteCode
ft = "Format-Table" - and then specify the properties to display in the table. To squeeze the columns a little closer together, add -autosize to the end of the command line.
At first glance, this command line:
$ResList | select-object RoleName, ServerName, `
ServerRemoteName, SiteCode
Performs the same operation as the format-table cmdlet. But by using select-object, we're then able to perform additional operations, such as sort, where, etc.
$ResList | select-object RoleName, ServerName, `
ServerRemoteName, SiteCode | sort-object ServerName, RoleName
This command sorts the output by ServerName, then by RoleName.
$ResList | where-object {$_.RoleName -eq "SMS Distribution Point"}
This command displays all distribution points. Notice the $_.RoleName variable. $_ is a special variable that represents the individual items in the pipeline. As we do a few more examples, this will begin to make more sense. Also, to be clear, we didn't have to actually make a $ResList variable - this command line performs the same operation:
gwmi -Cl SMS_SystemResourceList -Name root\sms\site_007 `
-comp sccmvhd | where-object {$_.RoleName -eq "SMS Distribution Point"}
But as you can see, the command line is starting to get pretty long, so you may find it easier at times to work with a variable if you're going to be manipulating the same objects multiple times.
Here are a few more formatting commands, to help make your day a little easier:
$reslist | select-object RoleName, ServerName, ServerRemoteName, `
SiteCode | export-csv c:\temp\ResList.csv -noTypeInformation
This command exports the data to a CSV file, which can easily be read using Microsoft Excel.
$reslist | select-object RoleName, ServerName, ServerRemoteName, `
SiteCode | export-clixml c:\temp\ResList.xml
Use export-clixml to export the data to an XML file.
$reslist | select-object RoleName, ServerName, ServerRemoteName, `
SiteCode | ConvertTo-HTML
Convert to an HTML file, and display in the console. To export to an HTML file, use the following:
$reslist | select-object RoleName, ServerName, ServerRemoteName, `
SiteCode | ConvertTo-HTML >c:\temp\ResList.html
That's a pretty good start! Spend some time running the commands in this article - get comfortable with piping, sorting, and selecting objects. If you get the hang of it, change the class to SMS_Package, or SMS_Advertisement, and experiment a bit. Please post questions/comments or send me your feedback (ramseyg@hotmail.com) as far as the usefulness (or lack thereof).
Stay tuned for more PowerShell and SCCM fun!
Greg
ramseyg@hotmail.com