Automate creation of an L2TP VPN with Pre Shared Key and Automatically Use Windows Credentials

May 19, 2011 — 52 Comments

I’ve been trying to find a way to script or automate the creation of a new Windows VPN connection that uses L2TP/IPSEC with a pre shared key and automatically uses the current user’s credentials, but it seems there is no way to do this using the CMAK, netsh, various powershell scripts, or GPO Preferences as none of them include all of the options we needed to set. So I have come up with a .NET app for doing this and am posting the code here in case it helps anyone else out.

The image below highlights the settings that we were struggling to configure via any kind of script or automated process.

Vpn

The MS CMAK tool lets you specify a pre shared key but does not let you enable the “Automatically use current windows credentials” option (there is an extra line you can add to the config file manually that supposedly enables this but it simply does not seem to work). This powershell script does not let you specify a pre shared key, and neither does the Server 2008 GPO Preferences option that lets you create VPN connection.

So I decided to try and write my own command line application in VB.NET that would create the VPN connection with all of the required settings. As there is no method for creating VPN connections in the .NET Framework, we need to use the native RAS Windows APIs. Anyone following this blog will know I’m no stranger to calling Windows APIs from .NET (see my Windows API Library) but I found a .NET library that already contains all of the RAS APIs here: http://dotras.codeplex.com/ so I decided to just use that rather than spending hours getting the API definitions worked out myself. So thanks to the author of the DotRas library (and yes I have made a donation to say thank you properly!) for saving me some time and headaches 🙂

After adding a reference to the DotRas library mentioned above, it was just a case of writing the following code to configure the VPN the way I wanted:

Imports DotRas

Module MainModule

    Sub Main()
        If Not My.Application.CommandLineArgs.Count = 3 Then
            ShowUsage()
        Else
            Dim VpnName As String = My.Application.CommandLineArgs(0)
            Dim Destination As String = My.Application.CommandLineArgs(1)
            Dim PresharedKey As String = My.Application.CommandLineArgs(2)

            Try
                Dim PhoneBook As New RasPhoneBook
                PhoneBook.Open()
                Dim VpnEntry As RasEntry = RasEntry.CreateVpnEntry(VpnName, Destination, DotRas.RasVpnStrategy.L2tpOnly, _
                                                                   DotRas.RasDevice.Create(VpnName, DotRas.RasDeviceType.Vpn))
                VpnEntry.Options.UsePreSharedKey = True
                VpnEntry.Options.UseLogOnCredentials = True
                PhoneBook.Entries.Add(VpnEntry)
                VpnEntry.UpdateCredentials(RasPreSharedKey.Client, PresharedKey)
                Console.WriteLine("VPN connection created successfully")
            Catch ex As Exception
                Console.WriteLine("ERROR: " & ex.Message & vbNewLine)
                Environment.Exit(999)
            End Try
        End If
    End Sub

    Private Sub ShowUsage()
        Console.WriteLine("Invalid number of arguments specified." & vbNewLine & vbNewLine & _
                          "Usage: VpnSetup.exe [VPN Name] [Destination] [Preshared Key]" & vbNewLine & vbNewLine & _
                          "EXAMPLE: VpnSetup.exe ""New VPN"" vpn.mycompany.com SomePassword" & vbNewLine)
    End Sub

End Module

As you can see, I have made this a command line application that accepts 3 arguments. The first is the name for the new VPN connection, the second is the destination IP address or host name, and the third is the pre shared key. So now in our SCCM task sequence for building laptops we can simply call this app like so:

VpnSetup.exe “Our VPN” vpn.company.com PreSharedKeyHere

Also note that this will need to be run “As Administrator” so I set the application to always require full admin rights so that you get a UAC prompt to elevate whenever you run it (to avoid access denied errors).

If anyone else is in need of a similar thing then let me know and I will see if I can make this available on my website and make it a bit more flexible (at the moment it is hard coded to only use L2TP with a pre shared key and use the current user’s logon credentials, you might not want that). Will have to check with the author of the DotRas library to see if this is OK as well.

Chris

