NOTE: You MUST have the ConfigMgr Admin Console installed locally as the module requires access to many of the assemblies that are installed with the console.

For those of you that don’t enjoy copying and pasting code off a web page (don’t blame you), I’ve uploaded a sample script with all of the code below. Start plugging in your values and get to creating applications! Head on over to the Downloads Section and grab it now!

First off we need to Import the ConfigMgr2012 Module and the SCCM Assemblies.

Import-Module ConfigMgr2012

Next up will be to define our variables. I’ve listed out most of what would be typically used. Use Get-Help or use IntelliSense to see a listing of all potential variables for each module function.

# Define SCCM Server Variables
$sccmServer = "Server1"  # Replace with your SCCM Site Server (CAS or Primary)
$sccmSiteCode = "CAS"	# Replace with your SCCM Site Code (CAS or Primary)
$authScopeID = Get-SCCMAuthoringScopeID -SCCMServer $sccmServer -SCCMSiteCode $sccmSiteCode

# Define Application Variables
$applicationName = "Test Application"
$applicationVersion = "1.0"
$applicationMfr = "IT"
$applicationDescription = "This is an Application"

# Define Deployment Type Variables
$deploymentTypeName = "Test Deployment Type"
$deploymentTypeDescription = "This is a Deployment Type"
$deploymentTypeContentSource = "\\server\source\app"  # Path to the Source Files
$msiProductCode = "{14506a41-b616-43ae-bc06-38633d557987}"  # That's a random GUID.  Use the actual Product Code here
$msiVersion = ""
$executeTime = "15" # in Minutes
$maxExecuteTime = "15" # in Minutes
$installCmdLine = "setup.msi /q"
$uninstallCmdLine = "MsiExec.exe /x $msiProductCode /q"

Now that we have all of our variables assigned with their appropriate values, the “real” work can begin. We start off by creating an Application Object. This object will hold all of the properties of the Application. (NOTE: Beware of line wrapping. This normally can fit all on a single line)

# Create a new Application Object
$appObject = New-SCCMApplicationObject -authScope $authScopeID -AutoInstallTaskSequence -AppName $applicationName -Description $applicationDescription -Version $applicationVersion -Mfr $applicationMfr -ReleaseDate ($(Get-Date).ToShortDateString()) -AutoDistribute -DownloadDelta -Verbose

After we have our Application Object ($appObject), we need to create our Deployment Type Object. This object will later be added to the Application Object. (Again, watch the line wrapping)

# Create a new Deployment Type Object
$deploymentTypeObject = New-SCCMDeploymentType -authScope $authScopeID -InstallerType MSI -DeploymentTypeName $deploymentTypeName -Description $deploymentTypeDescription -ContentSource $deploymentTypeContentSource -ExecuteTime $executeTime -MaxExecuteTime $maxExecuteTime -InstallCmdLine $installCmdLine -UninstallCmdLine $uninstallCmdLine -MSIProductCode $msiProductCode -MSIProductVersion $msiVersion -DetectionMethod ProductCode -AllowUninstall -PeerCache -InstallBehavior System -UserInteraction Normal -RequiresElevatedRights -PostInstallBehavior BasedOnExitCode -Verbose

At this point we have a pretty basic Deployment Type all of the basics covered. But let’s take it one step further and add in some custom Return Codes.

# Add Return Codes for Deployment Type
# NOTE: Standard MSI-Based Return Codes are generated automatically.  Use this method to add in custom return codes
$deploymentTypeObject.Installer.ExitCodes.Add($(New-SCCMDeploymentTypeReturnCode -ExitCode 1 -ExitClass Failure -Name "FAILURE" -Description ""))
$deploymentTypeObject.Installer.ExitCodes.Add($(New-SCCMDeploymentTypeReturnCode -ExitCode 2 -ExitClass SoftReboot -Name "REBOOT_REQUIRED" -Description ""))
$deploymentTypeObject.Installer.ExitCodes.Add($(New-SCCMDeploymentTypeReturnCode -ExitCode 3 -ExitClass HardReboot -Name "REBOOTING_SYSTEM" -Description ""))

As you can see from the above code, we are adding these directly to the Deployment Type Object Installer.ExitCodes Property. After making all of our additional modifications to the Deployment Type Object, we need to add it back to the Application Object (told you we’d get to it).

# Add Deployment Type Object to Application Object
Add-SCCMDeploymentType -AppObject $appObject -DeploymentTypeObject $deploymentTypeObject -Verbose

Now, if you are following along in the Console or ISE, take a look at $appObject.DeploymentTypes0. You’ll be able to see all of the properties we previously created for the Deployment Type Object! Pretty cool huh? Ok, moving on.
Now that we have our Application Object, complete with a new Deployment Type, we need to save this puppy back to SCCM so we can actually USE it. First things first, we must Serialize the Application Object back to a string so that ConfigMgr can consume it.

# Save (Serialize) the Application Object back to an XML String
$xml = Save-SCCMApplicationObject -AppObject $appObject -Verbose

Now, you may be wondering WHY I’m saving it to an object labeled $xml. Well, if you actually looked at the output of the Save-SCCMApplicationObject function, it is an XML File, all rolled up into a single (rather long) string.
Ok, so we’ve serialized our Application, now we need to actually create our new Application. But, I thought we already did that? Nope, we created an OBJECT to hold all of the Application Metadata. Now we will create the actual Application using all that metadata we generated earlier.

# Create the new SCCM Application
New-SCCMApplication -ServerName $SCCMServer -SiteCode $SCCMSiteCode -xml $xml -Verbose

And there you have it. If you refresh your console window you should have a shiny new application, complete with deployment type.

Last edited Apr 25, 2014 at 4:04 AM by dhedges01, version 2