Creating SCCM2012 Packages with Powershell

This simple powershell script will create a package in SCCM2012 with a single standard program, and distribute it to your DP Group.  It uses the ConfigMgr native commands, and you can add/remove/change the parameters to customise the script for your own environment.

It does not create a deployment, but that’s easy enough to do, just add code like the following after the content distribution part:

Write-Host 'Deploying Application to Collection'
$Deployment = Start-CMPackageDeployment `
 -CollectionName MyCollection `
 -PackageName $Pkg `
 -StandardProgramName $Pkg `
 -DeployPurpose Required

Create Package

<#
-------------
CreatePackage v1
-------------

This script creates and distributes an SCCM2012 package with a single standard program

Tested on: SCCM2012R2

//Instructions//

Set the variables below as required
#>


###############
## Variables ##
###############

# ConfigMgr Site Code
   $SiteCode = "ABC"
# Name of the package
   $Pkg = "7zip"
# Location of Source files
   $Source = "\\MySCCMServer\PackageSource\7Zip"
# Installation command line
   $CommandLine = "setup.exe -s"
# Required amount of disk space
   $DiskSpace = "200"
# Units for Disk space requirement, eg KB, MB or GB
   $DiskUnit = "MB"
# Maximum allowed run time
   $Duration = "16"
# Program can run, eg "WhetherOrNotUserIsLoggedOn", "OnlyWhenNoUserIsLoggedOn", "OnlyWhenUserIsLoggedOn"
   $RunType = "WhetherOrNotUserIsLoggedOn"
# Run mode, eg "RunWithAdministrativeRights" or "RunWithUserRights"
   $RunMode = "RunWithAdministrativeRights"
# Allow user to interact?
   $UserInteraction = $False
# Allow program to be installed from task sequence?
   $EnableTS = $True
# Distribution Point Group to distribute content to
   $DPG = "All Distribution Points"


# Import ConfigMgr Module

Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
$Drive = $SiteCode + ':'
cd $Drive
cls
$ErrorActionPreference = "Inquire"


## Create the Package ##

write-host 'Creating Package'
$Package = New-CMPackage -Name $Pkg -Path $Source


## Create a Program

write-host 'Adding Program'
$Program = New-CMProgram `
 -PackageName $Pkg `
 -StandardProgramName $Pkg `
 -CommandLine $CommandLine `
 -DiskSpaceRequirement $DiskSpace `
 -DiskSpaceUnit $DiskUnit `
 -Duration $Duration `
 -ProgramRunType $RunType `
 -RunMode $RunMode `
 -UserInteraction $UserInteraction


## Update Program

write-host 'Updating Program'
$ProgramUpdate = Set-CMProgram `
 -Name $Pkg `
 -StandardProgramName $Pkg `
 -EnableTaskSequence $EnableTS


## Distribute Content to DPs ##

write-host 'Distributing Content to DPs'
Start-CMContentDistribution -PackageName $Pkg -DistributionPointGroupName $DPG

write-host 'Done!' -ForegroundColor Yellow

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s