52 responses to Automate creation of an L2TP VPN with Pre Shared Key and Automatically Use Windows Credentials

  1. 

    I want to create a file that I can email out that will automatically setup remote computers to connect to my L2TP VPN Server. I don’t need it to use current credentials. What would be the best way to do this. Thanks Chris!

    V/R Matt Cohen

  2. 

    Hi,

    We have exactly the same requirement as you did. Could you possibly make the executable available as is?

    Thanks

    Rob

  3. 

    can I get a compiled version please as I don’t have .NET installed now…

  4. 

    This is a great piece of work Chris, I think it will really help me. I’m planning to use Powershell thru Group Policy to roll this out.

    One small request. How can I add a line that will force it to over-write an existing VPN connection of the same name each time it runs? We’re tinkering with our VPN connections and I’d like to be able to modify a setting (e.g. pre-shared key) and have this over-write our user’s VPN connection with new settings each time they boot up.

    Thanks in advance
    Andy

    • 

      I can’t remember if the code as it is already overwrites an existing connection, have you tried it? Also I’m confused as to how you are going to roll this out via Powershell as its .NET code not Powershell (although I guess its probably not hard to call .NET code from Powershell but as the entire application code is there it would be easier to just compile the .NET app and run that directly)

      • 

        It doesn’t, unfortunatley – if you try to run it with the same VPN name parameter you get

        ERROR: ‘Test VPN’ already exists in the phone book.
        Parameter name: item

        RE the deployment – I’ve just compiled it and call the .exe with parameter through a Powershell startup script in Group Policy

      • 

        Ah ok, well then you can just add these lines right after the PhoneBook.Open line:

        If PhoneBook.Entries.Contains(VpnName) Then
        PhoneBook.Entries.Remove(VpnName)
        End If

        and that should delete the connection if it already exists 🙂

      • 

        Oh and I still don’t really see the need for the Powershell script, you can just specify the compiled EXE directly from a GPO startup script and use the arguments section to specify the command line arguments. Unless of course you do other stuff in your Powershell script as well as this.

      • 

        Thanks for your help. I added those lines, compiled, and it works perfect when running on my local PC – overwriting the connection. However when I run it from Goup Policy, it doesn not. How would you suggest I run it from Group Policy? At the moment I have created a GPO using Computer Configuration>Windows Settings>Scripts>Startup and pointed it directly at the .exe which I have copied to the policy script folder, with the correct parameters.

      • 

        Which bit doesn’t work, creating a VPN connection when one doesn’t already exist or just the replacing of a connection if it already exists? Bear in mind that in a computer startup script the program will be running as the Local System account so it won’t see any VPN connections that have been created for one user only.

      • 

        It’s OK – figured it out. I had another script creating a conflict. It now works perfectly!

      • 

        ahh good stuff 🙂 I should have known it couldn’t possibly be something wrong with the program I wrote 😛 haha glad you got it working

  5. 

    Hello Chris, Andy again. Thanks again for the great little piece of code.
    Another small request. Is it possible to add a line that will enable split tunnelling when creating the connection? Networking tab > IPv4 Properties > Advanced > Use Default Gateway on Remote Network?

    Many thanks in advance

    • 

      Hey 🙂 Have you had a look at the code yourself to see if there’s any obvious options for that? That’s all I’m going to do

      • 

        there you go, took less than 30 seconds to find this option:

        VpnEntry.Options.RemoteDefaultGateway = False

      • 

        Wow, thanks for the quick reply! That worked great.
        Sorry, my programming skills are extremely basic to say the least – I wouldn’t have known how to find that out 🙂

      • 

        no worries 🙂 all I did was open the visual studio project that had the code shown in this article in, then typed “VpnEntry.Options.” and looked through the list of available properties that Intellisense shows you.

  6. 

    Thank you so much for the great post on this! I am the systems admin for a school district working at rolling out a couple hundred laptops as student rentals and finding this post was a huge relief! This worked perfectly for us, thanks again!

  7. 

    Hi
    My application freez at line
    PhoneBook.Entries.Add(VpnEntry)

    Any idea why?

    • 

      No I’ve never seen that happen. What OS are you running it on? I assume you can manually create the same VPN connection without a problem?

      • 

        HI thank you for the quick reply.
        I’m using Win7 SP1 and VB.NET 2010 targeting framework 3.5
        After disabled UAC I successfully created an entry, but at the next attempt the freeze problem was back.
        Also, I see the connection but when I hit connect nothing happens. I looked around it’s properties and look ok to me.

      • 

        So is it just freezing when the connection already exists? Also you shouldn’t have to disable UAC for it to work – it does require admin permissions so if you run it as a normal user account it will probably fail with an access denied error but shouldn’t just freeze.

      • 

        Also try adding the code I mentioned in the comments above to remove the connection if it already exists

  8. 

    Yes, Already was on to this. In order to update an entry I must do the following
    First check if entry exists
    If yes check via rasdial if connection is open
    If it is, /disconnect
    delete connection

    And it works.

    It will run in an windows service with system account, so the rights is not an issue.

    Thank you very much for the quick response. I really appreciate the help

    I’ll keep you updated and when I’m done I’ll put a snippet here.
    I still have no idea why it freeze instead of throwing errors…

  9. 

    Forgot to mention that I cannot connect manually to a connection created in this manner, but I can with rasdial. This is confusing but is exactly what I needed 😀 I’ll test it this days on several machines and come back with more feedback on it

  10. 

    OK, I had some time yesterday for some tests. I am now able to create, delete connections, and connect to them using rasdial not dialer dontrol. Dialer control do not work for me. After I finish the rutine, I’ll post it here.

  11. 

    Sub DoVPN()
    If Not My.Computer.Network.IsAvailable Then Exit Sub
    Dim T2 As String = ReturnShellResults(“rasdial”)
    logger.Info(“RAS Status: ” & T2)
    If VPNm = False And T2 Like “*VPN*” Then
    ‘Here is a check I made for a condition that make me disconnect from the VPN
    If Pune(Cloud & “towerdesk/agent/get_location”, “CPUID=” & PreiauDate(“CPUID”)) = “1” Then
    Dim q As New Process
    q.StartInfo.UseShellExecute = True
    q.StartInfo.FileName = “rasdial”
    q.StartInfo.Arguments = “VPN /DISCONNECT”
    q.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    q.Start()
    q.WaitForExit()
    q.Dispose()
    Exit Sub
    End If
    Exit Sub
    End If
    If T2 Like “*VPN*” Then Exit Sub
    Try
    Dim tr As String = Interogare(Cloud & “towerdesk/agent/preiau_VPN/” & PreiauDate(“CPUID”))
    ‘tr variable is requested via HTTP and is something like “10.0.0.1 User Password” or “NET” in case of error
    If tr = “NET” Then Exit Sub
    Dim T As New List(Of String)(tr.Split(” “))
    CVPN.Open()
    If CVPN.Entries.Contains(“VPN”) Then
    If T2 Like “*VPN*” Then
    Dim q As New Process
    q.StartInfo.UseShellExecute = True
    q.StartInfo.FileName = “rasdial”
    q.StartInfo.Arguments = “VPN /DISCONNECT”
    q.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    q.Start()
    q.WaitForExit()
    q.Dispose()
    End If
    CVPN.Entries.Remove(“VPN”)
    End If
    Dim entry As RasEntry = RasEntry.CreateVpnEntry(“VPN”, T(0), RasVpnStrategy.Default, RasDevice.GetDeviceByName(“(PPTP)”, RasDeviceType.Vpn))
    CVPN.Entries.Add(entry)
    Dim p As New Process
    p.StartInfo.UseShellExecute = True
    p.StartInfo.FileName = “rasdial”
    p.StartInfo.Arguments = “VPN ” & T(1) & ” ” & T(2)
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
    p.Start()
    p.WaitForExit()
    p.Dispose()
    Catch
    End Try
    End Sub

  12. 

    Is there a way to create this with “Challenge Handshake Authentication Protocol (CHAP)” unchecked, turned off?

    • 

      probably, have you tried?

      • 

        This is not in my wheel house, I was unable to find a registry key associated. Would this be in the library or native WMI library?

    • 

      Play with VpnEntry.Options
      I’m sure frome here you can set sny property of the connection

      • 
        mike@milehighwelch.com April 16, 2012 at 17:45

        Yes, found that one, thanks!
        VpnEntry.Options.RequireChap = false

        Also found something else that helped us as we wanted each user to have permissions to change settings if need be without administrative rights. So we changed it from using the system phone book to per user phone book with:
        PhoneBook.Open(true)

      • 

        Thanks for posting your solution, I’m sure that will come in handy for others reading this

  13. 
    mike@milehighwelch.com April 16, 2012 at 17:49

    We are having an issue creating a desktop shortcut. Does anyone have any pointers? With using our usually very reliable picture taker tool (prism deploy) we are able to create the desktop icon to the VPN connection but when you double click on it it says VPN cannot be found. Only by manually creating it ( the desktop shortcut (right click and drag from Network Connections) does a desktop shortcut work. Thanks!

    • 

      Well the shortcut just points to a GUID, so it will be different every time you create the VPN on different computers (or even if you recreate it on the same computer). So you would need to see if you can grab the GUID of the VPN after your code has created it, and then create your own shortcut to that GUID (creating shortcuts isn’t very easy in .NET code though). I’ll have a play around and see if I can get something working for you
      EDIT: Actually that’s odd, if I create a shortcut to 2 different VPNs the GUID the shortcut points to is exactly the same :S Looks like that GUID is just purely the GUID of the dial up connection manager. If you look at the contents of the actual shortcut file itself though (by using the TYPE command in command prompt) you can see it has the path to the user’s phone book PBK file in it, as well as the destination host name / IP address.

  14. 

    Hi cjwdev
    Thanks again for your great work. Still using your fantastic code to deploy to our 100+ users.
    Is there a parameter I could introduce to populate the field ‘DNS suffix for this connection’ under Networking tab > IPv4 > Advanced > DNS tab ?

    • 

      Probably, have you had a look at the properties available on the VpnEntry object to see if anything looks like it would do that? Just open up the code I provided in visual studio and type VpnEntry.Options. When you press the last full stop it will show you a list of available properties and you can look through them to see if there’s anything obvious. That’s all I would do

  15. 

    which type of project must open in Visual studio 2010 ?

  16. 

    Hi cjwdev,
    i copied your script in a txt file, how do I rename it? File.VB?
    Which type of project must open in visual studio 2010? and i must set informatio in the argument parameter eg.
    Dim VpnName As String = My.Application.CommandLineArgs(myVPN)
    Dim Destination As String = My.Application.CommandLineArgs(asa.vpn.domain.com)
    Dim PresharedKey As String = My.Application.CommandLineArgs(ABC123) ?

    Sorry but i’m a newbie when it comes to programming 🙂

    • 

      Hi,

      It isn’t a script – its a VB.NET application, so you need to compile it in Visual Studio. If you send me an email though (cwright @ cjwdev dot co dot uk) I’ll send you a compiled version

  17. 

    I’m currently stumped by the VpnEntry.Options.RequirePap and RequireChap lines in my code. I need to require PAP authentication and disallow CHAP, but nothing I do seems to make these options work in my code. Any ideas or suggestions are appreciated.

    Imports DotRas

    Module MainModule

    Sub Main()
    If Not My.Application.CommandLineArgs.Count = 3 Then
    ShowUsage()
    Else
    Dim VpnName As String = My.Application.CommandLineArgs(0)
    Dim Destination As String = My.Application.CommandLineArgs(1)
    Dim PresharedKey As String = My.Application.CommandLineArgs(2)

    Try
    Dim PhoneBook As New RasPhoneBook
    PhoneBook.Open()

    ‘create vpn connection
    Dim VpnEntry As RasEntry = RasEntry.CreateVpnEntry(VpnName, Destination, DotRas.RasVpnStrategy.L2tpOnly, DotRas.RasDevice.Create(VpnName, DotRas.RasDeviceType.Vpn))

    ‘set vpn connection options
    VpnEntry.Options.RequireChap = False
    VpnEntry.Options.RequirePap = True
    VpnEntry.Options.UsePreSharedKey = True
    PhoneBook.Entries.Add(VpnEntry)
    VpnEntry.UpdateCredentials(RasPreSharedKey.Client, PresharedKey)

    Console.WriteLine(“VPN connection created successfully”)
    Catch ex As Exception
    Console.WriteLine(“ERROR: ” & ex.Message & vbNewLine)
    Environment.Exit(999)
    End Try
    End If
    End Sub

    Private Sub ShowUsage()
    Console.WriteLine(“Invalid number of arguments specified.” & vbNewLine & vbNewLine & _
    “Usage: VpnSetup.exe [VPN Name] [Destination] [Preshared Key]” & vbNewLine & vbNewLine & _
    “EXAMPLE: VpnSetup.exe “”LC VPN”” v.lifestylecommunities.com SomePassword” & vbNewLine)
    End Sub

    End Module

    • 
      alexgannfarmersfire March 25, 2015 at 02:29

      rsx_,

      This is so far after-the-fact, but I was having the same issue until I used dotras to inspect the Options on a manually created VPN entry to see what I was missing.

      The culprit is VpnEntry.Options.RequireEncryptedPassword. If this is True (which it is by default), then Chap and MSChap are enabled and Pap is disabled no matter what you specify otherwise. When you set VpnEntry.Options.RequireEncryptedPassword = False, the connection is still encrypted as long as your RequireDataEncryption = True, nothing is sent plain text over the wire.

      Others deploying Cisco Meraki MX series devices will run into this per https://docs.meraki.com/display/MX/VPN+settings+for+Windows+7

      Hope that helps someone – had me scratching my head for awhile.

  18. 

    Hi Chris,

    I have been looking for this code every where, you couldn’t send me the compiled version of this code ?

    Thanks
    Haq

  19. 

    Could you please send me a copy of compiled program ?? I dont have known of programing but I have the same need of you.

    Thanks in Advance

  20. 

    Hi, could you please send me the copy of your compiled exe?
    Thank you a lot

  21. 

    Hi guys,

    I’ve spent the last day or so working on this sample code, refactoring it for a project i’m working on.

    My use case is that i’m connection Windows 7 laptops to an OpenBSD L2TP/IPSEC environment using PSK and migrating it PKI in the near future.

    Each user gets a pre-built, custom deployment package that contaiins everything (including username/passwords) for a set of VPN connections.

    I refactored this code to make an app that the package install script calls for each VPN connection to be deployed (during this migration phase, there’s 4 connections, with the PSK connections to be removed in the future).

    Hopefully this code is helpful for folks.

    Changes I’ve made are:

    1) app over-writes VPN connections if you install one with the same name as a pre-existing one
    2) App takes additional flags such as [PSK|PKI] and a username/password in addition to the PSK pass in the original code.
    3) resulting VPN connections hide the user interface and dont prompt the user for a username/password. It’s assumed that password changes necessitate a total redeployment of the install package this is bundled with.

    Thanks!
    ————————————–

    ‘rakrasvpnmgr

    ‘This program is meant to bridge a gap in Windows 7 that prevents the
    ‘deployment of pre-configured VPN connections to users.

    ”Windows 8 provides a number of userful powershell comlets such as Get-VpnConnection
    ”and set-VpnConnection that do the heavy lifting for deploying VPN connections.

    ”Windows 7 doesn’t have any thing so useful. Instead, calls to the dotnet framework
    ”have to be made through a comunity provided library called DotRas to achieve the same goal.

    ‘As a result, this program has been modified from source code found at the below cited references
    ‘to do the following:

    ‘1) Provide a CLI for building VPN interfaces
    ‘2) Configure the interface to either authenticate using a PSK (pre-shared-key)
    ‘ or to use ssl client certificates provided as part of a PKI (Public Key Infrastructure)
    ‘3) Configure the interface with a pre-determined set of user credentials to simplify user experience.

    ‘It should be noted that the driving assumptions for this program’s design are:

    ‘a) That a unique deployment package for each user is generated and deployed, and that this program
    ‘ will be bundled inside the package and called with the prescribed users credentials.
    ”b) That sufficent effort is taken to prevent that package from being accessed by
    ” any one who is not the prescribed user

    ‘This program was prepared for the following environment, and has not been tested outside of the below:

    ‘Windows 7 SP1 (6.1.7601.65536)

    ‘.net Environment Versions:
    ‘———– ——-
    ‘v2.0.50727 2.0.50727.5420
    ‘v3.0 3.0.30729.5420
    ‘Windows Communication Foundation 3.0.4506.5420
    ‘Windows Presentation Foundation 3.0.6920.5011
    ‘v3.5 3.5.30729.5420
    ‘Client 4.5.50938
    ‘Full 4.5.50938
    ‘Client 4.0.0.0

    ‘DotRAS Library Version: 1.3.5166.33429
    ‘DotRAS Lib Path: DotRas SDKs\v1.3\Libraries\NET20\WIN7\DotRas.dll
    ‘Functional Runtime Environment: v2.0.50727

    ‘Thanks to Chris Wright (cwright@cjwdev.co.uk) for the initial source code that is modified below.

    ‘Further thanks to Jeff Winn and the team at DotRas.

    ‘Original VB source code found at: https://blog.cjwdev.co.uk/2011/05/19/automate-creation-of-an-l2tp-vpn-with-pre-shared-key-and-automatically-use-windows-credentials/
    ‘DotRas SDK found here: http://dotras.codeplex.com/

    Imports DotRas
    Imports System.Net
    Imports System.Collections.ObjectModel

    Module MainModule
    Sub Main()
    If Not My.Application.CommandLineArgs.Count >= 5 Then
    Console.WriteLine(“Invalid number of arguments specified.” & vbNewLine & vbNewLine)
    ShowUsage()
    Else

    Dim VpnName As String = My.Application.CommandLineArgs(0)
    Dim Destination As String = My.Application.CommandLineArgs(1)
    Dim AuthType As String = My.Application.CommandLineArgs(2)
    Dim AuthUser As String = My.Application.CommandLineArgs(3)
    Dim AuthPass As String = My.Application.CommandLineArgs(4)

    Dim PresharedKey As String = “”
    Select Case AuthType
    Case “PSK”
    Console.WriteLine(“You chose the PSK Authentication Method” & vbNewLine & vbNewLine)
    Try
    PresharedKey = My.Application.CommandLineArgs(5)
    Catch ex As Exception
    Console.WriteLine(“You did not provide a Preshared Key” & vbNewLine & vbNewLine)
    ShowUsage()
    Environment.Exit(888)
    End Try
    Case “PKI”
    Console.WriteLine(“You Chose the PKI Authentication Method”)
    Case Else
    Console.WriteLine(“ERROR: Incorrect Auth Type.” & vbNewLine & vbNewLine)
    ShowUsage()
    Environment.Exit(888)
    End Select

    Try

    Dim VPNUserCreds As New NetworkCredential(AuthUser, AuthPass)

    Dim PhoneBook As New RasPhoneBook
    PhoneBook.Open()
    Dim VpnEntry As RasEntry = RasEntry.CreateVpnEntry(VpnName, Destination, DotRas.RasVpnStrategy.L2tpOnly, _
    DotRas.RasDevice.Create(VpnName, DotRas.RasDeviceType.Vpn))
    If AuthType = “PSK” Then
    VpnEntry.Options.UsePreSharedKey = True
    Else

    End If

    ‘This option turns on “AutoLogon” in the VPN connection and the user wont be prompted for a username and password.
    ‘It ALSO forces the connection to use the Windows Login Credentials for the VPN connection instead of the stored RAS password
    ‘ This option should always be false if the VPN connection does not use your Windows Login Credentials for authentication.
    VpnEntry.Options.UseLogOnCredentials = False

    ‘When “False”, this option overrides the Windows Login Credentials and passes the stored RAS credentials to the connection.
    VpnEntry.Options.DoNotUseRasCredentials = False

    ‘These three options will suppress the user dialogue when launching the VPN connection, preventing users from changing
    ‘elements like their user name or their password. Possibly helpful if account credentials are provided in a binary install file
    ‘and a change to a VPN account password causes a new package to be built and delivered to the user.
    VpnEntry.Options.PreviewDomain = False
    VpnEntry.Options.PreviewPhoneNumber = False
    VpnEntry.Options.PreviewUserPassword = False

    ‘If we have an pre-existing entry in the phonebook that matches our VpnEntry.name item, then assume we are over-writing that config with
    ‘new configurations.
    ‘ Test also if the connection is open, and if so, close it prior to re-installing the connection.
    If PhoneBook.Entries.Contains(VpnEntry.Name) Then
    Console.WriteLine(“A previous VPN Phonebook Entry for the item ” & VpnEntry.Name & ” already exists. Overwriting this entry.” & vbNewLine)
    Dim current_connections As ReadOnlyCollection(Of RasConnection) = RasConnection.GetActiveConnections()
    For Each connection As RasConnection In current_connections
    If connection.EntryName = VpnEntry.Name Then
    Console.WriteLine(“Hanging up connection to ” & connection.EntryName & vbNewLine)
    connection.HangUp()
    End If
    Next
    PhoneBook.Entries.Remove(VpnEntry.Name)
    End If

    PhoneBook.Entries.Add(VpnEntry)
    VpnEntry.UpdateCredentials(RasPreSharedKey.Client, PresharedKey)
    VpnEntry.UpdateCredentials(VPNUserCreds, True)

    Console.WriteLine(“VPN connection created successfully”)
    Catch ex As Exception
    Console.WriteLine(“ERROR: ” & ex.Message & vbNewLine)
    Environment.Exit(999)
    End Try
    End If
    End Sub
    Private Sub ShowUsage()
    Console.WriteLine(“Usage: rakrasvpnmgr.exe [VPN Name] [Destination] [PSK | PKI] [User Name] [User Password] [Preshared Key]” & vbNewLine & vbNewLine & _
    “EXAMPLE: rakrasvpnmgr.exe “”New VPN”” vpn.mycompany.com PKI “”SomeUser”” “”UserPassword””” & vbNewLine & vbNewLine & _
    “or rakrasvpnmgr.exe “”Different VPN”” vpn.mycompany.com PSK “”DifferentUser”” “”DoodsPassword”” “”MyCompanyPreSharedKey”” ” & vbNewLine)
    End Sub
    End Module

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s