From h@nnes.be, 6 Years ago, written in PowerShell.
This paste is a reply to Install-ConnexeonVpn.ps1 from h@nnes.be - view diff
Embed
  1. # PowerShell script to add a PPTP dialin VPN for Connexeon - run this script from an **elevated PowerShell** prompt.
  2. #
  3. #  - for all users: so create once for all local users
  4. #  - tries to use AD credential of the logged in user, no more re-typing the same login
  5. #  - uses a hostname so it'll be a lot easier to migrate the VPN server in the future, no reconfig on the clients  required
  6.  
  7. # First ensuring we're running elevated because this is required to succesfully create the VPN.
  8. # The script wil exit without trying to proceed if it's not possible to elevate.
  9. param([switch]$Elevated)
  10. function Check-Admin {
  11.   $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
  12.   $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
  13. }
  14. if ((Check-Admin) -eq $false)  {
  15.   if ($elevated)
  16.   {
  17.     # Could not elevate, quit
  18.   }
  19.   else {
  20.     Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
  21.   }
  22.   exit
  23. }
  24.  
  25. # If we reach this, we are running elevated.
  26. # The actual magic starts here.
  27.  
  28. # If a VPN connection exist with the same name, we will first remove the old one, so the script can proceed without throwing errors
  29. $vpnConnections = Get-VpnConnection -AllUserConnection
  30. if($vpnConnections.Name -eq "VPN Connexeon")
  31. {
  32.   Remove-VpnConnection -Name "VPN Connexeon" -AllUserConnection -Force
  33. }
  34.  
  35. # Also removing user specific VPN connection with the same name as the one we're trying to create.
  36. $vpnConnections = Get-VpnConnection
  37. if($vpnConnections.Name -eq "VPN Connexeon")
  38. {
  39.   Remove-VpnConnection -Name "VPN Connexeon" -Force
  40. }
  41.  
  42. # Adding all user VPN, requiring encryption doesn't prompt for credentials and tries currently logged in user first.
  43. Add-VpnConnection -Name "VPN Connexeon" -ServerAddress "vpn.connexeon.com" -TunnelType Pptp -EncryptionLevel Required -AuthenticationMethod MSChapv2 -AllUserConnection -RememberCredential -PassThru -UseWinlogonCredential
  44.  
  45. # Split tunneling for a local internet breakout - this will prevent Internet traffic being tunneled.
  46. # This is more performant and keeps clear from possible config issues when terminating the internet connection on the VPN server.
  47. Set-VpnConnection "VPN Connexeon" -AllUserConnection -SplitTunneling $True
  48.  
  49. # Adding internal prefixes to be tunneled
  50. Add-VpnConnectionRoute -ConnectionName "VPN Connexeon" -DestinationPrefix 10.0.0.0/12 -PassThru
  51. Add-VpnConnectionRoute -ConnectionName "VPN Connexeon" -DestinationPrefix 10.111.0.0/16 -PassThru
  52. Add-VpnConnectionRoute -ConnectionName "VPN Connexeon" -DestinationPrefix 10.50.0.0/16 -PassThru
  53.