Skip to content

Instantly share code, notes, and snippets.

@tuxitop
Created October 22, 2025 12:00
Show Gist options
  • Select an option

  • Save tuxitop/2ee124d3f0d2e2c9bdc0c4fb560498bc to your computer and use it in GitHub Desktop.

Select an option

Save tuxitop/2ee124d3f0d2e2c9bdc0c4fb560498bc to your computer and use it in GitHub Desktop.
Automate kerio login in windows
## Author: Ali Mousavi
## Last Updated: 2025-10-22
## Description: This script checks the SSID of the Wifi network and
## if it matches, trys to log in with kerio.
## Hint: Further automation can be made using windows task scheduler.
## e.g. run the script on network change.
$Ssid = '' # SSID of the wifi
$KerioURL = '' # https://kerio.example.com
$KerioUser = ''
$KerioPass = ''
function Get-CurrentWifiSsid {
# Uses netsh (works on most Windows versions)
$out = netsh wlan show interfaces 2>$null
if (-not $out) {
return $null
}
foreach ($line in $out) {
if ($line -match '^\s*SSID\s*:\s*(.+)$') {
return $matches[1].Trim()
}
}
return $null
}
function LoginKerio {
$requestParams = @{
Uri = "$KerioURL/internal/dologin.php"
Method = 'POST'
Body = @{
"kerio_username" = $KerioUser
"kerio_password" = $KerioPass
}
ContentType = "application/x-www-form-urlencoded"
}
try {
# Using Invoke-RestMethod for JSON/text response handling
$response = Invoke-WebRequest @requestParams
Write-Host "✅ Request Sent."
if ($respose.Content -like "*The following user is currently logged on from your computer*") {
Write-Host "⚠️ Someone is currently logged in. Nothing to do."
exit 0
} elseif ($response.Content -like "*Incorrect username or password*") {
Write-Host "❌ Login failed. Invalid username or password."
exit 2
} else {
Write-Host "✅ Logged in successfully."
}
}
catch {
Write-Host "❌ Request failed:"
Write-Host $_.Exception.Message
}
}
$currentSsid = Get-CurrentWifiSsid
if ($null -eq $currentSsid) {
Write-Host "Not connected to any Wi-Fi network."
exit 1
}
if ($currentSsid -ieq $Ssid) {
Write-Host "✅ Connected to '$Ssid' — Logging in with kerio"
LoginKerio
} else {
Write-Host "⚠️ SSID('$currentSsid') does not match '$Ssid'. Nothing to do."
}
@tuxitop
Copy link
Author

tuxitop commented Oct 22, 2025

To automate running the script using windows task scheduler:

  • Press Win + S → type: Task Scheduler → Open
  • Click Create Task (not "basic task")

🔒 General Tab:

  • Name: Select a name
  • Check: Run whether user is logged in or not

⏰ Triggers Tab:

  • Click New…
  • Begin the task: On an event
  • Settings:
    • Log: Microsoft-Windows-NetworkProfile/Operational
    • Source: NetworkProfile
    • Event ID: 10000 (connected to Wi-Fi)
  • Click OK

⚙️ Actions Tab:

  • Click New
  • Action: Start a program
  • Program/script: "C:\Path\To\login_script.ps1"

Conditions Tab:

  • Uncheck Start only if on AC power (so it runs on battery too)

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