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.
Below is some pictures of the TestApp and it’s settings:
Really cool!
Hi Nickolaj,
Thanks for the great post!
I’m having an issue with the deployment type, when using your script.
I can’t get it to show the content path in the GUI (The field is blank). The content show fine when I query the information in WMI.
Any suggestions?
Kind regards
Soren
Great post!
The script seems to work fine, however it seems that the content source path of the deployment type isn’t showing in the GUI. The information is there when I query WMI.
Any suggestions?
Thank you ! I didn’t know we can use the DLL to create object in order to serialize an app
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
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.
[email protected]
Hi Rajesh,
I hope you could have the script to customize the deployment type properties.
Could you please share it with me @ [email protected]
It would be really appreciate.
Thanks,
R Raj
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.
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 !
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.
Excellent work you have done. Thank you for the script. BTW, I also would like to receive your two Deployment Type scripts.
Awesome script! can u send me the other scripts too 🙂
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”
Hello Nickolaj,
Excellent, could you send me the 2 scripts to my [email protected]
Thank you once again.
Carlos
Great work. Could you please send me the other scripts.
Hi Navaneeth,
Send me an email to the address specified on the Contact page, and I’ll reply back.
Regards,
Nickolaj
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 [email protected].
Much Appreciated
Carlos
Sorry. I guess that was way too loaded of a question.
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.
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
Hello Nickolaj, if you don’t mind I would like to have a look at those Deployment Type scripts 🙂
Regards Jarko
Hi Jarko,
This was an very old thread. and now i need these scripts if you could have it.
Kindly share to the mail [email protected].
it would be really apprecitable.
Thanks,
R Raj
No worries. Thank you! ds0934 @ gmail.com
Hello Nickolaj and Skatterbrainza. This thread is a little older but can a copy of this also be sent to lydellscooby @ yahoo
Hi Nickolaj,
Could you pls forward the script to [email protected]?
It would be really appreciate.
Thanks in advance.
Very nice! Thank you for sharing this!
Thank you, I hope it helps!
Regards,
Nickolaj
You’re very welcome! I’m glad that you like it.
Regards,
Nickolaj
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 https://blog.lechar.nl/2012/10/01/71/
Hope this helps.
Hitesh
Thanks for sharing your findings Hitesh!
Regards,
Nickolaj
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é