When covid hit, everyone scrambled to get a VPN connection to every PC in the org. Eventually we wrote a script that got put into GPO to create an alluserconnection VPN connection on each PC to one of 3 devices based on where they were located around the state.
Now I have been tasked with creating a script that removes all VPN connections from all PCs and replace them with a single uniformly named connection. I have the main script written to remove everything except the one we want, but it will not get profile specific connections since i run the scirpt as me and cannot see VPNs that were added without the alluserconnection switch. And if i put the script into GPO, it will run as system.
Is there a way to get any and all connections on a PC or perhaps run the script as the current/last logged on user to grab those as well?
Here is the script i have so far, maybe it will help someone even if i dont get an answer.
#Get all VPNs that do not match the correct one using the -AllUserConnection switch
$VPNs = Get-VpnConnection -AllUserConnection | where {$_.name -ne “TheOne”}
#check for a connection that matches the name of the new VPN
$theOne = Get-VpnConnection -AllUserConnection | where {$_.name -eq "TheOne"}
#Clean up any other VPN connections that may be installed for whatever reason
$VPNothers = Get-VpnConnection
#Remove all other AllUserConnection VPNs (like the various ones added for WFH) that are not the new one
foreach ($vpn in $VPNs)
{
Remove-VpnConnection -AllUserConnection $
vpn.name
-Force -PassThru
}
#Remove any other VPNs that may be installed on the PC
foreach ($vpnOther in $VPNothers)
{
Remove-VpnConnection $
vpnother.name
-Force -PassThru
}
#If $theOne is not present on the machine, it will get added
if(!$theOne)
{
Add-VpnConnection -AllUserConnection -Name "TheOne" blah blah blah, all that good stuff here -RememberCredential $false -Force -PassThru
}