Deploy SCCM Application with PowerShell

It has been a while since the last time I blogged. In this post I will show the last step in my PowerShell script thats create and deploy an application in SCCM 2012. The steps of the script are

  • Create Application SCCM 2012 XML
  • Import XML into SCCM 2012
  • Create Collection
  • Create Collection Rule
  • Distribute Content to Distribution group
  • Deploy Application to collection

I will explain the last step of this script. Altough it is a simple step it was hard to discover how it worked. In 2012 deploying an application is actually assigning an application to a collection. It therefore uses the wmi class SMS_ApplicationAssignment. To deploy an application al you have to do is to create an instance, fill it with values en save it. An example is shown below

$ApplicationAssignmentClass = [wmiclass] "\\$($SccmServer.Machine)\$($SccmServer.Namespace):SMS_ApplicationAssignment"
        $newApplicationAssingment = $ApplicationAssignmentClass.CreateInstance()
        $newApplicationAssingment.ApplicationName = "Microsoft Office 2013 NL"
        $newApplicationAssingment.AssignmentName = "Deploy Microsoft Office 2013" 
        $newApplicationAssingment.AssignedCIs                 = $CI_ID
        $newApplicationAssingment.CollectionName                  = $ColllectionName
        $newApplicationAssingment.CreationTime                    = "20120101120000.000000+***" 
        $newApplicationAssingment.LocaleID                        = 1043
        $newApplicationAssingment.SourceSite                      = "S01
        $newApplicationAssingment.StartTime                       = "20120101120000.000000+***" 
        $newApplicationAssingment.SuppressReboot                  = $true
        $newApplicationAssingment.NotifyUser                      = $true
        $newApplicationAssingment.TargetCollectionID              = $CollectionID 
                
        $newApplicationAssingment.OfferTypeID = 2
        $newApplicationAssingment.WoLEnabled  = $false
        $newApplicationAssingment.RebootOutsideOfServiceWindows = $false
        $newApplicationAssingment.OverrideServiceWindows  = $false
        $newApplicationAssingment.UseGMTTimes = $true
        [void] $newApplicationAssingment.Put()

System Center 2012 Configuration Manager SDK

Two months ago Microsoft released the System Center 2012 suite. And now they have released the SDK for Configuration Manager (). It’s a different approach then the consumer products, where Microsoft showed a nice presentation about the features of Windows 8 for developers.
Continue reading System Center 2012 Configuration Manager SDK

Creating an SCCM 2012 Application with PowerShell

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.