Prerequisites: PolicyPak version 2791 or higher must be used.
Create a new Scripts & Triggers policy on the computer side, choose switched mode like in the screenshot below.
At the “On apply action” screen select “PowerShell script” from the dropdown, then in the main text window, paste in the script below, check the option “Run script as user, then click “Next”.
At the “Policy settings” screen give the policy a descriptive name then click “Finish”.
Now create another policy (Map drives when VPN Connect Event ID is Detected) using Scripts & Triggers on the computer side, choose switched-mode like in the screenshot below.
At the “On apply action” screen select “PowerShell script” from the dropdown, then in the main text window, paste in the script below then change the drive mappings to match the settings needed for your environment, check the option “Run script as user, then click “Next”.
Before continuing Connect to the VPN then open the Windows application log and locate the successful VPN Connection event, take note of the Level, the source, and the Event ID number for that event as you will need them in the next step.
For this example I used an Azure Point-to-Site VPN connection, and the successful connection Event ID number is 20225
VPN disconnect example using Azure Point-to-Site VPN
Note: “On trigger” does not work with Revert action script which is why you need to create a new policy to disconnect the drives.