Sunday 12 February 2017

PowerShell to connect to a VPN more efficiently

TL;DR;

On starting PowerShell autoload a function that will prompt for a password which it then uses in a call to rasdial to open a previously configured VPN connection.  Why?  To stop using the mouse.

Details

If you have to frequently connect to a VPN from Windows 10, using the GUI starts to feel quite long-winded.  Particularly if (like me) you cannot save your password as part of the VPN profile (in my case the password is a token that has to be requested for every new connection, but you might equally have it mandated that the password is not to be stored locally). Fortunately with just a few lines of code, PowerShell offers a faster, mouse-free alternative.

The first thing to do is create the VPN profile (see the instructions from MS on how to do this).
Now create a script file that contains a function that will do the actual connecting to the VPN, e.g.
Function vpnMyProfile
{
$password = Read-Host -Prompt 'Enter password'
rasdial "[your VPN profile name]" [your vpn user name] $password
}

Save it in a sensible location (you'll use the path in a moment).

Finally, add a line into your PowerShell profile to the file.  I have a few scripts that I want to load when PowerShell starts so it's slightly more convoluted, but the principle is get PowerShell to
$psdir = "C:\Users\[YourUser]\Documents\WindowsPowerShell\AutoLoad" #The path from the step above
Get-ChildItem "${psdir}\*.ps1" | %{.$_} #loop through all the .ps1 files in the dir and load them

Now you should be able to simply type 'vpnMyProfile' at the PowerShell prompt and you will be prompted for your password.  Enter it and hit 'return' and hopefully you should connect to your VPN.

Added bonus: Disconnect from your VPN without using the GUI.
In the same file that contains your function to connect to your VPN, add the following:
Function vpnKill { rasdial /Disconnect}

Which is slightly quicker than typing out the command it effectively aliases (with the flag).

10 comments:

  1. Great post.
    You have a clue if the same tasks can be done with pure PowerShell, I mean, not using rasdial?

    ReplyDelete
    Replies
    1. Sorry, I don't know. Why do you want to avoid rasdial?

      Delete
    2. I've developed a VPN configuration client for a company I work for. It was made in pure C#, invoking Powershell to configure on Windows >= 8. But even in these versions, PoSH can't do connections/disconnections.

      On Windows < 8, RasDial and some configuration files are the only way to accomplish the task...

      After all, even on recent versions of Windows, the code went mixed, with rasdial and PowerShell code. I was looking for a way to do everything using just one strategy.

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. Great Article with Nice information Thank you for sharing us.,


    Nice Article I Found this blog very informative and helpful to get good information. I'm very happy to share your info with us. thank you Keep Updating Us..,

    ReplyDelete
  5. Do Follow URL Submission is a social bookmarking submission website that provide you genuine do follow link for backlink of your website
    Do Follow URL Submission

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. Great knowledge, do anyone mind merely reference back to it save money with vpn

    ReplyDelete