System Center, Automation, Powershell and other Thoughts

Tag Archives

SCCM vNext and Powershell

Most of you already know the power of Powershell. Also more and more products, also of the System Center family, have support for Powershell.

But what about SCCM?

In the actual version, SCCM 2007, there is little support for Powershell.
Only because the Powershell has a small amount of cmdlets for WMI, it can be used for managing, administration and automation on SCCM.

So what’s about Powershell support in SCCM vNext?

As I’m very interested in that topic I have asked several people including MVP’s and the technical partner line from Microsoft to get a hint.
All answers are pointing towards the same direction:

“No official statement at the moment, please look forward to MMS 2010

So, in the words of Douglas Adams: “DON’T PANIC”,
just be patient for a few days more…

Search for a string in a bunch of files …

A colleague had a directory with a lot of log-files in. In these he wants to search for a string and get the filename and line number automatically.

So we made this simple code in Powershell:

$items = Get-item *.log 
Foreach($item in $items)
                $loc = $item.fullname; 
                $content = get-content $item
                $contentcount = $content.count

                For($i = 0;$i –lt $contentcount; $i++)
                               If($content[$i] –match $searchstring)

So he was happy.

A few days later he came around with a simpler one:

select-string -path "*.log" -pattern "$searchstring" -allmatches -simplematch

Add a computer variable using Powershell

Servers mostly run with fixed IP-Addresses. So a question was to automate the server deployment using SCCM that also adds a fixed IP-Address to the server.

While SCCM has the ability to add a fixed IP-Address, the thing to do the deployment automatically was to add the OSD-Variables to the computer object in SCCM.

This can be done using a simple Powershell script and the WMI-Classes brought with SCCM.

# Powershell V2 only (V1 has errors in WMI)
param([string]$computer = ".",[string]$smssite = "MND",[int]$ResourceID=-9999,[string]$variable,[string]$value)

if(($ResourceID -ne -9999) -and ($variable -ne "") -and ($value -ne ""))
$pc_class = [WmiClass]""
$pc_class.psbase.Path ="\\$computer\ROOT\SMS\SITE_$($smssite):SMS_MachineSettings"
$pc = $pc_class.createInstance()

$pc.ResourceID = $ResourceID
$pc.SourceSite = $smssite


$pc.MachineVariables = $pc.MachineVariables + [WmiClass]"\\$computer\ROOT\SMS\SITE_$($smssite):SMS_MachineVariable"

$machinevariables = $pc.MachineVariables 

$machinevariables[($machinevariables.count)-1].Name = $variable
$machinevariables[($machinevariables.count)-1].value = $value

$pc.MachineVariables = $machinevariables 


  "Variable set"
  "Error in setting variable"
  "Not enough arguments given."

Adding variables this way is very easy and the correct names can be found in the SCCM documentation (