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
}
‘’’