Location>code7788 >text

[Original] Solve NasCab drop process, NasCab process maintenance

Popularity:715 ℃/2024-09-20 17:15:15

Recently the object complained that the home server can not connect again, can not watch the examination video.
I pulled out my cell phone and tried, and it really didn't connect. The server at home is a Win11 platform with video files managed with NasCab and then an intranet penetration done through frpc.
We're in an outside library, and there are only three things that can't be connected: the server is off, the NasCab intranet penetration plugin is down, and the NasCab process is down.
Logged into the server remotely and realized it was the NasCab process that had dropped. Clicked on NasCab and 1 minute later the object was happily watching exam videos again.
I thought about it and decided to write a script to deal with this, as I've also been experiencing NasCab dropping processes from time to time, which I had previously ignored, but now realize it can affect the experience.
Idea: Write a script that maintains the process, cyclically scans for nascab processes, and turns on NasCab if it doesn't detect it.
Requirement: the script can't be affected by NasCab, and at the same time the script can't affect NasCab, then you need another terminal to turn on nascab, so it's bat->cmd->powershell->NasCab, so that only the powershell window will be affected, and turning off the cmd won't affect NasCab. .
The implementation code is:powershell -command "& {Start-Process %executable%}", where executable is the program to be executed, is nascab.
Note that the path of nascab should be added to path with the path:C:\Users\hpl\AppData\Local\Programs\nascab\, where hpl is replaced with your own username.
Then just let the script boot up and start itself. This can be accomplished by creating a task plan or by dragging it into the startup folder.

The complete code is as follows (first published on the whole site):
Named Gaia because I love playing Zero Dawn and Gaia in it is powerful, hhhh

Click to view code
@echo off
rem Turns off the echo command display

chcp 65001
rem Force UTF-8 on to support Chinese display

rem Get administrator privileges for this script
ver | findstr "10\. [0-9]\. [0-9]*" >nul && goto powershellAdmin

:mshtaAdmin
rem The principle is to use mshta to run a vbscript script to authorize a bat file.
rem Here we use %~dpnx0 with quotes around it to indicate the current script, which is more reliable than the original short %~s0 file name.
rem Two Net sessions are used here, the second one is to check if the authorization is successful, and if the authorization fails, it jumps to the failed tag.
rem This effectively avoids the problem of the bat file continuing to execute after a failure.
Net session >nul 2>&1 || mshta vbscript:CreateObject("").ShellExecute("","/c ""%~dpnx0""","","", "runas",1)()&&exit
Net session >nul 2>&1 || goto failed
goto Admin

:powershellAdmin
rem The principle is to use powershell to authorize the bat file.
rem Here two Net sessions are used, the second one is to check if the authorization is successful, if it fails then it jumps to the failed tag.
rem This avoids the problem of the bat file continuing to execute after a failed attempt.
Net session >nul 2>&1 || powershell start-process \"%0\" -verb runas && exit
Net session >nul 2>&1 || goto failed
goto Admin

:failed
echo The authorization failed, maybe your antivirus software blocked the authorization operation, or you didn't agree to the UAC authorization request.
echo It is recommended that you right-click on this script and select "Run as administrator".
echo Press any key to exit.
pause >nul
nul

:Admin
echo The path where this script is processed: %0
echo Administrator privileges have been granted!
ping 127.0.0.1 -n 1 > nul
echo If the title of this window shows the word "Administrator", it means the authorization has been successful.

echo Trying to move the working directory to the software directory...
cd /d %~dp0
rem Switch the software working directory to the software folder
ping 127.0.0.1 -n 1 > nul
echo Directory transfer successful!

rem Run this script in the background
if "%1" == "h" goto begin
mshta vbscript:createobject("").run("""%~nx0"" h",0)()&&exit
:begin

rem Set the target program here
:: Add Path: C:\Users\hpl\AppData\Local\Programs\nascab\ to the program path.
set executable=nascab
set /a counter=0
set TITLE=GaiaNasCab-background-admin [1.4]

rem set TITLE
title %TITLE%

echo -------------------------------------------------------------------------------- > %TITLE%.log
echo [%DATE%%TIME:~0,8%]This script [%TITLE%] is running in administrator mode! >> %TITLE%.log
echo This script is used to guard %executable% to make sure it is always running >> %TITLE%.log
echo Updated September 19, 2024 by HPL. >> %TITLE%.log
echo -------------------------------------------------------------------------------- >> %TITLE%.log
echo: >> %TITLE%.log
echo: >> %TITLE%.log
echo --------------------------------------------------------------------------------
echo [%DATE%%TIME:~0,8%]This script [%TITLE%] is running in administrator mode!
ping 127.0.0.1 -n 1 > nul
echo This script is used to guard %executable% to make sure it is always running!
ping 127.0.0.1 -n 1 > nul
echo Updated September 19, 2024 by HPL.
echo --------------------------------------------------------------------------------
echo.

echo [%DATE%%TIME:~0,8%] Target program scanning... >> %TITLE%.log
echo: >> %TITLE%.log
echo [%DATE%%TIME:~0,8%]Target program scanning...
echo.


:checkrun
tasklist|findstr /i "%executable%"
if errorlevel 1 (
echo [%DATE%%TIME:~0,8%]Running %executable% not detected, preparing to turn on %executable%... >> %TITLE%.log
echo counter[%counter%] >> %TITLE%.log
echo -------------------------------------------------------------------------------- >> %TITLE%.log
echo [%DATE%%TIME:~0,8%]Running %executable% not detected, preparing to turn on %executable%...
echo counter[%counter%]
echo --------------------------------------------------------------------------------
goto start
)
if errorlevel 0 (
rem The scanning time is recommended to be more than 5 minutes to avoid the program being triggered when updating NasCab.
echo [%DATE%%TIME:~0,8%] found %executable%, scanning in loop [300s]...
echo counter[%counter%]
echo --------------------------------------------------------------------------------
ping 127.0.0.1 -n 300 > nul
goto checkrun
)
ping 127.0.0.1 -n 5 > nul

:start
echo: >> %TITLE%.log
echo [%DATE%%TIME:~0,8%] is enabling %executable%... >> %TITLE%.log
echo.
echo [%%DATE%%TIME:~0,8%] is turning on %executable%...

rem Running NasCab in the background with powershell (for testing)
::powershell -command "& {Start-Process %executable% -WindowStyle Hidden}"

rem Run NasCab with powershell foreground (works well, highly recommended)
powershell -command "& {Start-Process %executable%}"

rem Run NasCab in foreground (disable, this way NasCab will inject the program log into the cmd where this bat is located, shutting down this bat will shut down NasCab at the same time)
::start %executable%
ping 127.0.0.1 -n 1 > nul
echo.
echo [%DATE%%TIME:~0,8%] has successfully turned on %executable%! >> %TITLE%.log
echo [%DATE%%TIME:~0,8%] has successfully turned on %executable%!
echo counter[%counter%] >> %TITLE%.log
echo -------------------------------------------------------------------------------- >> %TITLE%.log
echo: >> %TITLE%.log
echo [%DATE%%TIME:~0,8%]5s to enter run state detection checkrun... >> %TITLE%.log
echo: >> %TITLE%.log
echo: >> %TITLE%.log
echo [%DATE%%TIME:~0,8%]5s into run state detection checkrun...
ping 127.0.0.1 -n 5 > nul
set /a counter+=1
goto checkrun

Effect of use:
When the program runs, it will generate a log in the directory where the bat is located, so you can observe the running status.
image