Skip to content

Instantly share code, notes, and snippets.

@haggaie
Created July 28, 2024 06:49
Show Gist options
  • Save haggaie/559d4523ad60ac67822ac7bc40bd1390 to your computer and use it in GitHub Desktop.
Save haggaie/559d4523ad60ac67822ac7bc40bd1390 to your computer and use it in GitHub Desktop.
Cisco AnyConnect can require that no other users be logged in. This script logs off these other users before launching it.
# Cisco AnyConnect can require that no other users be logged in.
# This script logs off these other users before launching it.
# Function to log off all users except the current one
function LogOffOtherUsers {
Write-Output "LogOffOtherUsers:"
$currentUser = (whoami).Split('\')[1] # Extract the username from "hostname\user"
Write-Output "Current user: $currentUser"
$sessions = quser | ForEach-Object -Process { $_ -replace '\s{2,}',',' } | ConvertFrom-Csv
$sessions | ForEach-Object {
$session = $_
$user = ($session.username) -replace '^[> ]',''
if ($user -ne $currentUser) {
Write-Output "Logging off $user"
sudo logoff $session.sessionname
}
}
# }
}
# Function to start Cisco AnyConnect GUI
function StartCiscoAnyConnectGUI {
$anyConnectPath = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
if (Test-Path $anyConnectPath) {
Start-Process -FilePath $anyConnectPath
} else {
Write-Output "Cisco AnyConnect client not found at $anyConnectPath"
}
}
# Main script execution
LogOffOtherUsers
# Start Cisco AnyConnect GUI
StartCiscoAnyConnectGUI
@haggaie
Copy link
Author

haggaie commented Apr 6, 2025

On Windows 11 24H2 need to enable Sudo as explained here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment