Cisco Anyconnect Api Powershell
For these reasons, is the acquisition of powershell connect VPN cisco anyconnect promising: The wonderful Benefits, the itself when Use of powershell connect VPN cisco anyconnect result are impressive: A potentially dangerous and very much expensive operational Intervention is avoided. I wrote this script because I had a need to detect the current state of a VPN connection. With Windows 10, you can use the “Get-VpnConnection” cmdlet. However, if you are working in an environment where that commandlet is not available you can use a WMI query to determine the VPN Connection State. This is a powershell script “Cisco AnyConnect Auto Login”, this is created to automatically connect and log me in or auto reconnect with Cisco AnyConnect Secure Mobility Client version 3.0.5080. It works quite nice on Windows 7 and Windows 8. Create file for example c:testCiscoVPNAutoLogin.ps1 2. Paste the code below into that file. 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. First off, I cannot use the RDM Cisco AnyConnect plugin as the particular systems I connect to have additional custom prompts that need to be answered. Instead I have been launching the AnyConnect powershell script utilizing RDM's embedded script function for the past 2 years and it had worked great up until about a month ago.
# Source www.cze.cz |
# This script is tested with 'Cisco AnyConnect Secure Mobility Client version 3.0.5080' |
# Run using %SystemRoot%system32WindowsPowerShellv1.0powershell.exe -ExecutionPolicy Bypass -File 'C:CiscoVPNAutoLogin.ps1' |
# VPN connection details |
[string]$CiscoVPNHost='vpn.example.com' |
[string]$Login='username' |
[string]$Password='password' |
[string]$vpncliAbsolutePath='C:Program Files (x86)CiscoCisco AnyConnect Secure Mobility Clientvpncli.exe' |
[string]$vpnuiAbsolutePath='C:Program Files (x86)CiscoCisco AnyConnect Secure Mobility Clientvpnui.exe' |
#**************************************************************************** |
#**** Please do not modify code below unless you know what you are doing **** |
#**************************************************************************** |
Add-Type-AssemblyName System.Windows.Forms -ErrorAction Stop |
# Set foreground window function |
# This function is called in VPNConnect |
Add-Type@' |
using System; |
using System.Runtime.InteropServices; |
public class Win { |
[DllImport('user32.dll')] |
[return: MarshalAs(UnmanagedType.Bool)] |
public static extern bool SetForegroundWindow(IntPtr hWnd); |
} |
'@-ErrorAction Stop |
# quickly start VPN |
# This function is called later in the code |
FunctionVPNConnect() |
{ |
Start-Process-FilePath $vpncliAbsolutePath-ArgumentList 'connect $CiscoVPNHost' |
$counter=0; $h=0; |
while($counter++-lt1000-and$h-eq0) |
{ |
sleep -m 10 |
$h= (Get-Process vpncli).MainWindowHandle |
} |
#if it takes more than 10 seconds then display message |
if($h-eq0){echo 'Could not start VPNUI it takes too long.'} |
else{[void] [Win]::SetForegroundWindow($h)} |
} |
# Terminate all vpnui processes. |
Get-Process|ForEach-Object {if($_.ProcessName.ToLower() -eq'vpnui') |
{$Id=$_.Id; Stop-Process$Id; echo 'Process vpnui with id: $Id was stopped'}} |
# Terminate all vpncli processes. |
Get-Process|ForEach-Object {if($_.ProcessName.ToLower() -eq'vpncli') |
{$Id=$_.Id; Stop-Process$Id; echo 'Process vpncli with id: $Id was stopped'}} |
# Disconnect from VPN |
echo 'Trying to terminate remaining vpn connections' |
start-Process-FilePath $vpncliAbsolutePath-ArgumentList 'disconnect'-wait |
#Connect to VPN |
echo 'Connecting to VPN address '$CiscoVPNHost' as user '$Login'.' |
VPNConnect |
# Write login and password |
[System.Windows.Forms.SendKeys]::SendWait('$Login{Enter}') |
[System.Windows.Forms.SendKeys]::SendWait('$Password{Enter}') |
# Start vpnui |
start-Process-FilePath $vpnuiAbsolutePath |
# Wait for keydown |
echo 'Press any key to continue ...' |
try{$x=$host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')}catch{} |
commented Jul 22, 2020
Is there any way to run this script in hidden mode? |
commented Jul 24, 2020
@Shikha1912, sorry, been a long time since I’ve used this but no, there wasn’t at the time. The script relies on launching the login form to send the username and password. It’s possible that a new client has a cleaner way to make thins work. Please let me know if you find a solution. |