ConfigMgr: Powershell Script

Generell baue ich eine Configuration Manager Umgebung so auf, dass ich pro zu verteilender Software eine eigene Collection einrichte. Diese wird unterhalb eines Ordner _Software und darunter in einem Ordner mit dem Namen des Herstellers abgelegt. In diese Device Collection kommen dann die Clients, die die Software erhalten sollen.

So erhält man die maximale Flexibilität, auch wenn man am Anfang wahrscheinlich das meiste an die gleiche Gruppe von Clients verteilt.

Um diesen Ablauf zu vereinfachen, habe ich ein kleines Powershell Script geschrieben, dass die Ordner und die Collection entsprechend anlegt.

Der Aufruf erfolgt mittels:

.\createSWCollection.ps1 -vendor "Adobe" -software "Flash Player 14" -site MBK -siteserver cm01

Loading ConfigMgr Module  D:\Program files\ConfigMgr\bin\ConfigurationManager.psd1
Collection Flash Player 14 successfull created. ID:MBK00011

Das gesamte Script hat einige nützliche Funktionen, die ich auch in anderen Powershellscripte verwende:

[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$Vendor,
[Parameter(Mandatory=$true)]
[string]$Software,
[Parameter(Mandatory=$true)]
[string]$Site,
[Parameter(Mandatory=$true)]
[string]$Siteserver,
[Parameter(Mandatory=$false)]
[string]$rootFolder="_Software"
)

 #Load the ConfigurationManager Module

 #from http://www.dexterposh.com/2014/06/powershell-sccm-2012-getting-started.html
try
{
    $ConfigMgrModule = "$($env:SMS_ADMIN_UI_PATH | Split-Path -Parent)\ConfigurationManager.psd1"
    if (!(Test-Path $ConfigMgrModule)) {
        throw 'Configuration Manager module not found in admin console path'
    }
    write-host "Loading ConfigMgr Module " $ConfigMgrModule
    Import-Module $ConfigMgrModule
    $BeforeLocation = (Get-Location).Path
} catch
{
   Write-Error $_.Exception.Message
}

cd $site":"

$updateCollectionMin=30
$limitingCollection="All Systems"

$namespace="root\SMS\Site_$site"
$PSDefaultParameterValues =@{
    "get-cimclass:namespace"="Root\SMS\site_$site";
    "get-cimclass:computername"=$siteserver;
    "get-cimInstance:computername"=$siteserver;
    "get-ciminstance:namespace"="Root\SMS\site_$site";
    "get-wmiobject:namespace"="Root\SMS\site_$site";
    "get-WMIObject:computername"=$siteserver}

 $rootDeviceFolder=Get-CimInstance -ClassName SMS_ObjectContainerNode -Filter "ObjectType=5000 and ParentContainerNodeid=0"

 function getFolder([String]$name,[int]$parentID,[bool]$deviceFolder=$true) {
   if ($deviceFolder) {
       $objectTyp=5000
    } else {
     $objectTyp=6000
    }
    Get-CimInstance -ClassName SMS_ObjectContainerNode -Filter "ObjectType=$objectTyp and ParentContainerNodeid=$parentID and Name='$name'"
 }

 function createFolder([String]$name,[int]$parentID,[bool]$deviceFolder=$true)
 {
    if ($deviceFolder) {
       $objectTyp=5000
    } else {
     $objectTyp=6000
    }
    $rt=getfolder $name $parentID $deviceFolder
    if (-not $rt) {
        $rt=New-CimInstance -ClassName SMS_ObjectContainerNode -Property @{Name=$name;ObjectType=$objectTyp;ParentContainerNodeid=$parentID;SourceSite=$site} -Namespace $namespace -ComputerName $siteserver
        Write-Host "Folder $name successfull created."
    }
    else
    {
        write-verbose "Folder $name already exists."
    }
    $rt
 }

 function createFolderPath([String]$folderPath,[int]$parentID,[bool]$deviceFolder=$true)
 {
    if ($deviceFolder) {
       $objectTyp=5000
    } else {
     $objectTyp=6000
    }
    $folders=$folderPath.split('\')
    $parentID=0
    $folders | ForEach-Object {
       $folder=$_
       $rt=createFolder $folder $parentID $true
       $parentID=$rt.ContainerNodeID
    }
    $rt
 }

 #$rootDeviceFolder

 $folderpath=$rootFolder+"\$vendor"
 $targetFolder=createFolderPath $folderpath 0 $true

 $collection=Get-CMDeviceCollection -name $software
 if (-not $collection) {
    $collection=New-CMDeviceCollection -Name $software -Comment "Software Deployment Target for $software" -LimitingCollectionName $limitingCollection  -RefreshType Periodic -RefreshSchedule (New-CMSchedule -Start (get-date) -RecurInterval Minutes -RecurCount $updateCollectionMin)
    write-host ("Collection $software successfull created. ID:"+$collection.CollectionID)

 }
else
{
    write-verbose ("Collection $software already exists. ID:"+$collection.CollectionID)
}
Move-CMObject -FolderPath ($site+":\DeviceCollection\"+$folderpath) -InputObject $collection

cd $BeforeLocation
This entry was posted in Configuration Manager, Deutsch, Powershell, System Center, System Center 2012 and tagged , . Bookmark the permalink.