Newbie help to confirm app is running before continuing .... and other stuff!

Hello folks, I’m very new to PowerShell scripting although I have managed to get a grip of VBA in the last year - which came in very handy for streamlining Excel task in my current job - brownie points (but no bonus !!)

Anyway, I have the PowerShell script below that works but I need it to be a bit more automated.
The script, at the moment does the following:

Asks the user if a VPN app is running (Versa Secure Access) and waits for an input of y or n (no error trapping as yet - so if anything other than a n (or N) is entered it follows the “Else” path of the If statement).
It stores the answer to this question
It edits a text file to change a value
It runs an “If” and dependent on what was entered above:
If it sees a n or N it will run up the VPN app and wait for 30 seconds for the user to log into the VPN.
Then it will run up another app
Finally it will exit

In the “Else” bit of the IF - if a y, Y or any digit is pressed it will just run the 2nd app.

What I would like it to do is:

Not ask a question
When the script is run it will automatically check to see if the VPN is already running, if it is it will run the 2nd app.
If the VPN is not running it will run it but wait until the VPN is connected before it calls the 2nd app (rather that wait for x seconds)
Note - there is an event in the Event log when the VPN connects so thought that could be a route - or maybe using PIDs ?
If after 1 minute the VPN is not seen to connect I want to close the VPN app
Make a change to another text file
Display a bit of text
Run the VPN app again
Wait for the connect message (the text displayed above will have told the user to connect to another VPN node)
Once VPN is connected run the 2nd app

Not much to ask eh !!

The bit I need help with is to check the Event log or PID for confirmation that the VPN is running and then to kill the VPN app if it remains unconnected for a minute

Thanks in advance for any help given.

Regards

Netrix

Current code:
‘’’
#Ask if Versa is already running - store answer as $Versa_State

$Versa_State = Read-Host  -Prompt  ' Is Versa Already Running ? Answer Y or N and press Enter ' 

#Edit boot.cfg file and replace "include all network adapters = false" with "true"

$file = 'C:\ProgramData\IPTrade\Soft Turret\boot.cfg'
$regex = 'application.boot.network.adapter.include.all=false'
(Get-Content $file) -replace $regex, 'application.boot.network.adapter.include.all=true' | Set-Content $file

#Check to see if Versa is running (Looks for y or Y and n or N as a response to the question above)

IF 
    ($Versa_State -icontains 'n')

#If the answer to the initial question re Versa is n or N - then Run the Versa app  

{
#Note to move PowerShell Window on the Screen

    Write-Host " Move This Command Window so that it is not hidden by the Versa Window - then connect to Versa in the usual manner" -ForegroundColor Green
    Write-Host " "

#Start versa Application

    Start-Process -FilePath "C:\Program Files (x86)\Versa Secure Access\versaSecureAccessClient.exe"

#Wait for 30 seconds for Versa to run and for user to log in

    Start-Sleep -s 30

    Write-Host " Ensure Versa has successfully connected BEFORE selecting the Flexpro Network Adapter" -ForegroundColor Green

#Run the FlexPro Application

    Write-Host " "
    Write-Host " [ ...starting the " -NoNewline;
    Write-Host "FlexPro" -ForegroundColor White -BackgroundColor Magenta -NoNewline;
    Write-Host " application]" 
 
    Start-Process -FilePath "C:\Program Files\IPTrade\Soft Turret\FlexPro"

    Start-Sleep -s 3

#Remind User to Select the Correct Network Adapter

    Write-Host " "
    Write-Host " Please Select the Appropriate Network Adapter ie BP-UK-London If the London Node is selected in Versa" -ForegroundColor Green
    Write-Host " "
    Write-Host " [ The FlexPro Window may not be visible - Check on the Taskbar ]" 
    Write-Host " "

    Start-Sleep -s 5

    Write-Host " PowerShell is Closing" -ForegroundColor Green

    Start-Sleep -s 5

}

#If the answer to first question contained a y or Y - just run FlexPro app

    Else

{

    Write-Host " [ ...starting the " -NoNewline;
    Write-Host "FlexPro" -ForegroundColor White -BackgroundColor Magenta -NoNewline;
    Write-Host " application]" 

 #Run the FlexPro Application

    Start-Process -FilePath "C:\Program Files\IPTrade\Soft Turret\FlexPro"

    Start-Sleep -s 3

#Remind User to Select the Correct Network Adapter

    Write-Host " "
    Write-Host " Please Select the Appropriate Network Adapter ie BP-UK-London If the London Node is selected in Versa" -ForegroundColor Green
    Write-Host " "
    Write-Host " [ The FlexPro Window may not be visible - Check on the Taskbar ]"

    Start-Sleep -s 5

    Write-Host " "
    Write-Host " PowerShell is Closing......." -ForegroundColor Green

    Start-Sleep -s 5

}
‘’’

Hi @netrix, welcome to the forums :wave:

What traffic do you allow on the VPN tunnel?

My inclination would be to use Test-NetConnection to something that’s only accessible when the VPN is up. If that’s successful, do what you need to do, if not, establish the VPN connection, re-test, etc.

The problem, in my experience, is that VPN clients can be running even if the tunnel isn’t established so checking for a running process isn’t always a good indicator of whether the tunnel is up.

Windows 10 does have VPN cmdlets such as Get-VPNConnection - I don’t know whether they’re of any use with 3rd party VPN software like you’re using. It’s not something I’ve tested.

Matt, good suggestion. I am pretty sure Get-VPNConnection is specific to Windows built VPN versus third party. Not tested here either. Not many people use the Windows VPN that I am aware of.

Thanks for the suggestion Matt. The VPN is to allow a connection from home broadband into a corporate network - I’m pretty sure it doesn’t “connect” if the tunnel isn’t working which is why I need the help: Every now and then the UK VPN Node [chosen as default for the UK users] has an issue and the VPN will not connect so the end-users will need to connect to another node. When they do this the 2nd app that runs has to be launched differently hence the change to the 2nd text file (boot cfg file)
Does that make sense ?
I am off work until next Weds now but will try your suggestion then.
Thanks again.

Netrix