简体   繁体   中英

Autostart/shutdown sripts not running on windows 10 bat

I have a simple script in a.bat:

@echo off

echo Date: %date% >> C:\Users\Anon\Desktop\start.txt

which I want to run at the system start, and copy of that file, which I want to run at system shutdown. I've added these files to gpedit.msc->scripts->autostart/shutdown, but non of them works. Why is that? How can I manage it to work?

More complex, but pure batch solution (working here in windows 8)

You may need to edit some default values

@echo off

rem get arguments: these will be flushed to log file
set "action=%*"

rem log file name
set "logFile=%~dpn0.log"

rem check for delete keyword.
if /i "%~1" EQU "delete" (
  call:deleteScripts && exit/B 0 || (echo/ Scripts deleted: Failed & exit/B 1)
) else (
  call:testScripts || (echo/ Scripts created: Failed & exit/B 1)
)

rem language independent time
for /f "tokens=2 delims==" %%a in ('wmic os get localdatetime /value') do set "Tm=%%a"

set "timeStamp=%Tm:~0,4%-%Tm:~4,2%-%Tm:~6,2% %Tm:~8,2%:%Tm:~10,2%:%Tm:~12,2%"

if not exist "%logFile%" (
  (
    echo/----------------------------------------------------------------------------
    echo/----------------------------------------------------------------------------
    echo/
    echo/Log registry [%~dpnx0]
    echo/
  )
)>>"%logFile%"

echo(%timeStamp% %action%>>"%logFile%"

exit/B

rem delete registry keys.
:deleteScripts
set "baseKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts"
set "machKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts"
reg delete "%baseKey%\Startup\0" /F >NUL 2>&1
reg delete "%machKey%\Startup\0" /F >NUL 2>&1
reg delete "%baseKey%\Shutdown\0" /F >NUL 2>&1
reg delete "%machKey%\Shutdown\0" /F >NUL 2>&1
echo/ Scripts deleted: Success
exit/B 0

rem check registry keys. if they don't exist create them
:testScripts
set/a msg=0
set "baseKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\Scripts"
set "machKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Group Policy\State\Machine\Scripts"
reg query "%baseKey%\Startup\0">NUL 2>&1 || (call:createScript "Startup" || exit/B 1)
reg query "%machKey%\Startup\0">NUL 2>&1 || (call:createScript "Startup" || exit/B 1)
reg query "%baseKey%\Shutdown\0">NUL 2>&1 || (call:createScript "Shutdown" || exit/B 1)
reg query "%machKey%\Shutdown\0">NUL 2>&1 || (call:createScript "Shutdown" || exit/B 1)
if %msg% equ 1 echo/ Scripts created: Success
exit/B 0

:createScript
set/a msg=1
setlocal
set "scriptKey=%~1"
set "logMessage=Computer %%COMPUTERNAME%% [%~1]"

rem needed for creating registry keys
set "scriptName=%~dpnx0"

rem English users
set "GP_Name=Local group policy" & rem set "GP_Name=Local Computer policy"
rem Spanish users
rem set "GP_Name=Directiva de grupo local" & rem set "GP_Name=Directiva Equipo local"

reg add "%baseKey%\%scriptKey%\0" /V "GPO-ID" /T REG_SZ /D "LocalGPO" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%baseKey%\%scriptKey%\0" /V "SOM-ID" /T REG_SZ /D "Local" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%baseKey%\%scriptKey%\0" /V "FileSysPath" /T REG_SZ /D "C:\\Windows\\System32\\GroupPolicy\\Machine" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%baseKey%\%scriptKey%\0" /V "DisplayName" /T REG_SZ /D "%GP_Name%" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%baseKey%\%scriptKey%\0" /V "GPOName" /T REG_SZ /D "%GP_Name%" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%baseKey%\%scriptKey%\0" /V "PSScriptOrder" /T REG_DWORD /D "00000001" /F >NUL 2>&1 || (EndLocal & exit/B 1)

reg add "%baseKey%\%scriptKey%\0\0" /V "Script" /T REG_SZ /D "%scriptName:\=\\%" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%baseKey%\%scriptKey%\0\0" /V "Parameters" /T REG_SZ /D "%logMessage%" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%baseKey%\%scriptKey%\0\0" /V "IsPowershell" /T REG_DWORD /D "00000000" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%baseKey%\%scriptKey%\0\0" /V "ExecTime" /T REG_BINARY /D "00000000000000000000000000000000" /F >NUL 2>&1 || (EndLocal & exit/B 1)

reg add "%machKey%\%scriptKey%\0" /V "GPO-ID" /T REG_SZ /D "LocalGPO" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%machKey%\%scriptKey%\0" /V "SOM-ID" /T REG_SZ /D "Local" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%machKey%\%scriptKey%\0" /V "FileSysPath" /T REG_SZ /D "C:\\Windows\\System32\\GroupPolicy\\Machine" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%machKey%\%scriptKey%\0" /V "DisplayName" /T REG_SZ /D "%GP_Name%" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%machKey%\%scriptKey%\0" /V "GPOName" /T REG_SZ /D "%GP_Name%" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%machKey%\%scriptKey%\0" /V "PSScriptOrder" /T REG_DWORD /D "00000001" /F >NUL 2>&1 || (EndLocal & exit/B 1)

reg add "%machKey%\%scriptKey%\0\0" /V "Script" /T REG_SZ /D "%scriptName:\=\\%" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%machKey%\%scriptKey%\0\0" /V "Parameters" /T REG_SZ /D "%logMessage%" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%machKey%\%scriptKey%\0\0" /V "ErrorCode" /T REG_DWORD /D "00000000" /F >NUL 2>&1 || (EndLocal & exit/B 1)
reg add "%machKey%\%scriptKey%\0\0" /V "ExecTime" /T REG_BINARY /D "00000000000000000000000000000000" /F >NUL 2>&1 || (EndLocal & exit/B 1)
EndLocal
exit/B 0

Save it as your_name.bat file and execute once without arguments. It will create the starting and shutting down scripts in the Local policy group . You may execute your_name.bat delete to delete the scripts.

Be aware that if you have already set an starting and/or shutting down script, they may be overwritten.

You need administrative rights to execute registry operations , but if you're able to open gpedit.msc this won't be your case

Once you have setup your scripts you can remove all the if /i "%~1 ... section and all the subroutines so the script would be simple.

As mentioned in my comment, type in gpedit.msc and navigate to Windows Settings > Scripts > Startup/Shutdown.

Check the picture to understand where I am at. 脚本-gpedit.msc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM