Schedule and Monitor ConfigMgr Package Distributions with PowerShell

Update! (22-Nov-2014) Updated the script to include a prompt for which distribution point group to use and a couple of minor improvements

Have you ever wanted to have the capability of scheduling a package distribution in ConfigMgr to occur at a specific date / time? Or perhaps you want to monitor your package distributions an receive an email when it has completed? Well now you can!

As a ConfigMgr administrator, I sometimes want to schedule my package distributions to take place outside of office hours, especially with very large packages like windows images.  I don’t want to consume a lot of bandwidth during working hours, so I prefer to start them at a time when the bandwidth I consume has the least impact on the sites I am distributing from and to. Sure I can use bandwidth limiting on the distribution point, but I don’t want to restrict bandwidth for all my distributions during working hours, only the really big ones, so out of hours is a better option.

Additionally I want to monitor my distributions to see when they have completed and how long they take.  There is a nice tool in the ConfigMgr 2012 R2 toolkit called ‘Distribution Point Job Queue Manager’, but there is no facility to receive a notification when a distribution has completed. Instead you must continually refresh it, which I can’t do if I’m scheduling a distribution out of hours.

To meet this need, I prepared a PowerShell script available from the Technet Gallery that will do either of the following things:

  1. Schedule the distribution of a new ConfigMgr package (equivalent to ‘Distribute Content‘ in the ConfigMgr console), monitor it, and send an email when the distributions have finished.

  2. Schedule the update of a package content to a distribution point group (equivalent to ‘Update Distribution Points‘ in the ConfigMgr Console), monitor it, and send an email when the distributions have finished.

  3. Monitor an already-running distribution and send an email when the distributions have finished.

The script will work for any package type, eg standard package, driver package, task sequence packages, software update packages, application content packages, OS images, boot images, OS installer packages.

It works by creating a ‘scheduled job’ in PowerShell, which will start at the time you specify.  The scheduled job is a PowerShell script that will start the package distribution, monitor the status of the distributions in WMI, then send an email when all the active distributions for that package are complete.  It will also email you if a distribution fails, although ConfigMgr will retry a failed distribution up to 99 times, so it will continue to monitor it for completion.

Update Distribution Points Example

Let’s take a look at an example.   In this case I’ve updated the source files for a deployment type called ‘Uninstall Java‘ in my ‘Java‘ application, and I want to update the distribution points with the new content.

Let’s run the script.  The first thing it asks for is what I want to do.  If I’ve created a new package or application and I want to distribute it to my distribution point group, I choose option [1].  If I’ve updated source files for an existing package or application and I want to update the distribution points with the new content, I choose [2].  If I’ve already started a package distribution and I simply want to monitor it, I choose [3].  In this case, i’m choosing [2].

1

Now it asks me for an email address to send the distribution monitor email notifications to.

2

Next I can enter the date and time that I want the package distribution to start.  You can enter enter any date and/or time format that your regional settings will understand.  For example, I can enter 22:00 to start my distribution today at 10pm.  Or I can enter 25/12/14  15:00 to start my distribution at 3pm on Christmas day in the UK time zone.  Pretty sure that won’t impact the network!  If I want to distribute the package now, I simply hit Enter, and it will be scheduled in 3 minutes from now.

3

Next it asks me for the name of the package or application I want to distribute.  It will then check the ConfigMgr WMI to validate whether a package or application exists with that name, and what type of package it is.

4

Since it is an application I must also enter which deployment type I want to distribute content for:

5

Great, now my distribution job has been scheduled and will run at the set time.

6

Let’s see what email notifications we get.  First, we get a notification that the distribution has started:

email1

Sometimes a distribution to one or more of the distribution points may fail the first time.  If it does, ConfigMgr will retry it up to 99 more times before giving up.  The script will continue to monitor the distribution, but we will get a notification that one or more the distributions has failed, and will be retried.  This is good to know as the distribution will likely take longer than usual because of a failure.

email2

Once the distribution has completed on all distribution points, we get an email summarizing the distribution.

email3Package Distribution Monitor Example

Let’s look at another example.  in this case, I’ve already distributed content for a package called ‘Retain Corporate Client‘, and I want to monitor the distribution for its completion.

In this case, I’m asked similar questions to before, but then it will tell me what package distributions are currently active, and ask me which one I want to monitor.  There is only one at the moment, and I enter the Package ID that it gives me.  Then my monitor job is scheduled to run in 2 minutes from now.

7

I get much the same emails as before:

email4

email5

Configuring the Script

To run the script, you should enter the following email variables at the top of the script.  These will probably not change so you can just enter them one time and that’s it.

Capture

The script must also be run as administrator.  This is required to create scheduled jobs.  If you are not running it as administrator, you will be notified.

Enjoy 🙂

 

Re-Triggering a ConfigMgr Application Install

Ok, so let’s say you’ve deployed an Application in ConfigMgr, but you’re testing the Application – making sure that it works, that it installs correctly.  If it does, great.  But then you make some changes on the target client and you want to install it again to make sure that it still works, without making any changes to the Application itself.

For testing installations, I usually make Available deployments so that I can run them again if they fail.  But if the Application has installed correctly and you want to run it again, you can’t do that from the Software Center, as the Application will only have the option to Uninstall.

I guess the long way is to remove it from the targeted collection, refresh the policy on the client, add it to the collection again, refresh the client policy again.

But surely there must be something quicker?  Indeed there is 🙂  Using WMI, you can simply call the Install method for the Application in the CCM_Application class.

Here’s how with PowerShell:


$ComputerName = "PC001"
$AppName = "Microsoft Office 2013 Pro"

$s = New-PSSession -ComputerName $ComputerName
Invoke-Command -Session $s -Argu $ComputerName,$AppName -ScriptBlock `
{
param ($ComputerName,$AppName)
write-host "Getting Parameters for '$AppName' on $ComputerName"
$App = Get-WmiObject -computername $ComputerName -Namespace "root\ccm\ClientSDK" -Class CCM_Application | where {$_.Name -like "$AppName"} | Select-Object Id, Revision, IsMachineTarget
$AppID = $App.Id
$AppRev = $App.Revision
$AppTarget = $App.IsMachineTarget
write-host $AppID, $AppRev, $AppTarget -ForegroundColor Yellow
write-host "Triggering Installation!" -ForegroundColor Green
([wmiclass]'ROOT\ccm\ClientSdk:CCM_Application').Install($AppID, $AppRev, $AppTarget, 0, 'Normal', $False) | Out-Null
}
Remove-PSSession $s