Jump to content

Batch script for windows & software updates installation


Recommended Posts

A better source is actual dedicated sites:

http://ss64.com/nt/

http://www.robvanderwoude.com/batchfiles.php

Yzöwl, which is our resident "batch master" :thumbup has a "quirk" to writing very good snippets/scripts in the most simplified (which sometimes means complex ) way :ph34r: ).

This:

@ECHO OFF&SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION 
FOR /R %%# IN (*-KB*.MSU) DO (CALL :SUB %%~n#
ECHO= INSTALLING KB!_!&ECHO=WUSA "%%#" /QUIET /NORESTART)
PAUSE&GOTO :EOF
:SUB
SET "_=%*"
FOR /F "DELIMS=-" %%$ IN ("%_:*-KB=%") DO SET "_=%%$"

Can be re-written as:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R %%A IN (*-KB*.MSU) DO (
CALL :SUB %%~nA
ECHO INSTALLING KB!KB_number!
ECHO=WUSA "%%A" /QUIET /NORESTART
)
PAUSE

GOTO :EOF

:SUB
SET "KB_number=%*"
FOR /F "DELIMS=-" %%B IN ("%KB_number:*-KB=%") DO SET "KB_number=%%B"

Which may be more readable. :unsure:

jaclaz

Edited by jaclaz
Link to comment
Share on other sites


Thanks Yzöwl and jaclaz! :thumbup

I've made some changes and the batch file works fine for me like below!

After testing I see this batch file works fine, if I set the batch file inside a folder (example: C:\Post_SP1\) and the batch searching inside a subfolder (example: C:\Post_SP1\Updates);

or if it is inside the same folder where the updates are located (example: C:\Post_SP1\Updates\).

The batch file is inside C:\Post_SP1\ or inside C:\Post_SP1\Updates and is working fine (outside or inside the folder).

Works also fine if I launch the batch file on C:\ partition!


@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R %%A IN (*-KB*.MSU) DO (
CALL :SUB %%~nA
ECHO INSTALLING KB!KB_number!
TIMEOUT /t 3 >NUL
C:\Windows\System32\wusa.exe %%A /quiet /norestart
)
ECHO == Press any key to restart ==&PAUSE>NUL
shutdown.exe /r /t 0
GOTO :EOF

:SUB
SET "KB_number=%*"
FOR /F "DELIMS=-" %%B IN ("%KB_number:*-KB=%") DO SET "KB_number=%%B"

Output command window

------------------------------------------------------------

INSTALLING KB2425227

INSTALLING KB2479943

INSTALLING KB2491683

INSTALLING KB2503658

INSTALLING KB2506212

INSTALLING KB2506223

INSTALLING KB2507618

INSTALLING KB2508272

INSTALLING KB2508429

INSTALLING KB2509553

INSTALLING KB2511455

== Press any key to restart ==

------------------------------------------------------------

Edited by myselfidem
Link to comment
Share on other sites

Just thought I'd add a note about the use of the /R switch with FOR in this case.

You are recursing the directory rooted at the current directory, not necessarily that of the batch files location. If you wanted to make sure of that I'd suggest that you think about using either:

