Fun with ConfigMgr 2007 and PowerShell


I needed some PowerShell practice this evening, so I did some experimentation to figure out how to interact with the ConfigMgr provider via PowerShell and WMI.  As a result of that experimentation, I’ve put together a module that you might find useful, if nothing else as a good example of how to do similar activities yourself.

A few notes:

  • I am using PowerShell v2 CTP 3 (download from http://www.microsoft.com/downloads/details.aspx?FamilyID=c913aeab-d7b4-4bb1-a958-ee6d7fe307bc&DisplayLang=en) for this, and it will not work with PowerShell v1 because I am using modules and advanced functions.
  • Because this is a PowerShell script, you’ll need to save the text below locally on your computer and make sure you’ve set the execution policy to something like “RemoteExecute”.  (The script isn’t signed.)
  • The script doesn’t currently support credentials for connecting to the ConfigMgr site – you can add that if you want, but I prefer to use the credentials of the currently-logged-on account.
  • Presently the script only does queries.  Creating and updating items are a little more dangerous (and as you can see below, deletes are trivial).

Here’s a brief description of each of the functions included in the module:

  • Connect-SCCMServer [-serverName <name>] [-siteCode <siteCode>]

    The Connect-SCCMServer method handles the details of connecting to the ConfigMgr provider (properly, by following the link from the site server to the provider server).  If you don’t specify a server name, the script will assume the local machine.  If you don’t specify a site code, it will use the default site code on the machine.  The resulting connection details (provider server name, site code, namespace path) are saved in global variables so that the other functions don’t require specifying all the same details.  (Yes, it gets a bit tiring to type those over and over again.

  • Get-SCCMObject -class <className> [-filter <filterText>]

    The Get-SCCMObject method is the base routine used for retrieving items from ConfigMgr.  Specify the class name (e.g. SMS_Package) and an optional filter (the where clause to insert in the WQL, e.g. “Name=’ABCD’”).  This function is used by all the other functions, so you don’t need to use this one directly if you don’t want to.

  • Get-SCCMPackage
  • Get-SCCMCollection
  • Get-SCCMAdvertisement
  • Get-SCCMDriver
  • Get-SCCMDriverPackage
  • Get-SCCMTaskSequence
  • Get-SCCMSite
  • Get-SCCMImagePackage
  • Get-SCCMOperatingSystemInstallPackage
  • Get-SCCMBootImagePackage

    These methods all take an optional “-filter” parameter (see above) and retrieve a list of the specified type of items.

  • Get-SCCMSiteDefinition

    This retrieves the site definition properites, used by the next function.

  • Get-SCCMIsR2

    This returns true if the site is running ConfigMgr R2 and false if it isn’t.

You can do fancier things with these too, if you want to get creative.  For example, assume you want to delete all drivers.  You could do something like this:

Import-Module C:\SCCM.PSM1

Connect-SCCMServer -serverName MYSERVER

Get-SCCMDriver  | % {$_.Delete()}

Here’s the actual script.

— Snip —

function Connect-SCCMServer {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $serverName,
        [Parameter(Position=2)] $siteCode
    )

    # Clear the results from any previous execution

    Clear-Variable -name sccmServer -errorAction SilentlyContinue
    Clear-Variable -name sccmNamespace -errorAction SilentlyContinue
    Clear-Variable -name sccmSiteCode -errorAction SilentlyContinue
    Clear-Variable -name sccmConnection -errorAction SilentlyContinue

    # If the $serverName is not specified, use "."

    if ($serverName -eq $null -or $serverName -eq "")
    {
        $serverName = "."
    }

    # Get the pointer to the provider for the site code

    if ($siteCode -eq $null -or $siteCode -eq "")
    {
        Write-Verbose "Getting provider location for default site on server $serverName"
        $providerLocation = get-wmiobject -query "select * from SMS_ProviderLocation where ProviderForLocalSite = true" -namespace "root\sms" -computername $serverName -errorAction Stop
    }
    else
    {
        Write-Verbose "Getting provider location for site $siteName on server $serverName"
        $providerLocation = get-wmiobject -query "select * from SMS_ProviderLocation where SiteCode = ‘$siteCode’" -namespace "root\sms" -computername $serverName -errorAction Stop
    }

    # Split up the namespace path

    $parts = $providerLocation.NamespacePath -split "\\", 4
    Write-Verbose "Provider is located on $($providerLocation.Machine) in namespace $($parts[3])"
    $global:sccmServer = $providerLocation.Machine
    $global:sccmNamespace = $parts[3]
    $global:sccmSiteCode = $providerLocation.SiteCode

     # Make sure we can get a connection

    $global:sccmConnection = [wmi]"${providerLocation.NamespacePath}"
    Write-Verbose "Successfully connected to the specified provider"
}

function Get-SCCMObject {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $class,
        [Parameter(Position=2)] $filter
    )

    if ($filter -eq $null -or $filter -eq "")
    {
        get-wmiobject -class $class -computername $sccmServer -namespace $sccmNamespace
    }
    else
    {
        get-wmiobject -query "select * from $class where $filter" -computername $sccmServer -namespace $sccmNamespace
    }
}

function Get-SCCMPackage {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter
    )

    Get-SCCMObject SMS_Package $filter
}

function Get-SCCMCollection {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter
    )

    Get-SCCMObject SMS_Collection $filter
}

