0

I am writing a batch file, this batch file will be distributed to many computers in our company by a agent software, and run on them.

It will install some patches on target computer, and need five times restart. the batch file will register itself in HKLM\Software\Microsfot\Windows\CurrentVersion\Run, the problem is all those computer have password, my batch file can only run after user login.

the whole process will take 1.5 hours, I don't want users to sit in front their computer and wait to input password.

Is there any way to temporarily clear user's password or auto login a user ?

here is my batch file:

@echo off

color 1a
mode con:cols=120 lines=50

setlocal enableextensions enabledelayedexpansion

rem 判断是否以管理员运行
fltmc 1>nul  || (
    echo 未用管理员运行!退出执行
    exit 
)

if "%PROCESSOR_ARCHITECTURE%"=="x86" (set ARCH=x86) else (set ARCH=x64)

for /f "tokens=4-8 delims=[.] " %%i in ('ver') do @(if %%i==Version (set verstr=%%j.%%k.%%l.%%m) else (set verstr=%%i.%%j.%%k.%%l))

for /f "tokens=1-5 delims=." %%i in ('echo %verstr%') do @(
    set OSV=%%i.%%j
    set BLD=%%k
    set MIV=%%l
)

set tempdir=edgesetup
set logfile=edgesetup.log
set cdn=http://x.x.x.x/soft

if %OSV% neq 6.1 echo not windows 7 && exit

call :Resume
goto %current%
goto :eof

rem =================== step1 ===================
:one
mkdir C:\%tempdir%
reg add HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /d %~dpnx0 /f >>c:\%logfile% 2>&1
echo two >%~dp0current.txt
call:installkb 3020369
call:installkb 3125574
call:installkb 4490628
shutdown -r -t 0
goto :eof

rem =================== step2 ===================
:two
echo three >%~dp0current.txt
call:installkb 4507437
shutdown -r -t 0
goto :eof

rem =================== step3 ===================
:three
echo four >%~dp0current.txt
call:installkb 2533623
call:installkb 2670838
call:installkb 2731771
call:installkb 2729094
call:installkb 2786081
call:installkb 2834140
shutdown -r -t 0
goto :eof

rem =================== step 4 ===================
:four
echo five >%~dp0current.txt

echo ---------------------------- 安装IE 11中... ----------------------------
echo ---------------------------- 安装IE 11 ---------------------------- >>c:\%logfile% 2>&1
powershell -c "(new-object System.Net.WebClient).DownloadFile('%cdn%/win7/%ARCH%/IE11-Windows6.1-%ARCH%-zh-cn.exe','C:\%tempdir%\IE11-Windows6.1-%ARCH%-zh-cn.exe')" >>c:\%logfile% 2>&1
C:\%tempdir%\IE11-Windows6.1-%ARCH%-zh-cn.exe /quiet /update-no /closeprograms /norestart >>c:\%logfile% 2>&1
if %ErrorLevel% equ 0 echo IE 11安装/升级成功
call:installkb 4511872
shutdown -r -t 0
goto :eof

rem =================== step 5 ===================
:five
reg delete HKLM\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /f
del %~dp0current.txt

echo ---------------------------- 安装OA控件中... ----------------------------
echo ---------------------------- 安装OA控件 ---------------------------- >>c:\%logfile% 2>&1
powershell -c "(new-object System.Net.WebClient).DownloadFile('%cdn%/EcologyPlugin.zip','C:\%tempdir%\EcologyPlugin.zip')" >>c:\%logfile% 2>&1
powershell -c "(new-object System.Net.WebClient).DownloadFile('%cdn%/unzip.exe','C:\%tempdir%\unzip.exe')" >>c:\%logfile% 2>&1
C:\%tempdir%\unzip.exe C:\%tempdir%\EcologyPlugin.zip -d C:\%tempdir% >>c:\%logfile% 2>&1
call C:\%tempdir%\EcologyPlugin\setup.bat >>c:\%logfile% 2>&1
if %ErrorLevel% equ 0 echo OA控件安装成功

