2
\$\begingroup\$

I've been working on a script which configures newly imaged computers (power settings, installed apps, etc)

This is what I have so far:

echo off
cls

SET WINDOW_WIDTH=100
SET WINDOW_HEIGHT=42
mode con: cols=%WINDOW_WIDTH% lines=%WINDOW_HEIGHT%

Set SOURCEPATH=\\networkpath
Set JAVAPATH=%SOURCEPATH%\Java\

@echo [TASK] Adding CITY\WorkstationAdmins to Administrators
net localgroup Administrators CITY\WorkstationAdmins /add >Nul 2>&1 && (call :printPass && echo CITY\WorkstationAdmins added to computer) || (call :printFail && echo Failed to add CITY\WorkstationAdmins to Administrators, may already be added)

@echo [TASK] Installing Windows 10 Corporate Pro Key ([XXXXX-XXXXX-XXXXX-XXXXX-XXXXX[0m^)
start slmgr.vbs //b /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX && (call :printPass && echo Windows 10 Pro Key Installed) || (call :printFail && echo Failed to add Windows 10 Pro Key)

@echo [TASK] Installing Java 
call "%JAVAPATH%\jre-6u30-windows-i586.exe" /s && (call :printPass && echo Java installation complete) || (call :printFail && echo Java installation failed)
reg delete HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Run /v SunJavaUpdateSched /f > NUL 2>&1

@echo [TASK] Configuring Remote Registry to Automatically Start
sc.exe config RemoteRegistry start= auto  >Nul 2>&1 && (call :printPass && echo Remote registry will auto start on boot) || (call :printFail && echo Failed to set remote registry to auto start, may already be set)

@echo [TASK] Starting Remote Registry
net start RemoteRegistry  >Nul 2>&1 (call :printPass && echo Remote registry started)  || (call :printFail && echo Failed to start remote registry, may already be started)

@echo [TASK] Disabling Automatic Sleep
powercfg /change standby-timeout-ac 0  >Nul 2>&1  && (call :printPass && echo AC sleep disabled)  || (call :printFail && echo AC sleep still enabled) 
powercfg /change standby-timeout-dc 0  >Nul 2>&1  && (call :printPass && echo DC sleep disabled)  || (call :printFail && echo DC sleep still enabled)

@echo [TASK] Disabling Automatic Hibernation
powercfg /change hibernate-timeout-ac 0  >Nul 2>&1  && (call :printPass && echo AC hibernation disabled)  || (call :printFail && echo AC hibernation still enabled)
powercfg /change hibernate-timeout-dc 0  >Nul 2>&1  && (call :printPass && echo DC hibernation disabled)  || (call :printFail && echo DC hibernation still enabled)

@echo [TASK] Disabling Hybrid Sleep.
powercfg -setacvalueindex SCHEME_BALANCED SUB_SLEEP HYBRIDSLEEP 0 >Nul 2>&1  && (call :printPass && echo AC hybrid sleep disabled)  || (call :printFail && echo AC hybrid sleep still enabled)
powercfg -setdcvalueindex SCHEME_BALANCED SUB_SLEEP HYBRIDSLEEP 0 >Nul 2>&1  && (call :printPass && echo DC hybrid sleep disabled)  || (call :printFail && echo DC hybrid sleep still enabled)

@echo [TASK] Clearing Login Screen.
REG add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI" /v LastLoggedOnDisplayName /t REG_SZ /d "" /f  >Nul 2>&1 && (call :printPass && echo Removed last logged on DisplayName)  || (call :printFail && echo Could not remove last logged on DisplayName) 
REG add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI" /v LastLoggedOnSAMUser /t REG_SZ /d "" /f  >Nul 2>&1 && (call :printPass && echo Removed last logged on SAMUser)  || (call :printFail && echo Could not remove last logged on SAMUser) 
REG add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI" /v LastLoggedOnUser /t REG_SZ /d "" /f  >Nul 2>&1  && (call :printPass && echo Removed last logged on User) || (call :printFail && echo Could not remove last logged on User)

@echo [TASK] Enabling Screensaver Timeout and Password
REG add "HKCU\Software\Policies\Microsoft\Windows\Control Panel\Desktop" /v ScreenSaverIsSecure /t REG_SZ /d 1 /f >Nul  && (call :printPass && echo  Set Screensaver password) || (call :printFail && echo Could not set Screensaver password)
REG add "HKCU\Software\Policies\Microsoft\Windows\Control Panel\Desktop" /v ScreenSaveTimeOut /t REG_SZ /d 900 /f >Nul && (call :printPass && echo  Set Screensaver password) || (call :printFail && echo  Could not set Screensaver timeout)

@echo [TASK] Updating group policy
echo n | gpupdate /force /wait:0  >Nul 2>&1  && (call :printPass && echo Group Policy Updated)  || (call :printFail && echo Group Policy update failed)

@echo [INFO] Configuration Complete.
@echo *Check if any errors occured.* 
@echo Press any key to exit. && pause>Nul
goto:eof 

:printFail
call :printInfo
echo |set /p="[31m[FAIL][0m "
EXIT /B 0

:printPass
call :printInfo
echo |set /p="[32m[PASS][0m "
EXIT /B 0

:printInfo
echo|set /p=[INFO] 
EXIT /B 0

As you can see at the bottom, I've started to put stuff in separate functions so that it can be a bit modular.

Right now I'm thinking of putting each section into its own function so that if I want to disable the section, (ex We fix the Java task sequence so that we don't need to install it post imaging), then it is a simple comment out.

Any suggestions or tips, I'm new to batch scripting that why I want to stick on it. It has been suggested to move it to PowerShell which I might do in the future.

EDIT:

I tried what I was already thinking of doing by moving each task into its own function. Also moved some stuff info variables for the readability of the code.

UPDATED CODE:

echo off
cls

SET WINDOW_WIDTH=100
SET WINDOW_HEIGHT=40
mode con: cols=%WINDOW_WIDTH% lines=%WINDOW_HEIGHT%

SET SOURCEPATH=\\networkpath
SET JAVAPATH="%SOURCEPATH%\Java.exe"

SET WINDOWS_KEY=XXXXX-XXXXX-XXXXX-XXXXX-XXXXX

SET LOGIN_REG_PATH=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI
SET SCRSVR_REG_PATH="HKEY_CURRENT_USER\SOFTWARE\Policies\Microsoft\Windows\Control Panel\Desktop"

call :addDomainStuff
call :installKey
call :installJava
call :setDefaultApps
call :configRemote
call :disableSleep
call :clearLogin
call :setScreenSaver
call :pullGroupPolicy

@echo [INFO] Configuration Complete.
@echo *Check if any errors occured.* 
@echo Press any key to exit. && pause>Nul
goto:eof 


:addDomainStuff
@echo [TASK] Adding CITY\WorkstationAdmins to Administrators
net localgroup Administrators CITY\WorkstationAdmins /add >Nul 2>&1 && (call :echoPass "CITY\WorkstationAdmins added to computer") || (call :echoFail "Failed to add CITY\WorkstationAdmins to Administrators, may already be added")
EXIT /B 0

:installKey
@echo [TASK] Installing Windows 10 Corporate Pro Key ([93m%WINDOWS_KEY%[0m^)
start slmgr.vbs //b /ipk %WINDOWS_KEY% && (call  :echoPass "Windows 10 Pro Key Installed") || (call :echoFail "Failed to add Windows 10 Pro Key")
EXIT /B 0

:configRemote
@echo [TASK] Configuring Remote Registry to Automatically Start
sc.exe config RemoteRegistry start= auto  >Nul 2>&1 && (call :echoPass "Remote registry will auto start on boot") || (call :echoFail "Failed to set remote registry to auto start, may already be set")

@echo [TASK] Starting Remote Registry
net start RemoteRegistry  >Nul 2>&1 (call :echoPass "Remote registry started")  || (call :echoFail "Failed to start remote registry, may already be started")
EXIT /B 0


:disableSleep
@echo [TASK] Disabling Hybrid Sleep.
powercfg -setacvalueindex SCHEME_BALANCED SUB_SLEEP HYBRIDSLEEP 0 >Nul 2>&1  && (call :echoPass "AC hybrid sleep disabled")  || (call :echoFail "AC hybrid sleep still enabled")
powercfg -setdcvalueindex SCHEME_BALANCED SUB_SLEEP HYBRIDSLEEP 0 >Nul 2>&1  && (call :echoPass "DC hybrid sleep disabled")  || (call :echoFail "DC hybrid sleep still enabled")

@echo [TASK] Disabling Automatic Sleep
powercfg /change standby-timeout-ac 0  >Nul 2>&1  && (call :echoPass "AC sleep disabled")  || (call :echoFail "AC sleep still enabled") 
powercfg /change standby-timeout-dc 0  >Nul 2>&1  && (call :echoPass "DC sleep disabled")  || (call :echoFail "DC sleep still enabled")

@echo [TASK] Disabling Automatic Hibernation
powercfg /change hibernate-timeout-ac 0  >Nul 2>&1  && (call  :echoPass "AC hibernation disabled")  || (call :echoFail "AC hibernation still enabled")
powercfg /change hibernate-timeout-dc 0  >Nul 2>&1  && (call  :echoPass "DC hibernation disabled")  || (call :echoFail "DC hibernation still enabled")
EXIT /B 0


:clearLogin
@echo [TASK] Clearing Login Screen.
REG add %LOGIN_REG_PATH% /v LastLoggedOnDisplayName /t REG_SZ /d "" /f  >Nul 2>&1 && (call :echoPass "Removed last logged on DisplayName")  || call :echoFail "Could not remove last logged on DisplayName") 
REG add %LOGIN_REG_PATH% /v LastLoggedOnSAMUser /t REG_SZ /d "" /f  >Nul 2>&1 && (call :echoPass "Removed last logged on SAMUser")  || (call :echoFail "Could not remove last logged on SAMUser") 
REG add %LOGIN_REG_PATH% /v LastLoggedOnUser /t REG_SZ /d "" /f  >Nul 2>&1  && (call :echoPass "Removed last logged on User") || (call :echoFail "Could not remove last logged on User")
EXIT /B 0


:setScreenSaver
@echo [TASK] Enabling Screensaver Timeout and Password
REG add %SCRSVR_REG_PATH% /v ScreenSaverIsSecure /t REG_SZ /d 1 /f >Nul  && (call :echoPass "Set Screensaver password") || (call :echoFail "Could not set Screensaver password")
REG add %SCRSVR_REG_PATH% /v ScreenSaveTimeOut /t REG_SZ /d 900 /f >Nul && (call :echoPass "Set Screensaver password") || (call :echoFail "Could not set Screensaver timeout")
EXIT /B 0


:installJava
@echo [TASK] Installing Java 
call %JAVAPATH% /s && (call :echoPass "Java installation complete") || (call :echoFail "Java installation failed, may already be installed")
reg delete HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Run /v SunJavaUpdateSched /f > NUL 2>&1
EXIT /B 0


:pullGroupPolicy
@echo [TASK] Updating group policy
echo n | gpupdate /force /wait:0  >Nul 2>&1  && (call :echoPass "Group Policy Updated")  || (call :echoFail "Group Policy update failed")
EXIT /B 0

:setDefaultApps
@echo [TASK] Changing default browser to chrome
echo not configure

@echo [TASK] Changing pdf reader to adobe
echo not configure
EXIT /B 0

:printFail
call :printInfo
echo |set /p="[31m[FAIL][0m "
EXIT /B 0

:printPass
call :printInfo
echo |set /p="[32m[PASS][0m "
EXIT /B 0

:printInfo
echo|set /p=[INFO] 
EXIT /B 0

:echoFail
call :printFail
echo %~1
EXIT /B 0

:echoPass
call :printPass
echo %~1
EXIT /B 0
\$\endgroup\$
  • \$\begingroup\$ Java 6 — really? Also, have you considered Active Directory? \$\endgroup\$ – 200_success Jul 15 at 19:43
  • \$\begingroup\$ @200_success java 6 because a whole bunch of apps are still using it and for the active directory, I don't have permission/access to a whole bunch of functions and tools \$\endgroup\$ – Exikle Jul 15 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.