E×ρréšš VPN cli PowerShell script

sUFF3R

Forum Expert
What's this script?
  1. Properly capture full location names from status messages
  2. Wait up to 20 seconds for connection establishment
  3. Continuously check connection state during setup
  4. Compare full location names exactly
  5. Handle server-side location redirection
  6. Connects to random location every 5 minutes
Notes:
(Save as [anyfilename].ps1) Eg. E×ρréššvpnrotate.ps1


Code:
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Definition)`"" -Verb RunAs
    exit
}

$E×ρréššvpn = "C:\Program Files (x86)\E×ρréššVPN\services\E×ρréššVPN.CLI.exe"

function Get-RandomVPNLocation {
    $vpnList = & $E×ρréššvpn list
    $locations = $vpnList | ForEach-Object {
        if ($_ -match '^\s{2}(.+?)\s+(\d+)$' -and $matches[1] -ne 'Pick for Me') {
            [PSCustomObject]@{
                FullName = $matches[1].Trim()
                ID = $matches[2]
            }
        }
    } | Where-Object { $_ -ne $null }
   
    return $locations | Get-Random
}

function Get-CurrentConnection {
    $status = & $E×ρréššvpn status
    if ($status -match 'Connected to (.+?)(\.|$)') {
        return $matches[1].Trim()
    }
    return $null
}

try {
    # Initial disconnect
    & $E×ρréššvpn disconnect | Out-Null
    Start-Sleep -Seconds 5

    while ($true) {
        # Get random location
        $location = Get-RandomVPNLocation
       
        # Connect to selected location
        Write-Host "Connecting to $($location.FullName) (ID: $($location.ID))..." -ForegroundColor Cyan
        & $E×ρréššvpn connect $location.ID | Out-Null
       
        # Wait for connection with timeout
        $timeout = 20  # seconds
        $connected = $false
        while ($timeout -gt 0) {
            $currentLocation = Get-CurrentConnection
            if ($currentLocation) {
                if ($currentLocation -eq $location.FullName) {
                    $connected = $true
                    break
                }
                Write-Host "Current connection: $currentLocation" -ForegroundColor DarkGray
            }
            Start-Sleep -Seconds 1
            $timeout--
        }

        if ($connected) {
            Write-Host "Successfully connected to $currentLocation" -ForegroundColor Green
        }
        else {
            Write-Host "Failed to connect to $($location.FullName)" -ForegroundColor Red
            continue
        }

        # Wait 5 minutes with progress
        Write-Host "Next rotation in 5 minutes..." -ForegroundColor DarkGray
        1..300 | ForEach-Object {
            Write-Progress -Activity "VPN Active" -Status "$currentLocation - Time remaining: $(300 - $_) seconds" -PercentComplete ($_/3)
            Start-Sleep -Seconds 1
        }
       
        # Disconnect before next iteration
        & $E×ρréššvpn disconnect | Out-Null
        Start-Sleep -Seconds 5
    }
}
finally {
    & $E×ρréššvpn disconnect | Out-Null
    Write-Host "Disconnected" -ForegroundColor Yellow
}




Preview:
1742099849595.webp
 

About this Thread

  • 2
    Replies
  • 702
    Views
  • 3
    Participants
Last reply from:
Skia Monarch

Trending Topics

Online now

Members online
1,060
Guests online
1,618
Total visitors
2,678

Forum statistics

Threads
2,315,586
Posts
29,183,130
Members
1,182,640
Latest member
re322
Back
Top