FOR /R C:\Post_SP1 %%A IN (

or

FOR /R %~dp0 %%A IN (

You may of course need to ensure that paths with spaces are catered for too!

Link to comment
Share on other sites

Thanks Yzöwl to point me about this question!

Could you tell me if the batch file is correct like this.

Works for me, even if the folder has a space in its name.


@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R "%~dp0" %%A IN (*-KB*.MSU) DO (CALL :SUB %%~nA
ECHO INSTALLING KB!KB_number!
TIMEOUT /t 3 >NUL
C:\Windows\System32\wusa "%%A" /quiet /norestart)
ECHO == Press any key to restart ==&PAUSE>NUL
shutdown.exe /r /t 0
GOTO :EOF

:SUB
SET "KB_number=%*"
FOR /F "DELIMS=-" %%B IN ("%KB_number:*-KB=%") DO SET "KB_number=%%B"

Edited by myselfidem
Link to comment
Share on other sites

That is correct.

Do bear in mind that WUSA.EXE is located within %PATH% as is SHUTDOWN.EXE and that executables located within the path do not require their path prepending to the command. You have done so with one and not the other, whilst both are correct it seems pointless to include that which isn't required.

You can also simplify lines 2 & 3 by merging them into one thus

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
FOR /R "%~dp0" %%A IN (*-KB*.MSU) DO (
CALL :SUB %%~nA
ECHO= Installing KB!KB_NUM!
>NUL TIMEOUT /t 3
WUSA "%%A" /quiet /norestart)
ECHO= == Press any key to restart ==
>NUL PAUSE
SHUTDOWN.EXE /r /t 0
GOTO :EOF

:SUB
SET "KB_NUM=%*"
FOR /F "DELIMS=-" %%B IN ("%KB_NUM:*-KB=%") DO SET "KB_NUM=%%B"

Link to comment
Share on other sites

Many thanks Yzöwl

Really nice and awesome! :thumbup

But, I must admit it is hard for me, because I'm a newbe with batch files.

However It's very interresting to learn how that works.

Thanks again for your time and the very useful informations!

Edited by myselfidem
Link to comment
Share on other sites

For windows xp updates, you can probably get away with simply changing MSU to EXE and removing WUSA.

@ECHO OFF 
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
FOR /R "%~dp0" %%A IN (*-KB*.EXE) DO (
CALL :SUB %%~nA
ECHO= Installing KB!KB_NUM!
>NUL PING -n 4 127.0.0.1
"%%A" /quiet /norestart)
ECHO= == Press any key to restart ==
>NUL PAUSE
SHUTDOWN.EXE /r /t 0
GOTO :EOF

:SUB
SET "KB_NUM=%*"
FOR /F "DELIMS=-" %%B IN ("%KB_NUM:*-KB=%") DO SET "KB_NUM=%%B"

Link to comment
Share on other sites

  • 1 month later...

Please can anyone Modify this batch Script to include the Installation of software updates of the type SFXCAB.EXE as in Silverlight and type WEXTRACT.EXE as in Microsoft Visual C++ 2005 Redisributable package.

The above script will Search for the updates in The form of KB in the folder and subfolders is Placed in.

The scenario that i was faced with was after Manual downloading the windows updates which obviously were like

windowsxp-kb2476490-x86-enu_1545ba40647300fc0cde0c3e4c8e307e7d77d45d

ndp40-kb2518870-x86_42ed6547df9927704552b65505ed7dd76b363be6

and many others like the above two, i also included the software optional updates like type SFXCAB.EXE as in Silverlight and type WEXTRACT.EXE as in Microsoft Visual C++ 2005 Redisributable package. the above script did not install the software optional updates.

Therefore Please can Anyone modify the script to make it possible also to install software optional updates. That is if it is Possible.

your Help will be highly appreciated

Thanking All In Advance

Edited by Yzöwl
Merged New Topic with its directly related thread
Link to comment
Share on other sites

  • 6 months later...

hi,Yzowl. I am a newbe here..your code is awesome :thumbup thanks a lot.

But I just got a little problem,when I tested this batch by choosing "run as administrator"(windows7 x64) in a folder with all MSU and EXE update files, .bat processed and listed them,

but in fact none of them was actuelly installed. Then I tried to add "cd /d %~dp0" to change the path at the beginning before your code, and it worked finally.

But in your code these is already "FOR /R "%~dp0" %%A IN (*-KB*.MSU) DO (" , looks like this parte doesn't work for me, I dont understand why, could you please explain?

PS: your second code for .exe file updating woks well.

That is correct.

Do bear in mind that WUSA.EXE is located within %PATH% as is SHUTDOWN.EXE and that executables located within the path do not require their path prepending to the command. You have done so with one and not the other, whilst both are correct it seems pointless to include that which isn't required.

You can also simplify lines 2 & 3 by merging them into one thus

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
FOR /R "%~dp0" %%A IN (*-KB*.MSU) DO (
CALL :SUB %%~nA
ECHO= Installing KB!KB_NUM!
>NUL TIMEOUT /t 3
WUSA "%%A" /quiet /norestart)
ECHO= == Press any key to restart ==
>NUL PAUSE
SHUTDOWN.EXE /r /t 0
GOTO :EOF

:SUB
SET "KB_NUM=%*"
FOR /F "DELIMS=-" %%B IN ("%KB_NUM:*-KB=%") DO SET "KB_NUM=%%B"

Link to comment
Share on other sites

If all of the updates are located within the same directory as the batch file then you don't need the recursion and can indeed navigate to the source directory.

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
PUSHD %~dp0
FOR %%A IN (*-KB*.MSU) DO (
CALL :SUB %%~nA
ECHO= Installing KB!KB_NUM!
>NUL TIMEOUT /t 3
WUSA "%%~fA" /quiet /norestart)
ECHO= == Press any key to restart ==
>NUL PAUSE
SHUTDOWN.EXE /r /t 0
GOTO :EOF

:SUB
SET "KB_NUM=%*"
FOR /F "DELIMS=-" %%B IN ("%KB_NUM:*-KB=%") DO SET "KB_NUM=%%B"

Link to comment
Share on other sites

  • 1 month later...

@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

InstallMSU.zip

Link to comment
Share on other sites

  • 4 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...