Created
July 28, 2024 06:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On Windows 11 24H2 need to enable Sudo as explained here