echo ---------------------------- 安装edge浏览器中... ----------------------------
echo ---------------------------- 安装edge浏览器 ---------------------------- >>c:\%logfile% 2>&1
reg add HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\EdgeUpdate /v Allowsxs /t REG_DWORD /d 1 /f >>c:\%logfile% 2>&1
powershell -c "(new-object System.Net.WebClient).DownloadFile('%cdn%/MicrosoftEdgeEnterprise%ARCH%.msi','C:\%tempdir%\MicrosoftEdgeEnterprise%ARCH%.msi')" >>c:\%logfile% 2>&1
C:\%tempdir%\MicrosoftEdgeEnterprise%ARCH%.msi /quiet >>c:\%logfile% 2>&1
if %ErrorLevel% equ 0 echo Edge浏览器安装成功

echo ---------------------------- 设置组策略中... ----------------------------
echo ---------------------------- 设置组策略 ---------------------------- >>c:\%logfile% 2>&1
powershell -c "(new-object System.Net.WebClient).DownloadFile('%cdn%/LGPO.exe','C:\%tempdir%\LGPO.exe')" >>c:\%logfile% 2>&1
powershell -c "(new-object System.Net.WebClient).DownloadFile('%cdn%/gp.txt','C:\%tempdir%\gp.txt')" >>c:\%logfile% 2>&1
C:\%tempdir%\LGPO.exe /t C:\%tempdir%\gp.txt >>c:\%logfile% 2>&1
if %ErrorLevel% equ 0 echo 组策略设置成功

reg add HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce /v clear-edge-setup /d "cmd /C \"rd /Q /S C:\%tempdir%\"" /f >>c:\%logfile% 2>&1

shutdown -r -t 0
goto :eof

rem =================== 恢复执行 ===================
:resume
if exist %~dp0current.txt (
    set /p current=<%~dp0current.txt
    taskkill /F /IM explorer.exe
) else (
    set current=one
)
goto :eof

:installkb
echo=
echo ------------------------ 安装补丁%~1中,请稍候...请勿关闭窗口 -------------------------
echo=
echo ---------------------------- 安装补丁%~1 ---------------------------- >>c:\%logfile% 2>&1
mkdir C:\%tempdir%\%~1
set KB=%~1
powershell -c "(new-object System.Net.WebClient).DownloadFile('%cdn%/win7/%ARCH%/windows6.1-kb%KB%-%ARCH%.msu','C:\%tempdir%\windows6.1-kb%KB%-%ARCH%.msu')" >>c:\%logfile% 2>&1
expand -F:* C:\%tempdir%\windows6.1-kb%KB%-%ARCH%.msu C:\%tempdir%\%KB% >>c:\%logfile% 2>&1
for %%i in (C:\%tempdir%\%~1\*KB%KB%*.cab) do (
    echo %%i >>c:\%logfile% 2>&1
    DISM.exe /Online /Add-Package /PackagePath:%%i /NoRestart >>c:\%logfile% 2>&1
    systeminfo | findstr "KB%KB%"
    if %ErrorLevel% equ 0 echo 补丁KB%KB%安装成功
)
exit /B 0

endlocal
2
  • You should fully deploy the image to the machine before the user even has the opportunity to log into their machine. You don’t need their password. You also don’t need to clear it. You can have an account that exists that your able to use remotely. However, what you describe, doesn’t exist. An Administrator cannot view a user’s password only reset it.. Once the password is changed the previous password cannot be restored.
    – Ramhound
    Aug 14, 2020 at 7:19
  • Microsoft also makes software to deploy software packages like what your script installs remotely
    – Ramhound
    Aug 14, 2020 at 7:20

1 Answer 1

2

You can't get the user password even you after log in, using a batch file, but reset it (or clear it, without knowing the old password)!

You can run net user username * and press enter. Now the password will be cleared and not required. Later when batch file is finished then you can reset password again.

there is also GUI way. Before running the batch file you can configure user's settings to don't require a password while logging in, and when the batch file is done, restore settings. To do that hit Win+R and and type netplwiz (In the user's computer) and then uncheck "Users must enter an username and password to use this computer" and when you click OK a dialog box will come to ask the user's current password. Ask the user to enter it and confirm. Now run the batch file and after when it is done, open netplwiz again and check the option. Now users will require password.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .