2016-08-23 102 views
0

我在a.bat一個簡單的腳本:自動啓動/關機sripts不是運行在Windows 10蝙蝠

@echo off 

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

,我想在系統啓動時運行,而該文件,這是我想的副本在系統關閉時運行。 我已經將這些文件添加到gpedit.msc-> scripts-> autostart/shutdown,但是它們都不起作用。 這是爲什麼?我怎樣才能管理它的工作?

+0

你在哪裏編輯這些腳本?正確的部分將是Windows設置>腳本>關機/啓動 – Steven

+0

我已經將它們添加到gpedit.msc->啓動/關機 – user6746473

+0

我找不到'windows設置/腳本' – user6746473

回答

0

正如我的評論中提到的,鍵入gpedit.msc並導航到Windows設置>腳本>啓動/關機。

查看照片瞭解我在哪裏。 Scripts - gpedit.msc

+0

這就是我做了兄弟。我添加了腳本啓動和關閉那裏,似乎沒有任何工作 – user6746473

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文件並執行一次沒有參數。它將在Local policy group中創建starting and shutting down scripts。您可以執行your_name.bat delete刪除腳本。

請注意,如果您已經設置了開始和/或關閉 腳本,它們可能會被覆蓋。

你需要管理權限execute registry operations,但 如果你能打開gpedit.msc這會不會是你的情況

一旦你設置你的腳本,你可以刪除所有if /i "%~1 ...部分和all the subroutines等等該腳本會很簡單。