I’ve been meaning to create a script that will create applications in ConfigMgr 2012 for a while now. Instead of using the ConfigMgr 2012 PowerShell cmdlets I wanted to try to do it the hard way. When looking around on the web I found out that the basic functionality on how to create an application is provided in the Package Conversion Manager (found here). I set out to try and use some parts from PCM code to see how it would go. And to my amazement, it worked!

In this post I’ll provide you with what I put together from the PCM code. I’ll not take any credits, that goes to the PCM team. Basically the script below creates an application with a MSI deployment type and sets some configurations. My next mission is to create a script out of this that accepts parameters and will essentially replace the New-CMApplication cmdlet for me.

What does it do

Creates an application in ConfigMgr 2012 named TestApp (that’s easily changed of course) with a MSI Installer deployment type. The detection method is set to the product code (as of now fake, see note below).

The script

Note! Change the variables under the Variables comment if you’d like to use this in a lab environment. As of now, this script generates a fake Product Code for the detection method. If you’d to use this script in a production environment, you’d have to get the Product Code out of the MSI and replace it in the script.

[System.Reflection.Assembly]::LoadFrom((Join-Path (Get-Item $env:SMS_ADMIN_UI_PATH).Parent.FullName "Microsoft.ConfigurationManagement.ApplicationManagement.dll")) | Out-Null
[System.Reflection.Assembly]::LoadFrom((Join-Path (Get-Item $env:SMS_ADMIN_UI_PATH).Parent.FullName "Microsoft.ConfigurationManagement.ApplicationManagement.MsiInstaller.dll")) | Out-Null

# Variables
$SiteServer = "CM01.contoso.com"
$SiteCode = "PS1"
$ContentSourcePath = "\\CM01\ContentLibrary$\Software\TestApp"
$ApplicationTitle = "TestApp"
$ApplicationVersion = 1.0
$ApplicationSoftwareVersion = "1.0"
$ApplicationLanguage = (Get-Culture).Name
$ApplicationDescription = "Test description"
$ApplicationPublisher = "TestCorp"
$DeploymentInstallCommandLine = "setup.exe"
$DeploymentUninstallCommandLine = "setup.exe /uninstall"

# Get ScopeID
$GetIdentification = [WmiClass]"\\$($SiteServer)\root\SMS\Site_$($SiteCode):SMS_Identification"
$ScopeID = "ScopeId_" + $GetIdentification.GetSiteID().SiteID -replace "{","" -replace "}",""

# Create unique ID for application and deployment type
$ApplicationID = "APP_" + [GUID]::NewGuid().ToString()
$DeploymentTypeID = "DEP_" + [GUID]::NewGuid().ToString()

# Create application objects
$ObjectApplicationID = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.ObjectId($ScopeID,$ApplicationID)
$ObjectDeploymentTypeID = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.ObjectId($ScopeID,$DeploymentTypeID)
$ObjectApplication = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.Application($ObjectApplicationID)
$ObjectDeploymentType = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.DeploymentType($ObjectDeploymentTypeID,"MSI")

# Add content to the Application
$ApplicationContent = [Microsoft.ConfigurationManagement.ApplicationManagement.ContentImporter]::CreateContentFromFolder($ContentSourcePath)
$ApplicationContent.OnSlowNetwork = [Microsoft.ConfigurationManagement.ApplicationManagement.ContentHandlingMode]::DoNothing
$ApplicationContent.OnFastNetwork = [Microsoft.ConfigurationManagement.ApplicationManagement.ContentHandlingMode]::Download

# Application information
$ObjectDisplayInfo = New-Object Microsoft.ConfigurationManagement.ApplicationManagement.AppDisplayInfo
$ObjectDisplayInfo.Language = $ApplicationLanguage
$ObjectDisplayInfo.Title = $ApplicationTitle
$ObjectDisplayInfo.Description = $ApplicationDescription
$ObjectApplication.DisplayInfo.Add($ObjectDisplayInfo)
$ObjectApplication.DisplayInfo.DefaultLanguage = $ApplicationLanguage
$ObjectApplication.Title = $ApplicationTitle
$ObjectApplication.Version = $ApplicationVersion
$ObjectApplication.SoftwareVersion = $ApplicationSoftwareVersion
$ObjectApplication.Description = $ApplicationDescription
$ObjectApplication.Publisher = $ApplicationPublisher

