Skip to content
This repository has been archived by the owner on Nov 13, 2018. It is now read-only.

Example Script

andrebocchini edited this page Nov 23, 2012 · 1 revision

This is an example script that creates a package and advertises it automatically. I use something similar to automatically create test packages for pre-configured installers.

param (
    [parameter(Mandatory=$true)][ValidateNotNull()][string]$siteProvider,
    [parameter(Mandatory=$true)][ValidateNotNull()][string]$siteCode,
    [parameter(Mandatory=$true)][ValidateNotNull()][string]$testCollectionId,
    [parameter(Mandatory=$true)][ValidateNotNull()][int]$testAdvertisementsFolderId,
    [parameter(Mandatory=$true)][ValidateNotNull()][int]$testPackagesFolderId,
    [parameter(Mandatory=$true)][ValidateNotNull()][string]$packageName,
    [parameter(Mandatory=$true)][ValidateNotNull()][string]$packageVersion,
    [parameter(Mandatory=$true)][ValidateNotNull()][string]$packageManufacturer,
    [parameter(Mandatory=$true)][AllowEmptyString()][string]$packageLanguage,
    [parameter(Mandatory=$true)][AllowEmptyString()][string]$packageDescription,
    [parameter(Mandatory=$true)][AllowEmptyString()][string]$packageSourcePath,
    [parameter(Mandatory=$true)][ValidateNotNull()][string]$programName = "Silent install",
    [parameter(Mandatory=$true)][ValidateNotNull()][string]$programCommandLine
    )

Import-Module SCCM

Write-Progress -Activity "Packaging $packageName" -PercentComplete 16.66 -Status "Creating package name $packageName..."
$package = New-SCCMPackage `
                    -siteProvider $siteProvider `
                    -siteCode $siteCode `
                    -packageName $packageName `
                    -packageDescription $packageDescription `
                    -packageVersion $packageVersion `
                    -packageManufacturer $packageManufacturer `
                    -packageLanguage $packageLanguage `
                    -packageSource $packageSourcePath

Write-Progress -Activity "Packaging $packageName" -PercentComplete 33.32 -Status "Creating program named $programName..."
$program = New-SCCMProgram `
                    -siteProvider $siteProvider `
                    -siteCode $siteCode `
                    -packageId $package.PackageID `
                    -programName $programName `
                    -programCommandLine $programCommandLine

# These flags set the program to install only when no user is logged on.
$program.ProgramFlags = 135438336
Save-SCCMProgram $program

Write-Progress -Activity "Packaging $packageName" -PercentComplete 49.98 -Status "Getting a list of distribution points..."
$targetDistributionPoints = @()
$allDistributionPoints = Get-SCCMDistributionPoints -siteProvider $siteProvider -siteCode $siteCode
foreach($distributionPoint in $allDistributionPoints) {
    # We want to avoid putting our package on PXE distribution points
    if(!($($distributionPoint.NALPath).Contains("PXE"))) {
        $targetDistributionPoints += $distributionPoint
    }
}
Write-Progress -Activity "Packaging $packageName" -PercentComplete 66.64 -Status "Adding package to distribution points..."
Add-SCCMPackageToDistributionPoint `
                    -siteProvider $siteProvider `
                    -siteCode $siteCode `
                    -packageId $package.packageID `
                    -distributionPointList $targetDistributionPoints

Write-Progress -Activity "Packaging $packageName" -PercentComplete 83.33 -Status "Advertising program..."
$advertisement = New-SCCMAdvertisement `
                    -siteProvider $siteProvider `
                    -siteCode $siteCode `
                    -advertisementName "Install $packageManufacturer $packageName $packageVersion" `
                    -collectionId $testCollectionId `
                    -packageId $package.PackageID `
                    -programName $program.ProgramName

Write-Progress -Activity "Packaging $packageName" -PercentComplete 91.68 -Status "Moving package to the correct folder..."
Move-SCCMPackageToFolder `
                    -siteProvider $siteProvider `
                    -siteCode $siteCode `
                    -packageId $package.PackageID `
                    -targetFolderNodeId $testPackagesFolderId

Write-Progress -Activity "Packaging $packageName" -PercentComplete 91.68 -Status "Moving advertisement to the correct folder..."
Move-SCCMAdvertisementToFolder `
                    -siteProvider $siteProvider `
                    -siteCode $siteCode `
                    -advertisementId $advertisement.advertisementID `
                    -targetFolderNodeId $testAdvertisementsFolderId
Clone this wiki locally