SCCM Mandatory Assignment with Powershell

I had to automate some steps in SCCM. Therefore I created a GUI based tool with powershell to import applications in SCCM. It also creates advertisements for each application. My customer requires mandatory assignment, based on the date and time in the advertisement of the application. I wrote the following code to create an advertisement with a mandatory assignment.
Some variables are required

To get the correct AdvertFlags, DeviceFlags and RemoteClientFlags, create an advertisement with your requirements and collect the settings from the WMI.

NOTE: First test the script in a test environment before running in production. Use at your own risk!

If you have a question or suggestion, leave a comment.