# DeploymentType configuration
$ObjectDeploymentType.Title = $ApplicationTitle
$ObjectDeploymentType.Version = $ApplicationVersion
$ObjectDeploymentType.Enabled = $true
$ObjectDeploymentType.Description = $ApplicationDescription
$ObjectDeploymentType.Installer.Contents.Add($ApplicationContent)
$ObjectDeploymentType.Installer.InstallCommandLine = $DeploymentInstallCommandLine
$ObjectDeploymentType.Installer.UninstallCommandLine = $DeploymentUninstallCommandLine
$ObjectDeploymentType.Installer.ProductCode = "{" + [GUID]::NewGuid().ToString() + "}"
$ObjectDeploymentType.Installer.DetectionMethod = [Microsoft.ConfigurationManagement.ApplicationManagement.DetectionMethod]::ProductCode

# Add DeploymentType to Application
$ObjectApplication.DeploymentTypes.Add($ObjectDeploymentType)

# Serialize the Application
$ApplicationXML = [Microsoft.ConfigurationManagement.ApplicationManagement.Serialization.SccmSerializer]::SerializeToString($ObjectApplication)
$ApplicationClass = [WmiClass]"\\$($SiteServer)\root\SMS\Site_$($SiteCode):SMS_Application"
$ObjectApplication = $ApplicationClass.CreateInstance()
$ObjectApplication.SDMPackageXML = $ApplicationXML
$Temp = $ObjectApplication.Put()
$ObjectApplication.Get()

Creating an application by using the script

1. Save the script as New-CMApplication.ps1 (or whatever you like) and open a PowerShell console.
2. Browse to the folder you’ve saved the script in and run the following command:

.\New-CMApplication.ps1

3. After a while (took about 10 seconds in my lab environment), the application will have been created.
4. Check in the ConfigMgr console to see the application.

71_1

Below is some pictures of the TestApp and it’s settings:

71_2

71_3

71_4

71_5

71_6

71_7

Really cool!

Nickolaj Andersen
Principal Consultant and Enterprise Mobility MVP. Nickolaj has been in the IT industry for the past 10 years specializing in Enterprise Mobility and Security, Windows deployments and Automation. In 2015 Nickolaj was awarded as PowerShell Hero by the community for his script and tools contributions. Author of ConfigMgr Prerequisites Tool, ConfigMgr OSD FrontEnd, ConfigMgr WebService and a frequent speaker at user groups.

(809)

