D
D
demonca132020-09-15 08:22:21
openvpn
demonca13, 2020-09-15 08:22:21

Running service restart script with Task Scheduler?

Hello, I am in a difficult position.
I have machines with Windows 7 that connect via OPEN VPN, the gateway has Fail Over, if the first operator falls, Mikrotik will change to another one. The script checks the availability of the first gateway every 10 minutes, if it is not available, replaces the config file with a backup one, everything is fine, but there is one problem, in order for the config to take effect, you need to restart the OpenVPN service. Introduced into the service a command to stop start via net, but if the script is launched by the task scheduler, then Access is denied, and if the script is run manually, then everything works.
On computers, only the local administrator and the scheduler runs the script on behalf of the administrator.

I will be glad for any help.

The script itself

spoiler
@setlocal enableextensions enabledelayedexpansion
@echo off
set ipaddr1= Основной_ИП
set state1=down

for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr1!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state1=up
)
echo.Main is !state1!
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr1!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state1=up
)
echo.Main is !state1!
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr1!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state1=up
)
echo.Main is !state1!

set ipaddr2= Резервный_ип
set state2=down

for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr2!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state2=up
)
echo.Backup is !state2!
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr2!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state2=up
)
echo.Backup is !state2!
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr2!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state2=up
)
echo.Backup is !state2!

if %state1% NEQ up (
if %state2% NEQ up (
echo.No internet
echo.Do nothing
) else (
echo.Backup is working
fc "c:\Program Files\OpenVPN\config\client.ovpn-резерв" "c:\Program Files\OpenVPN\config\client.ovpn" > nul
if !ERRORLEVEL! EQU "0" (
echo.Continiue to use backup line
) else (
copy "c:\Program Files\OpenVPN\config\client.ovpn-резерв" "c:\Program Files\OpenVPN\config\client.ovpn"
ipconfig /flushdns
net stop OpenVPNService & net start openvpnservice
echo.Backup line activated
)
)
) else (
echo.Main is working
fc "c:\Program Files\OpenVPN\config\client.ovpn-основной" "c:\Program Files\OpenVPN\config\client.ovpn" > nul
if !ERRORLEVEL! EQU 0 (
echo.Continiue to use main line
) else (
copy "c:\Program Files\OpenVPN\config\client.ovpn-основной" "c:\Program Files\OpenVPN\config\client.ovpn"
ipconfig /flushdns
net stop OpenVPNService & net start openvpnservice
echo.Main line activated
)
)

endlocal

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2020-09-15
@demonca13

Kill it. OpenVPN knows how to HA/failover itself: https://openvpn.net/community-resources/implementi...

V
Vladimir Kivva, 2020-09-15
@zionkv

Try to run as system. In general, look on the Internet for articles on how to bypass UAC using the task scheduler and there will be exactly the knowledge you need in the output.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question