PowerShell – Check VPN Connection, Wait Until Session Has Ended, Upgrade VPN

email me

$VPNStatus = ""
$Status = ""
$PackagePath = $MyInvocation.MyCommand.Path
$dir = Split-Path $PackagePath
$dir = "c:\VPNSetup"
$ErrorActionPreference= 'silentlycontinue'
function CheckVPNStatus
    $VPNStatus = Get-WmiObject -Query "Select * from Win32_NetworkAdapter where (Name like '%Juniper%') and NetEnabled='True'"
    $VPNStatus = [bool]$VPNStatus
    return $VPNStatus
$Status = CheckVPNStatus
While ($Status) { 
    Write-Host "Status: Connected to VPN"
    Start-Sleep -s 300 # wait 5 minutes
    $Status = CheckVPNStatus
Write-Host "Status: Not connected to VPN`n"
# begin install - install with shared install enabled
Start-Process $dir\Install.exe -ArgumentList "/I $dir\setup64.msi /qn"
# wait for install to complete
Get-Process -Name Install -ErrorAction SilentlyContinue | Wait-Process
# message box
[System.Windows.Forms.MessageBox]::Show('Your VPN software has been upgraded. Please reboot at your earliest convenience.', 'Pulse Secure Upgrade', 'Ok', 'Info')
$VPNStatus = ""
$Status = ""
$dir = ""
$PackagePath = ""


Install.exe = msiexec.exe. I copy and use it in the package to differentiate the MSI installer in task manager, and other monitoring utilities. I normally do the same for mshta.exe as well.

For Shared Install, see Pulse Disconnecting Users – Shared Install


tags: MrNetTek