Powershell–Schedule a SCCM Advertisement

One of the business units I work with needed to deploy an application using SCCM. So far so good right? Then they said that they only wanted it to run between 23:00 and 06:00. So now you are thinking about putting a Maintenance Window on the collection to accomplish the time restriction requirement right? Well, what about all of the other distributions (like Software Updates)? They wouldn’t run on the machines that are in the collection with the Maintenance Window on it. I guess we could go into each and every deployment and flag it to ignore maintenance windows, but that would cause even more headaches.

With a little help from PowerShell and SQL we can change the advertisement properties. Specifically we need to change the following:

Advertisement Start Time

Advertisement Expires

Mandatory Assignments

Example of what we need to accomplish:

Today the Advertisement has the following settings

Advertisement Start Time – 4/20/2012 23:00.

Advertisement Expires – 4/21/2012 06:00

Mandatory Assignments – 4/20/2012 23:00

Tomorrow the Advertisement will have the following settings

Advertisement Start Time – 4/21/2012 23:00.

Advertisement Expires – 4/22/2012 06:00

Mandatory Assignments – 4/21/2012 23:00

Well, I could go into the properties of the advertisement and change them, but that isn’t my style.

What I did was write a PowerShell script to change the above times and then created a SQL Job to run the PowerShell script on a schedule.