自动启动/关闭sripts没有在Windows 10 bat上运行

时间:2016-08-23 06:30:44

标签: batch-file cmd

我在a.bat中有一个简单的脚本:

curl -X PUT $HOST/_config/admins/$1 -d '"'$2'"'

我想在系统启动时运行,以及该文件的副本,我希望在系统关闭时运行该文件。 我已将这些文件添加到gpedit.msc->脚本 - >自动启动/关闭,但它们不起作用。 这是为什么?如何管理它?

2 个答案:

答案 0 :(得分:1)

更复杂但纯粹的批处理解决方案(在Windows 8中工作)

  

您可能需要修改一些默认值

@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

将其保存为your_name.bat文件,并在不带参数的情况下执行一次。它会在starting and shutting down scripts中创建Local policy group。您可以执行your_name.bat delete删除脚本。

  

请注意,如果您已经设置了开始和/或关闭   脚本,它们可能会被覆盖。

     

您需要execute registry operations的管理权限,但是   如果你能够打开gpedit.msc这不是你的情况

设置完脚本后,您可以删除所有if /i "%~1 ...部分和all the subroutines,这样脚本就很简单。

答案 1 :(得分:0)

如我的评论中所述,输入gpedit.msc并导航到Windows设置>脚本>启动/关闭。

查看图片,了解我的位置。Scripts - gpedit.msc