How to use Scripts Manager Triggers to Map Network Drives when a VPN is Connected
Prerequisites:
-
VPN used must be in the list of supported VPNs in the article below.
Which VPN Solutions are currently supported for use with Scripts Manager VPN Triggers?
Step 1 – Create a new policy (Set Launch Folder Windows in a Separate Process to Enabled) using Scripts & Triggers on the computer side, choose switched-mode like in the screenshot below.
Step 2 – 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".
# Set Launch Folder Windows in a Separate Process to Enabled
if((Test-Path -LiteralPath "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced") -ne $true) {
New-Item "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -force -ea SilentlyContinue
};
New-ItemProperty -LiteralPath 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced' -Name 'SeparateProcess' -Value 1 -PropertyType DWord -Force -ea SilentlyContinue;
Step 3 – Then click "Next" at the "On revert action" screen to skip that screen, then at the "Specify process mode" screen choose the "Once" option.
Step 4 – At the "Policy settings" screen give the policy a descriptive name then click "Finish.
Step 5 – Now create another policy (Map drives when VPN connects) using Scripts & Triggers on the computer side, choose switched-mode like in the screenshot below.
Step 6 – 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".
# Wait for DNS to settle after VPN connects
Start-Sleep -s 10
# Map G Drive
if (-not(get-psdrive -name "G" -ErrorAction SilentlyContinue)) {
New-PSDrive -name "G" -PSProvider FileSystem -Root \\Server\share1 -Persist
}
# Map H Drive
if (-not(get-psdrive -name "H" -ErrorAction SilentlyContinue)) {
New-PSDrive -name "H" -PSProvider FileSystem -Root \\Server\Share2 -Persist
}
Step 7 – Click "Next" at the "On revert action" screen to skip that screen, then at the "specify process mode" screen choose the "On trigger" option, then choose "VPN connect" from the drop down before clicking "Next" to continue.
Step 8 – At the "Trigger settings" screen enter an asterisk (wildcard, etc.) for the server name, then click "Next".
Step 9 – At the "Policy settings" screen give the policy a descriptive name then click "Finish.
NOTE: You should have two policies now:
Step 10 – Lastly, test the policy by logging into a computer, (or run gpupdate
if already
logged in) and then connect to a VPN as a user that should receive the policy. If everything works
you should see the network drives show up in File Explorer, you may need to click refresh if you had
File Explorer already open to update the window contents.
Step 11 – Optionally, create a new Scripts and Triggers policy that disconnects the drives when the VPN disconnects by using the script below and also changing the trigger to "VPN disconnect".
Step 12 – "On trigger" does not work with Revert action script which is why you need to create a new policy to disconnect the drives.