With SCCM 2007 it was easy to import a package with PowerShell. For now it is a little bit tricky to accomplish it. In this post I will try to explain how you can create an application in SCCM 2012. This has been tested with the Release Candidate 2.
To create an application with powershell first you have to install the Admin Console. And load assemblies in your powershell script.
function Import-SCCMAssemblies($SCCMAdminConsolePath){
$path = "$SCCMAdminConsolePath\Microsoft.ConfigurationManagement.ApplicationManagement.dll"
if (Test-Path $path) { $t = [System.Reflection.Assembly]::LoadFrom($path)}
$path = "$SCCMAdminConsolePath\Microsoft.ConfigurationManagement.ApplicationManagement.Extender.dll"
if (Test-Path $path) { $t = [System.Reflection.Assembly]::LoadFrom($path)}
$path = "$SCCMAdminConsolePath\Microsoft.ConfigurationManagement.ApplicationManagement..MsiInstallerdll"
if (Test-Path $path) { $t = [System.Reflection.Assembly]::LoadFrom($path)}
}
Next step is to get the ScopeID of you SCCM Site server. How to do this can be found in my previous blog
Now you can create an application XML file with the use of the assemblies we have loaded
#Loading Assemblies to create an SCCM 2012 application
Import-SCCMAssemblies "D:\Program Files\Microsoft Configuration Manager\AdminConsole\bin\"
#Get Authoring ScopeID
$scopeid = Get-AuthoringScopeID $con
#Create an unique id for the application and the deployment type
$newApplicationID = "Application_" + [guid]::NewGuid().ToString()
$newDeploymentTypeID = "DeploymentType_" + [guid]::NewGuid().ToString()
#Create SCCM 2012 object id for application and deploymenttyo
$newApplicationID = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.ObjectID($scopeid,$newApplicationID)
$newDeploymentTypeID = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.ObjectID($scopeid , $newDeploymentTypeID)
#Create all the objects neccessary for the creation of the application
$newApplication = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.Application($newApplicationID)
$newDeploymentType = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.DeploymentType($newDeploymentTypeID,"MSI")
$newDisplayInfo = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.AppDisplayInfo
$newApplicationContent = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.Content
$newContentFile = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.ContentFile
#Setting Display Info
$newDisplayInfo.Title = $Application.DisplayName
$newDisplayInfo.Language = $newApplication.DisplayInfo.DefaultLanguage
$newDisplayInfo.Description = $Application.DisplayName
$newDisplayInfo.Version = $Application.PRVersion
$newApplication.DisplayInfo.Add($newDisplayInfo)
#Setting default Language must be set and displayinfo must exists
$newApplication.DisplayInfo.DefaultLanguage = "en-US"
$newApplication.Title = $APP.Title
$newApplication.Version = 1
#Deployment Type msi installer will be used
$newDeploymentType.Title = "Deploy $($APP.DisplayName)"
$newDeploymentType.Version = 1
$newDeploymentType.Installer.ProductCode = $APP.ProductCode
$newDeploymentType.Installer.InstallCommandLine = "Msiexec /i $($APP.MSIName) /qb-! Reboot=ReallySuppress"
$newDeploymentType.Installer.InstallFolder ="\"
$newDeploymentType.Installer.UninstallCommandLine = "Msiexec /x $($APP.ProductCode) /qb-! Reboot=ReallySuppress"
#Add the msi as content to the application
#UPDATE: Add all content to the application
$newApplicationContent = [Microsoft.ConfigurationManagement.ApplicationManagement.ContentImporter]::CreateContentFromFolder($APPUNCPath)
#DELETE: $newContentFile.Name = $APP.MSIName
#DELETE: $newApplicationContent.Files.Add($newContentFile)
#DELETE: $newApplicationContent.Location = $APP.UNCPath
$newApplicationContent.OnSlowNetwork = "Download"
$newDeploymentType.Installer.Contents.Add($newApplicationContent )
$newApplication.DeploymentTypes.Add($newDeploymentType)
#Serialize the object to an xml file
$newApplicationXML = [Microsoft.ConfigurationManagement.ApplicationManagement.Serialization.SccmSerializer]::SerializeToSTring($newApplication,$true)
So now we have an XML file with the data of the application. In this example we use an msi file. It is also possible to use a script or executable. then you have to set the deployment type to Script. Next step is to import the xml file.
$applicationClass = [WMICLASS]"\\$($SccmServer.Machine)\$($SccmServer.Namespace):SMS_Application"
$newApplication = $applicationClass.createInstance()
$newApplication.SDMPackageXML = $newApplicationXML
$tmp = $newApplication.Put()
#Reload the application to get the daat
$newApplication.Get()
Now we have created an application in SCCM 2012 with the use of PowerShell. The next step is to distribute and deploy the application to a collection.