Can you update the script so that the following behaviour happens? The following…
- User
Can you update the script so that the following behaviour happens?
The following messages should be shown at the END of the script execution, below everything else. Ideally, if no part of the script fails, it should print "Las rutas han sido añadidas correctamente, ya puede usar la VPN" If any part of the script has failed, it should be printed at the end, along with the message "Please contact your IT administrator copying this message", so the script adds two routes, it should print which route failed and why.
@echo off REM ------------------------------------------------- REM Batch script to add two static routes via VPN REM -------------------------------------------------
REM Ensure the script is running as Administrator openfiles >nul 2>&1 if %errorlevel% NEQ 0 ( echo This script requires administrative privileges. echo Please run as Administrator. pause REM exit /b )
REM ------------------------------------------------- REM Define Variables REM ------------------------------------------------- set "VPN_ALIAS=GrecoTormenta"
REM Define server IPs set "SERVER_IP1=10.207.10.114" set "SERVER_IP2=10.24.10.49"
set "SUBNET_MASK=255.255.255.255"
REM Initialize route status variables set "ROUTE1_OK=0" set "ROUTE2_OK=0"
REM ------------------------------------------------- REM Retrieve Interface Index using PowerShell REM ------------------------------------------------- for /f "usebackq tokens=* delims=" %%j in (
powershell -NoProfile -Command ^ "Get-NetIPConfiguration -InterfaceAlias '%VPN_ALIAS%' | Select-Object -ExpandProperty InterfaceIndex") do set "IFINDEX=%%j"REM Check if Interface Index was found if "%IFINDEX%"=="" ( echo Error: Interface Index for VPN connection "%VPN_ALIAS%" not found. echo Please verify the VPN alias and try again. pause REM exit /b )
REM ------------------------------------------------- REM Retrieve VPN IPv4 Address using PowerShell REM ------------------------------------------------- for /f "usebackq tokens=* delims=" %%g in (
powershell -NoProfile -Command ^ "Get-NetIPAddress -InterfaceAlias '%VPN_ALIAS%' -AddressFamily IPv4 | Select-Object -ExpandProperty IPAddress") do set "GATEWAY=%%g"REM Check if IPv4 Address was found if "%GATEWAY%"=="" ( echo Error: IPv4 Address for VPN connection "%VPN_ALIAS%" not found. echo Please verify the VPN connection and try again. pause REM exit /b )
REM ------------------------------------------------- REM Add Route for SERVER_IP1 REM ------------------------------------------------- route add %SERVER_IP1% mask %SUBNET_MASK% %GATEWAY% IF %IFINDEX% >nul 2>&1 if %errorlevel% EQU 0 ( set "ROUTE1_OK=1" ) else ( set "ROUTE1_OK=0" )
route -p add %SERVER_IP1% mask %SUBNET_MASK% %GATEWAY% IF %IFINDEX% >nul 2>&1 REM Persistent route addition failure is treated the same as non-persistent route for simplicity
REM ------------------------------------------------- REM Add Route for SERVER_IP2 REM ------------------------------------------------- route add %SERVER_IP2% mask %SUBNET_MASK% %GATEWAY% IF %IFINDEX% >nul 2>&1 if %errorlevel% EQU 0 ( set "ROUTE2_OK=1" ) else ( set "ROUTE2_OK=0" )
- Assistant