Introduction
Logging in every day to VPN through Cisco is a very boring process. Just setup once and you can utilize until your VPN login password expiry. This powershell script is used only on Windows 7 onwards systems whoever is connecting from your local systems to your AD servers through Cisco AnyConnect Secure Mobility Client.
Prerequisites
- Powershell version 4+ (if you are using the below versions, just download from here.
- .NET Framework 3.5+
- CISCO AnyConnect Secure Mobility Client v4.6
Setup Process
Copy the script paste in Windows Powershell editor, read the comments and setup.
I tested only Windows ENV with CISCO AnyConnect Secure Mobility Client v4.6.
<#
========================================================================
-- Author : RAMANAREDDY V
-- Create date : 26-06-2018
-- Description : VPN SERVER Login with single click
-- Script Name : VPN SERVER.ps1
===========================================================================
#>
Set-ExecutionPolicy -ExecutionPolicy Bypass
TRY{
Set-ExecutionPolicy -ExecutionPolicy Bypass
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::
GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
Set-ExecutionPolicy -ExecutionPolicy Bypass
$vpnuiAbsolutePath = 'C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility
Client\vpnui.exe' # Check the location CISCO vpnui.exe presence must and should.
$ServerIpAddress="10.000.00.00"
$UserName="Your domain\username"
$Password="Your password" # Here you can secure ur password by using encryption process.
I am given just basic format.
Start-Process -FilePath $vpnuiAbsolutePath
$pinvokes = @'
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Text;
public class Program
{
private delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
private static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Auto)]
private static extern IntPtr GetWindowCaption(IntPtr hwnd, StringBuilder lpString, int maxCount);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern long GetWindowText(IntPtr hwnd, StringBuilder lpString, long cch);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
public static int disconnectValue = 0;
public static void ClickButtonLabeledYes()
{
try
{
IntPtr focusWindow = FindWindow
("SetFocus", "Cisco AnyConnect Secure Mobility Client");
var windowCaption = FindWindowByCaption(IntPtr.Zero,
"Cisco AnyConnect Secure Mobility Client");
if (windowCaption.ToString().Length >= 2)
{
SetForegroundWindow(focusWindow);
EnumChildWindows(windowCaption, EnumChildWindowsCallback, IntPtr.Zero);
}
}
catch (Exception e)
{
// new LogEntry(": " + e.ToString());
}
}
public static bool EnumChildWindowsCallback(IntPtr handle, IntPtr pointer)
{
const uint WMLBUTTONDOWN = 0x0201;
const uint WMLBUTTONUP = 0x0202;
var sb = new StringBuilder(256);
// Get the control's text.
GetWindowCaption(handle, sb, 256);
var text = sb.ToString();
if (text.ToString().Equals("Connect", StringComparison.InvariantCultureIgnoreCase))
{
PostMessage(handle, WMLBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
PostMessage(handle, WMLBUTTONUP, IntPtr.Zero, IntPtr.Zero);
}
else if (text.ToString().Equals
("Disconnect", StringComparison.InvariantCultureIgnoreCase))
{
disconnectValue = 1;
}
return true;
}
public static void SecondWindowClick(string Password)
{
try
{
IntPtr focusWindow = FindWindow("SetFocus", "Cisco AnyConnect |
YOUR DOMAIN NAME"); // Enter your org domain name like shown on CISCO window Header
var windowCaption = FindWindowByCaption(IntPtr.Zero, "Cisco AnyConnect |
YOUR DOMAIN NAME"); // Enter your org domain name like shown on CISCO window Header
if (windowCaption.ToString().Length >= 2)
{
SetForegroundWindow(focusWindow);
SendKeys.SendWait(Password);
EnumChildWindows(windowCaption, SecondWindowClickCallback, IntPtr.Zero);
}
}
catch (Exception e)
{
// new LogEntry(": " + e.ToString());
}
}
public static bool SecondWindowClickCallback(IntPtr handle, IntPtr pointer)
{
const uint WMLBUTTONDOWN = 0x0201;
const uint WMLBUTTONUP = 0x0202;
var sb = new StringBuilder(256);
GetWindowCaption(handle, sb, 256);
var text = sb.ToString();
if (text.ToString().EndsWith("OK", StringComparison.InvariantCultureIgnoreCase))
{
PostMessage(handle, WMLBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
PostMessage(handle, WMLBUTTONUP, IntPtr.Zero, IntPtr.Zero);
}
return true;
}
public static void ThirdWindowClick()
{
try
{
IntPtr focusWindow = FindWindow("SetFocus", "Cisco AnyConnect");
var windowCaption = FindWindowByCaption(IntPtr.Zero, "Cisco AnyConnect");
if (windowCaption.ToString().Length >= 2)
{
SetForegroundWindow(focusWindow);
EnumChildWindows(windowCaption, ThirdWindowClickCallback, IntPtr.Zero);
}
}
catch (Exception e)
{
// new LogEntry(": " + e.ToString());
}
}
public static bool ThirdWindowClickCallback(IntPtr handle, IntPtr pointer)
{
const uint WMLBUTTONDOWN = 0x0201;
const uint WMLBUTTONUP = 0x0202;
var sb = new StringBuilder(256);
GetWindowCaption(handle, sb, 256);
var text = sb.ToString();
if (text.ToString().EndsWith("Accept", StringComparison.InvariantCultureIgnoreCase))
{
PostMessage(handle, WMLBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
PostMessage(handle, WMLBUTTONUP, IntPtr.Zero, IntPtr.Zero);
}
return true;
}
}
'@
[Reflection.Assembly]::LoadWithPartialName("System")
[Reflection.Assembly]::LoadWithPartialName("System.Runtime.InteropServices")
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[Reflection.Assembly]::LoadFile("C:\Program Files
(x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll") # Check the
location library presence must and should.
$refs = @("System","System.Runtime.InteropServices","System.Windows.Forms")
Add-Type -TypeDefinition $pinvokes -ReferencedAssemblies $refs -IgnoreWarnings
Start-Sleep -s 7 # SET TIME interval based on your internet and RAM speed
[Program]::ClickButtonLabeledYes()
$testvarible = [Program]::disconnectValue
If ($testvarible -eq '0')
{
Start-Sleep -s 7 # SET TIME interval based on your internet and RAM speed
[program]::SecondWindowClick($Password)
Start-Sleep -s 7 # SET TIME interval based on your internet and RAM speed
[program]::ThirdWindowClick()
Start-Sleep -s 7 # SET TIME interval based on your internet and RAM speed
}
Else {
Start-Sleep -s 1
}
cmdkey /generic:TERMSRV/$ServerIpAddress /user:$UserName /pass:$Password
mstsc /v:$ServerIpAddress
}
catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Output($ErrorMessage)
Write-Output($FailedItem)
Start-Sleep -s 10
}
Set the time intervals (i.e., Start-Sleep -s 7) based on your local Internet and RAM speed.
Precautions
- Just run the script once your system gets idle position after turning on.
- While executing this script, don't open any window means don't change the focus of executing window popup.
- preferences.xml file is mandatory in this location "C:\Users\Enter ur local machine name\AppData\Local\Cisco\Cisco AnyConnect Secure Mobility Client"
Please make sure in that file [<DefaultUser>Your username</DefaultUser>,<DefaultHostName>Your VPN Address </DefaultHostName>], these two attributes must and should. - Please make sure first time to save your CISCO login Username, VPN address and MSTSC login IP and password save by using Remember me checkbox on both logins.
Cons
- It will not work your VPN server password change time.
- It will not work slow network connectivity.
- It will not work If you delete CISCO system cache(temp files) in your local machine.
- It will not work MSTSC warning pop up occurs. (Make sure you check the remember check box once.)
Note
I am not responsible for your password. Please secure your local machine and run your script with single point of click. If anything is wrong with the script, please let me know and I will correct myself.