CM07 and PowerShell

Written by Stephen Jesok.

Here is stripped down powershell script that will setup an advertisement tied to a collection based on an AD group (It creates all 3). The SCCM parts are in bold. I found the PowerShell module out at http://www.snowland.se/sccm-posh/. It and a MDT module are in the attached zip.

Param(
[ValidateSet("UAT","TST","MAC")][String]$Area = "TST",
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][String]$AppVendor = $(throw "Required param AppVendor required."),
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][String]$AppName = $(throw "Required param AppName required."),
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][int]$AppVer = $(throw "Required param AppVer required."),
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][String]$PackageID = $(throw "Required param PacakgeID required."),
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][String]$InstallProgram = $(throw "Required param InstallProgram required."),
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][String]$UninstallProgram = $(throw "Required param UninstallProgram required.")
)

$myGroupI = "SCCM-$Area-SP-$AppVendor$AppName-$AppVer-Install"
$myGroupU = "SCCM-$Area-SP-$AppVendor$AppName-$AppVer-Uninstall"
$myCollectionI = "$Area-SP-$AppVendor$AppName-$AppVer-Install"
$myCollectionU = "$Area-SP-$AppVendor$AppName-$AppVer-Uninstall"
$myAdvertiseI = "$AppVendor $AppName $AppVer Install"
$myAdvertiseU = "$AppVendor $AppName $AppVer Uninstall"

Write-Host "===================================================

The following components will be created.

Install AD Group: $myGroupI
Uninstall AD Group: $myGroupU
Install Collection: $myCollectionI
Uninstall Collection: $MyCollectionU
Install Advertisement: $myAdvertiseI
Uninstall Adverisement: $myAdvertiseU

Package ID: $PackageID
Install: $InstallProgram
Uninstall: $UninstallProgram

===================================================


Do you want to continue? [y/n]
"

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp")

if ($x.character -ne "y") {Exit}

Import-Module ActiveDirectory
Import-Module c:\tools\ps\SCCM-Commands.psm1
Write-Host "Creating components"

#Create AD Groups
Write-Host "Creating Active Directory groups."

New-ADGroup -Name $myGroupI -GroupScope Global -GroupCategory Security -Path "OU=SoftwareProvisioning,OU=SCCM,OU=INFRASTRUCTURE,DC=my,DC=acme,DC=com" -Description "Managed by This email address is being protected from spambots. You need JavaScript enabled to view it." -ManagedBy "CN=SCCM-SoftwareProvisioning,OU=GROUPS,OU=SCCM,OU=INFRASTRUCTURE,DC=my,DC=acme,DC=com"
New-ADGroup -Name $myGroupU -GroupScope Global -GroupCategory Security -Path "OU=SoftwareProvisioning,OU=SCCM,OU=INFRASTRUCTURE,DC=my,DC=acme,DC=com" -Description "Managed by This email address is being protected from spambots. You need JavaScript enabled to view it." -ManagedBy "CN=SCCM-SoftwareProvisioning,OU=GROUPS,OU=SCCM,OU=INFRASTRUCTURE,DC=my,DC=acme,DC=com"

#Connect to SCCM Server
Write-Host "Connecting to SCCM Site Server"
$sccm = Connect-SCCMServer -Hostname SCCMServer.my.acme.com -Verbose

# Create a new collection with a collection rule
# Parent Collcetion MAC000D4

Write-Host "Creating Install Collection"
$newICollection = New-SCCMCollection -SccmServer $sccm -name $myCollectionI -comment "SCCM SP Collection" -refreshHours 1 -parentCollectionID "MAC000D4" -Verbose
Write-Host "Created collection: " $newiCollection.CollectionID

Write-Host "Adding AD Group Membership Rule"
$sccmInstallQuery = "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.SystemGroupName = 'ACME\\$myGroupI'"
Add-SCCMCollectionRule -SCCMServer $sccm -collectionID $newICollection.CollectionID -queryExpression $sccmInstallQuery -queryRuleName "AD Group $myGroupI" -Verbose

Write-Host "Creating Uninstall Collection"
$newUCollection = New-SCCMCollection -SccmServer $sccm -name $myCollectionU -comment "SCCM SP Collection" -refreshHours 1 -parentCollectionID "MAC000D4" -Verbose
Write-Host "Created collection: " $newUCollection.CollectionID

Write-Host "Adding AD Group Membership Rule"
$sccmUninstallQuery = "select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.SystemGroupName = 'ACME\\$myGroupU'"
Add-SCCMCollectionRule -SCCMServer $sccm -collectionID $newUCollection.CollectionID -queryExpression $sccmUninstallQuery -queryRuleName "AD Group $myGroupU" -Verbose

Write-Host "Creating Install Advertisement"
New-SCCMAdvertisement -SCCMServer $sccm -AdvertisementName $myAdvertiseI -CollectionID $newICollection.CollectionID -PackageID $PackageID -ProgramName $InstallProgram -Download -verbose -debug

Write-Host "Creating Uninstall Advertisement"
New-SCCMAdvertisement -SCCMServer $sccm -AdvertisementName $myAdvertiseU -CollectionID $newUCollection.CollectionID -PackageID $PackageID -ProgramName $UninstallProgram -Download -verbose -debug

MNSCUG Powered by Office 365