SCAPaoT

System Center, Automation, Powershell and other Thoughts

Archive for the ‘Powershell’ Category

(Update) SCCM vNext and Powershell

A few weeks ago,
(http://www.scapaot.de/blog/?p=57),
I have written about the question if there will be Powershell support in SCCM vNext.

On yesterday I had the chance to listen to a very interesting talk from Microsoft about the user centric software deployment and SCCM vNext.
There I had the chance to ask about the Powershell support in the next version.

The answer is:

The next version has support for the same WMI classes
as it has in SCCM 07.
And there will be new WMI classes for the new features.
Powershell support is there using WMI cmdlets.

And perhaps the SDK for vNext will have some little more infos about using the Powershell to manage SCCM.
Cmdlets are not planed at the moment.

So, for all of you want to learn how to automate SCCM,
go out and learn something about WMI.

Powershell: List Updates that are not Installed

Using the Microsoft Update Session ComObject, it is easy to determine the updates that are not installed on a computer at a moment.

# initialize the com object
$wsus = New-Object -ComObject "Microsoft.update.Session"

# build an update searcher
$wsussearch = $wsus.createupdatesearcher()

# build a filter and get the result
$res = $wsussearch.search("IsInstalled=0")

# list title and severity of updates that are not yet installed
$res.updates | Select-Object title, msrcseverity 

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:

param($searchstring)
$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)
                               {
                                    "$loc:$($i+1)"
                               }
                }
}

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.psbase.get() 

$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 

$pc.put()

if($?)
{
  "Variable set"
}
else
{
  "Error in setting variable"
}
}
else
{
  "Not enough arguments given."
}

Adding variables this way is very easy and the correct names can be found in the SCCM documentation (http://technet.microsoft.com/en-us/library/dd252744.aspx).