Bat file script to PS script; check NET; start VPN when success start another.

Welcome Forums General PowerShell Q&A Bat file script to PS script; check NET; start VPN when success start another.

Viewing 3 reply threads
  • Author
    Posts
    • #231961
      Participant
      Topics: 1
      Replies: 2
      Points: 1
      Rank: Member

      Hello all,

      Please, I am beginner. I am playing with PS an I have problem with create solution. For me is better compare real .bat file script with .ps1 file script in a real example. There is .bat file with some VPN connection routine. (check internet connection, check for active VPN and create VPN if not exist) . I downloaded it from internet and I little modified it for my scenario.

      Please, can somebody show me this .batch script in powershell script? I understand that the whole sctipt is longer. Maybe for start these parts: “:VPNNOTCONNECTED” “:CHECKDEPT”.

      Thank you in advance.

      Martin

       

      @echo off
      echo.
      echo.
      ECHO Checking for an Internet connection, please wait…
      PING -n 1 http://www.google.com|find “Reply from ” >NUL
      IF NOT ERRORLEVEL 1 goto :SUCCESS
      IF ERRORLEVEL 1 goto :FAILURE
      :SUCCESS
      echo.
      ECHO You have an active Internet connection
      echo.
      goto start

      :FAILURE
      cls
      echo.
      echo.
      ECHO You do not have an active Internet connection
      echo Please check your WiFi Connection and try again?
      echo.
      pause
      exit

      :start
      REM Try to ping a local server inside my network
      echo Checking to see if your connected via VPN or at the Office…
      PING -n 1 192.168.1.3|find “Reply from ” >NUL
      IF NOT ERRORLEVEL 1 goto :CHECKDEPT
      IF ERRORLEVEL 1 goto :STARTVPN

      :STARTVPN
      echo Checking to see if VPN is already loaded…
      echo.
      tasklist /FI “IMAGENAME eq openvpn.exe” | find /i “openvpn.exe”
      IF ERRORLEVEL 2 GOTO VPNEXIST
      IF ERRORLEVEL 1 GOTO VPNNOEXIST

      :VPNEXIST
      goto EXIT

      :VPNNOEXIST
      goto VPNNOTCONNECTED

      :EXIT
      tasklist /FI “IMAGENAME eq openvpn.exe” | find /i “openvpn.exe”
      IF ERRORLEVEL 2 GOTO VPNCONNECTED
      IF ERRORLEVEL 1 GOTO VPNNOTCONNECTED

      :VPNCONNECTED
      GOTO checkdept

      :VPNNOTCONNECTED
      taskkill /f /im openvpn.exe
      taskkill /f /im openvpn-gui.exe
      cls
      echo.
      echo.
      echo Waiting for VPN to reset…
      CHOICE /C X /T 4 /D X > NUL
      start C:\PROGRA~1\OpenVPN\bin\openvpn-gui.exe –connect test-user.ovpn
      ECHO.
      ECHO.
      CHOICE /C X /T 2 /D X > NUL
      goto :checkdept

      :CHECKDEPT
      cls
      echo.
      echo Waiting for a solid VPN connection… Please Wait.
      PING -n 1 192.168.1.3|find “Reply from ” >NUL
      IF NOT ERRORLEVEL 1 goto :END
      IF ERRORLEVEL 1 goto :CHECKDEPT

      :END
      REM This starts a batch file…. exit for now.
      exit

       

    • #231991
      Participant
      Topics: 12
      Replies: 1642
      Points: 2,660
      Helping Hand
      Rank: Community Hero

      Martin_CZ_24, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      We’d be happy to assist you, but it would be appreciated if you actually posted some Powershell code that you tested and we can assist from there rather than someone writing it for you. Below is the code you posted with commands that you need to do the same thing in Powershell. Use Get-Help or search for the commands and there will be documentation and tons of examples. Post back when you get stuck with the code formatted as explained above.

      @echo off
      echo.
      echo.
      ECHO Checking for an Internet connection, please wait…
      ## Test-Connection ##
      PING -n 1 http://www.google.com|find “Reply from ” >NUL
      IF NOT ERRORLEVEL 1 goto :SUCCESS
      IF ERRORLEVEL 1 goto :FAILURE
      :SUCCESS
      echo.
      ECHO You have an active Internet connection
      echo.
      goto start
      
      :FAILURE
      cls
      echo.
      echo.
      ECHO You do not have an active Internet connection
      echo Please check your WiFi Connection and try again?
      echo.
      pause
      exit
      
      :start
      REM Try to ping a local server inside my network
      echo Checking to see if your connected via VPN or at the Office…
      ## Test-Connection ##
      PING -n 1 192.168.1.3|find “Reply from ” >NUL
      IF NOT ERRORLEVEL 1 goto :CHECKDEPT
      IF ERRORLEVEL 1 goto :STARTVPN
      
      :STARTVPN
      echo Checking to see if VPN is already loaded…
      echo.
      ## Get-Process ##
      tasklist /FI “IMAGENAME eq openvpn.exe” | find /i “openvpn.exe”
      IF ERRORLEVEL 2 GOTO VPNEXIST
      IF ERRORLEVEL 1 GOTO VPNNOEXIST
      
      :VPNEXIST
      goto EXIT
      
      :VPNNOEXIST
      goto VPNNOTCONNECTED
      
      :EXIT
      ## Get-Process ##
      tasklist /FI “IMAGENAME eq openvpn.exe” | find /i “openvpn.exe”
      IF ERRORLEVEL 2 GOTO VPNCONNECTED
      IF ERRORLEVEL 1 GOTO VPNNOTCONNECTED
      
      :VPNCONNECTED
      GOTO checkdept
      
      :VPNNOTCONNECTED
      
      ## Stop-Process ##
      taskkill /f /im openvpn.exe
      taskkill /f /im openvpn-gui.exe
      cls
      echo.
      echo.
      echo Waiting for VPN to reset…
      ## Read-Host ##
      CHOICE /C X /T 4 /D X > NUL
      ## Start-Process ##
      start C:\PROGRA~1\OpenVPN\bin\openvpn-gui.exe –connect test-user.ovpn
      ECHO.
      ECHO.
      ## Read-Host ##
      CHOICE /C X /T 2 /D X > NUL
      goto :checkdept
      
      :CHECKDEPT
      cls
      echo.
      echo Waiting for a solid VPN connection… Please Wait.
      ## Test-Connection ##
      PING -n 1 192.168.1.3|find “Reply from ” >NUL
      IF NOT ERRORLEVEL 1 goto :END
      IF ERRORLEVEL 1 goto :CHECKDEPT
      
      :END
      REM This starts a batch file…. exit for now.
      exit
      
    • #232276
      Participant
      Topics: 1
      Replies: 2
      Points: 1
      Rank: Member

      Hello

      Thank you a lot for answer and thank you for little corrected sript. It is for me good way.

      I am going to try work with this.

      Martin

    • #236332
      Participant
      Topics: 1
      Replies: 2
      Points: 1
      Rank: Member

      Hello,

      I am sorry I am trying to put the code correctly. (tried to make the code shorter)

      I have problem with this part:

      how to do, that the script will waits for result? (established or N/A)
      how to do, that an user not needs put in user name / password (user: tester password: test) ?

      I found in internet that it is not simple.

      C:\PROGRA~1\OpenVPN\bin\openvpn-gui.exe --connect test-user.ovpn

      There is a link to github:
      VPNprepare

      ## Test-Connection ## INTERNET ##
      $TestNetDNS = Test-Connection -Count 1 www.google.com -ErrorAction SilentlyContinue
      if ($TestNetDNS -ne $null)
      {
      Write-Host "Internet available" -ForegroundColor Green
      Stop-Process -Name "openvpn-gui" -Force 2> $null
      Stop-Process -Name "openvpn" -Force 2> $null
      ##1 second waiting for OpenVpn flush##
      Start-Sleep -Seconds 1
      C:\PROGRA~1\OpenVPN\bin\openvpn-gui.exe --connect test-user.ovpn
        }
      else
      {
      Write-Host "Internet not available" -ForegroundColor Red
      Write-Host "Try restart PC or check internet connection" -ForegroundColor Red
      pause
      exit
      }
      
      • This reply was modified 2 weeks, 6 days ago by Martin_CZ_24.
Viewing 3 reply threads
  • You must be logged in to reply to this topic.