function Get-SCCMAdvertisement {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter
    )

    Get-SCCMObject SMS_Advertisement $filter
}

function Get-SCCMDriver {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter
    )

    Get-SCCMObject SMS_Driver $filter
}

function Get-SCCMDriverPackage {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter
    )

    Get-SCCMObject SMS_DriverPackage $filter
}

function Get-SCCMTaskSequence {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter
    )

    Get-SCCMObject SMS_TaskSequence $filter
}

function Get-SCCMSite {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter
    )

    Get-SCCMObject SMS_Site $filter
}

function Get-SCCMImagePackage {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter
    )

    Get-SCCMObject SMS_ImagePackage $filter
}

function Get-SCCMOperatingSystemInstallPackage {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter
    )

    Get-SCCMObject SMS_OperatingSystemInstallPackage $filter
}

function Get-SCCMBootImagePackage {

    [CmdletBinding()]
    PARAM
    (
        [Parameter(Position=1)] $filter
    )

    Get-SCCMObject SMS_BootImagePackage $filter
}

function Get-SCCMSiteDefinition {

    # Refresh the site control file

    Invoke-WmiMethod -path SMS_SiteControlFile -name RefreshSCF -argumentList $sccmSiteCode -computername $sccmServer -namespace $sccmNamespace

    # Get the site definition object for this site

    $siteDef = get-wmiobject -query "select * from SMS_SCI_SiteDefinition where SiteCode = ‘$sccmSiteCode’ and FileType = 2" -computername $sccmServer -namespace $sccmNamespace

    # Return the Props list
    $siteDef | foreach-object { $_.Props }
}

function Get-SCCMIsR2 {

    $result = Get-SCCMSiteDefinition | ? {$_.PropertyName -eq "IsR2CapableRTM"}
    if (-not $result)
    {
        $false
    }
    elseif ($result.Value = 31)
    {
        $true
    }
    else
    {
        $false
    }
}

— Snip —

Save the text between the “— Snip —” lines (without actually including those lines) to a file named “SCCM.PSM1”.  Then run PowerShell (either the GUI or command line interfaces), load the script, connect to the site, and run a query or two:

Import-Module C:\SCCM.psm1

Connect-SCCMServer -serverName MYSERVER -siteCode CEN

Get-SCCMPackage | out-GridView

Get-SCCMDriver -filter "DriverProvider=’Broadcom’ "

All of this is warm-up for MMS 2009 at the end of this month…


Comments (8)

  1. Anonymous says:

    Bonjour à tous, Me voilà de retour sur mon blog après un gros mois d’absence. Tout d’abord veuillez m

  2. Anonymous says:

    I’ve been working with the ConfigMgr (and previously, the SMS) provider for years, using the underlying

  3. Anonymous says:

    Pour ceux à qui cela n’évoque rien, Michael Niehaus est un Senior Software Development Engineer basé

  4. Anonymous says:

    A couple of interesting posts I came across. First up is a way to work with System Center Configuration

  5. Anonymous says:

    A couple of interesting posts I came across. First up is a way to work with System Center Configuration

  6. Panzerbjørn says:

    Great function, thanks. Maybe I’m missing something, but is there a way to report on the contents of a collection?

  7. showbox says:

    Thanks for the great info. I really loved this. I would like to apprentice at the same time as you amend your web site, how could i subscribe for a blog site?
    For more info on showbox please refer below sites:
    http://showboxandroids.com/showbox-apk/
    http://showboxappandroid.com/
    Latest version of Showbox App download for all android smart phones and tablets.
    http://movieboxappdownloads.com/ – It’s just 2 MB file you can easily get it on your android device without much trouble. Showbox app was well designed application for android to watch movies and TV shows, Cartoons and many more such things on your smartphone.
    For showbox on iOS (iPhone/iPad), please read below articles:
    http://showboxappk.com/showbox-for-ipad-download/
    http://showboxappk.com/showbox-for-iphone/
    Showbox for PC articles:
    http://showboxandroids.com/showbox-for-pc/
    http://showboxappandroid.com/showbox-for-pc-download/
    http://showboxforpcs.com/
    There are countless for PC clients as it is essentially easy to understand, simple to introduce, gives continuous administration, effectively reasonable. it is accessible at completely free of expense i.e., there will be no establishment charges and after establishment
    it doesn’t charge cash for watching films and recordings. Not simply watching, it likewise offers alternative to download recordings and motion pictures. The accompanying are the strides that are to be taken after to introduce Showbox application on Android.
    The above all else thing to be done is, go to the Security Settings on your Android telephone, Scroll down and tap on ‘Obscure sources’.

  8. aw says:

    hai, I just want to tell you that I am just very new to blogs and seriously loved this website. More than likely I’m planning to bookmark your blog post .
    You amazingly come with really good posts. Thanks a lot for sharing your blog Microsoft.

    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20Bank%20Jatim
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20Bank%20BJB
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20Bank%20BNI%2046
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20Bank%20BTPN%20Syariah
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20Bank%20DKI
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20Bank%20Panin
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20PT%20Mandiri%20Tunas%20Finance
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20Bank%20BRI%20Agro
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20Bank%20Muamalat
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20BItung
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20BursaKerja
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20CIKUPA
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20CPNS
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20D1
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20D2
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20D3
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20D4
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20Depnaker
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20Dokter
    http://www.lokerjobindo.com/search/label/Lowongan%20Kerja%20Dosen