comments
  • André J.
    Posted at 08:52 April 11, 2014
    André J.
    Reply
    Author

    Hi,
    this script is very cool but i would like to optimize it for me.
    Can you tell me how i can change the detection rule? i would like to work with registry key online and i don´t know how i can change it?

    thx a lot
    andré

  • Hitesh Pandya
    Posted at 19:53 April 25, 2014
    Hitesh Pandya
    Reply
    Author

    I have tried creating registry detection rule but the dcomobject.dll file has a bug in it. RegistrySetting constructor is not part of this DLL and so it didn’t work for me using PowerShell. However, it seems to work if you use C#. More information can be found at http://blog.lechar.nl/2012/10/01/71/
    Hope this helps.
    Hitesh

    • Nickolaj
      Posted at 20:55 April 25, 2014
      Nickolaj
      Reply
      Author

      Thanks for sharing your findings Hitesh!

      Regards,
      Nickolaj

  • Skatterbrainz
    Posted at 14:34 March 18, 2015
    Skatterbrainz
    Reply
    Author

    Very nice! Thank you for sharing this!

    • Nickolaj
      Posted at 15:18 March 18, 2015
      Nickolaj
      Reply
      Author

      Thank you, I hope it helps!

      Regards,
      Nickolaj

    • Nickolaj
      Posted at 12:18 March 20, 2015
      Nickolaj
      Reply
      Author

      You’re very welcome! I’m glad that you like it.

      Regards,
      Nickolaj

  • Skatterbrainz
    Posted at 20:15 March 18, 2015
    Skatterbrainz
    Reply
    Author

    I’m really finding this to be helpful, so I hate to ask “more?”. Do you have any tips for modifying some additional Deployment Type parameters such as those found on the “Content” and “User Experience” tabs of the console form? For example, we abide by a deployment policy that dictates setting the “Installation behavior” to “Install for System”, Logon requirement to “Whether or not a user is logged on” and Installation program visibility to “Hidden”. On the Content tab, we often check the box for “Allow clients to use a fallback source location for content” and set the bottom drop-down to “Download content…”. Are these possible using PowerShell? I’ve been poking around in each of the variables using the Intellisense lists and can’t seem to find these hiding anywhere.

    • Nickolaj
      Posted at 14:53 March 23, 2015
      Nickolaj
      Reply
      Author

      Hi Skatterbrainz,

      Sorry, I missed this comment when I answered a bunch of others. If you drop me an email, I can send you two scripts that further modifies the Deployment Type. I’m not sure that I’ve blogged those scripts yet, hence why I can only email them.

      Regards,
      Nickolaj

      • Jarko
        Posted at 10:29 March 24, 2015
        Jarko
        Reply
        Author

        Hello Nickolaj, if you don’t mind I would like to have a look at those Deployment Type scripts 🙂

        Regards Jarko

      • Skatterbrainz
        Posted at 23:23 March 24, 2015
        Skatterbrainz
        Reply
        Author

        No worries. Thank you! ds0934 @ gmail.com

        • lydellscooby
          Posted at 20:48 June 13, 2016
          lydellscooby
          Reply
          Author

          Hello Nickolaj and Skatterbrainza. This thread is a little older but can a copy of this also be sent to lydellscooby @ yahoo

  • Skatterbrainz
    Posted at 14:42 March 23, 2015
    Skatterbrainz
    Reply
    Author

    Sorry. I guess that was way too loaded of a question.

  • Carlos
    Posted at 10:26 April 4, 2015
    Carlos
    Reply
    Author

    Hello Nickolaj, Thank you so much for posting this excellent script. It takes the tediousness out of creating applications via the GUI. I am in the process of creating about 75 applications and this script is a gem.
    May I ask if you would email me the 2 scripts to further modify the deployment types? My account is cmi5424@yahoo.com.

    Much Appreciated

    Carlos

  • Navaneeth
    Posted at 14:47 September 2, 2015
    Navaneeth
    Reply
    Author

    Great work. Could you please send me the other scripts.

    • Nickolaj
      Posted at 13:54 September 7, 2015
      Nickolaj
      Reply
      Author

      Hi Navaneeth,

      Send me an email to the address specified on the Contact page, and I’ll reply back.

      Regards,
      Nickolaj

  • Carlos
    Posted at 19:14 September 9, 2015
    Carlos
    Reply
    Author

    Hello Nickolaj,

    Excellent, could you send me the 2 scripts to my Cmi5424@yahoo.com

    Thank you once again.
    Carlos

  • John
    Posted at 20:11 September 10, 2015
    John
    Reply
    Author

    Hi, that’s an amazing script! I was wondering is there a way to select the option “this MSI product code ……and the following condition must be met to indicate presence of application”. I was hoping to select these options below. Thanks so much sir.

    MSI Property – “Version”
    Operator – “Greater than or equal to”
    Value – “2.5.1”

  • Harrison
    Posted at 17:52 September 11, 2015
    Harrison
    Reply
    Author

    Awesome script! can u send me the other scripts too 🙂

  • JP
    Posted at 05:36 April 29, 2016
    JP
    Reply
    Author

    Excellent work you have done. Thank you for the script. BTW, I also would like to receive your two Deployment Type scripts.

  • golfusmc
    Posted at 17:31 July 25, 2016
    golfusmc
    Reply
    Author

    Nickolaj,
    Thanks for the information and great work. I am trying to so the same thing using ” ConvertTo-CMApplication and unable to add a second EnhancedDetectionMethod clause to the application. Any thoughts? I would also like to check out your other 2 scripts as well. Thanks in advance.

  • GOLFUSMC
    Posted at 18:47 July 25, 2016
    GOLFUSMC
    Reply
    Author

    Nickolaj,

    Thank you for the information. I am trying to do the same thing using “ConvertTo-CMApplication” and adding a second Enhanced Detection Method. I would also like to see the other two scripts you have.

    Thanks.

    • Ludovic
      Posted at 15:06 November 10, 2016
      Ludovic
      Reply
      Author

      Hello GOLFUSMC

      Have you found how to add a second enhanced detection method please ?
      I’m trying to find a solution but nobody knows…

      Thank you !

  • rajesh kawle
    Posted at 09:58 February 19, 2017
    rajesh kawle
    Reply
    Author

    Nickolaj,

    This script is really awesome and helped me a lot with some modification to add operating systems. can you please also send me the scripts to make further modification in deployments.
    I am looking to add contact details and change in user experience settings.
    Rajeshkawleus@gmail.com

  • fredyy
    Posted at 23:29 June 25, 2017
    fredyy
    Reply
    Author

    Hello
    thank you for all
    I have a question
    I retrieved the information from an application with the command Get-Cmapplication – name “****”
    After I deleted this Application (****)
    How to remix this application (****) in production with powershell

    I’m really stuck on this subject
    Thank you for your help

  • Leave a Reply