@ECHO OFF
:: Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
:: If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
Echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
Echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
Echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
Exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( Del "%temp%\getadmin.vbs" )
Pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET ERRORLEVEL=
SET ERRNUM=
FOR /R "%~dp0" %%A IN (*-KB*.MSU) DO (
CALL :SUB %%~nA
ECHO= Installing KB!KB_NUM!
>NUL TIMEOUT /t 3
SET ERRORLEVEL=
WUSA "%%A" /quiet /norestart
SET ERRNUM=!ERRORLEVEL!
IF !ERRNUM! EQU 3010 (
ECHO= == SUCCESS ^(RebootRequired^) ^[!ERRNUM!^] ==
) ELSE (
IF ERRORLEVEL 1 (
ECHO= == FAILED ^[!ERRNUM!^] ==
) ELSE (
ECHO= == SUCCESS ^[!ERRNUM!^] ==
)
)
)
CHOICE /C YN /M "Close all programs and press Y to restart or N to continue."
IF ERRORLEVEL 2 GOTO continue
IF ERRORLEVEL 1 GOTO restart
GOTO end
:continue
GOTO end
:restart
ECHO= == Press any key to restart ==
>NUL PAUSE
SHUTDOWN.EXE /r /t 0
:end
GOTO :EOF
:SUB
SET "KB_NUM=%*"
FOR /F "DELIMS=-" %%B IN ("%KB_NUM:*-KB=%") DO SET "KB_NUM=%%B"
REM http://www.msfn.org/board/topic/152020-batch-script-for-windows-software-updates-installation/page__view__findpost__p__964942
REM http://www.windows-commandline.com/2011/10/choice-command.html
REM http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx
REM http://ss64.com/nt/delayedexpansion.html
REM http://ss64.com/nt/runas.html
+++++++
+ VBScript Alternative
+++++++
'http://ccmexec.com/2012/02/installing-multiple-windows-7-hotfixes-msu-with-sccm/
'http://www.insidethe.com/blog/2009/12/how-to-launch-a-wsh-vbscript-as-administrator-in-windows-7-and-vista/
mytitle="MSU Installer"
mybuttons=65 ' vbOKCancel + vbInformation
If WScript.Arguments.Named.Exists("elevated") = False Then
'Launch the script again as administrator
CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
WScript.Quit
Else
'Change the working directory from the system32 folder back to the script's folder.
Set oShell = CreateObject("WScript.Shell")
oShell.CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
MsgBox "Now running with elevated permissions",0,mytitle
End If
Dim objfso, objShell
Dim folder, files, sFolder, folderidx, Iretval, return
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")
sFolder = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
Set folder = objfso.GetFolder(sFolder)
Set files = folder.Files
something=0 ' there is nothing to do
For each folderIdx In files
If Ucase(Right(folderIdx.name,3)) = "MSU" then
something=1 ' there is something to do.
wscript.echo "wusa.exe " & sfolder & folderidx.name & " /quiet /norestart"
'wscript.echo "wusa.exe " & sfolder & folderidx.name
iretval=objShell.Run ("wusa.exe " & sfolder & folderidx.name & " /quiet /norestart", 1, True)
'iretval=objShell.Run ("wusa.exe " & sfolder & folderidx.name, 1, True)
If (iRetVal = 0) or (iRetVal = 3010) then
'wscript.echo folderidx.name & " Success"
myprompt=folderidx.name & " Success " & "[" & iRetVal & "]"
Else
'wscript.echo folderidx.name & " Failed"
myprompt=folderidx.name & " Failed " & "[" & iRetVal & "]"
End If
devam=MsgBox(myprompt,mybuttons,mytitle)
If (devam = 2) or (devam = 3) or (devam = 7) Then
'Cancel/Abort/No was clicked
wscript.quit(1)
End If
End If
Next
If (something = 0) Then
MsgBox "Nothing to do.",64,mytitle
Else
MsgBox "Done.",64,mytitle
restartla=MsgBox("Close all programs and press Yes to restart or No to continue.",36,mytitle)
If (restartla = 6) Then
Set WSHShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "%SYSTEMROOT%\system32\shutdown.exe /r /t 0"
End If
End If