Creating a ConfigMgr Package with PowerShell

Hello all!

Some of you that know me know that I’ve started a new job (week 3) so I’ve been quiet recently as I settle in and try to make a good impression.  All going well so far I’m enjoying it and the people I work with are all nice so that’s great!   Recently I needed to create 15 new packages in SCCM so I decided to make it easier by writing a script for it that did all the hard work for me.

Nothing fancy here if you search about you’ll get lots of help with this as I did but hopefully this will make it easy for you.

First, in order for this to work you’re going to have to connect your PowerShell ISE to your ConfigMgr site.  As mentioned in a previous post you can either connect using the console option…

image

… or if you’ve got an add on that does it for you you can launch your ISE and run the add on.  As long as the configurationmanager module is loaded and you’re connected to your site you’re ready to go.

Here is the script I wrote

   1: $SourcesLocation = ""
   2: $PackageName = "" #Must be below 40 characters
   3: $PackageDescription = ""
   4: $PackageManufacturer = ""
   5: $PackageVersion = ""
   6: $SilentInstallCommand = ""
   7:  
   8: #Do my bidding PowerShell
   9: If ($PackageName.Length -lt 41) {
  10:     New-CMPackage -Name $PackageName -Description $PackageDescription -Manufacturer $PackageManufacturer -Version $PackageVersion -Path $SourcesLocation
  11:         Write-Progress -Activity "Creating Package" -PercentComplete 25
  12:     Set-CMPackage -Name $PackageName -DistributionPriority Normal
  13:         Write-Progress -Activity "Setting Package" -PercentComplete 60
  14:     New-CMProgram -PackageName $PackageName -StandardProgramName "$PackageName - Program" -CommandLine $SilentInstallCommand -ProgramRunType WhetherOrNotUserIsLoggedOn -RunMode RunWithAdministrativeRights
  15:         Write-Progress -Activity "Creating Program" -PercentComplete 95
  16:     }
  17:     Else {
  18:         Write-Warning "Your package name exceeds 40 characters, shorten and try again!"
  19:     }

Let me break this down for you piece by piece.

Firstly we set the variables.  We need to set six.  I always download my sources files and place them where they need to be first.  Nice neat folders structured in a readable manner so if you were to return to them in 18 months time, you wouldn’t be confused.  Once you have the files ready, be sure of the silent install command you need to run.   You could test this on a pilot box first if you have the time.  Once you have your source files and the silent install command you’re ready to start!

   1: $SourcesLocation = ""
   2: $PackageName = "" #Must be below 40 characters
   3: $PackageDescription = ""
   4: $PackageManufacturer = ""
   5: $PackageVersion = ""
   6: $SilentInstallCommand = ""

$SourcesLocation – set this to the folder share you have your source files.  UNC Path please!
$PackageName, $PackageDescription, $PackageManufacturer and $PackageVersion – Pretty self explanatory.  Remember to keep your package name under 40 characters (will explain later)
$SilentInstallCommand – example “msiexec.exe /i example.msi /qn”

Next we create a blank package using the above variables.  We give it a name.  My personal rule of thumb here is no more than 40 characters.  Why?  Well Because you also have to create a program for the package, and you also have to name to program so what I do is take the package name I chose (as long as its under 40 characters!) and I can append “- Program” on the end (which is ten characters) and the maximum limit you can use for a program name is….. 50 characters long.  So if you can keep your package name short and sweet but to the point the process can be automated neatly.  You don’t have to stick to this of course, you can call your program whatever you like you just have to be careful what you call your program as there is a character limit.  You could opt to add other logic into the script for your own program names but then you wouldn’t be doing it my way which would make it wrong. Right? ha.

So, creating a blank package we use this command:

   1: New-CMPackage -Name $PackageName -Description $PackageDescription -Manufacturer $PackageManufacturer -Version $PackageVersion -Path $SourcesLocation

We can use the command Set-CMPackage to set additional properties, I urge you to explore them and their differences.  In this instance I am only using the –DistributionPriority settings, but there are plenty of others to play around with.

   1: Set-CMPackage -Name $PackageName -DistributionPriority Normal

And finally we need to create a program to go with our package (to actually install the thing!).

   1: New-CMProgram -PackageName $PackageName -StandardProgramName "$PackageName - Program" -CommandLine $SilentInstallCommand -ProgramRunType WhetherOrNotUserIsLoggedOn -RunMode RunWithAdministrativeRights

Here we use New-CMProgram to create the program and set properties.  These properties can be quite important so again, explore the ones available to you from the command either within ISE or the TechNet link I’ve attached to the text.

I have used this code to create a package for 7Zip as an example. Here you can see the properties of my Package, and my Program:

Package

imageimage

Program

imageimage

Notice the name of the program is “7Zip v17.00 Beta – Program” and the properties of the program are set to how we want it.

Next steps?  Well you can add on a step to distribute this to a DP or DP Group.  Personally I don't like doing that at this stage I like to get all the packages and programs in first then roll them in one at a time so I can test deployments.

UPDATE: 14.06.17:   I’ve since discovered that this places the package in to the root of the packages folder.  In order to move that package to your folder the code would look like this:

   1: $Move = (Get-CMPackage -Name $PackageName)
   2: Move-CMObject -InputObject $Move -FolderPath LAB:\Package\Microsoft

Where LAB is your site code.

I hope this has been useful for you & thanks for reading.

Jonathan

Add comment