MSFN Forum: Batch script for windows & software updates installation - MSFN Forum

Jump to content


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Batch script for windows & software updates installation Installation of windows Updates incl software Updates via Batch script

#21 User is offline   myselfidem 

  • Member
  • PipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 2,388
  • Joined: 06-January 10
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 08 May 2011 - 04:43 PM

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!

This post has been edited by myselfidem: 08 May 2011 - 04:53 PM



#22 User is offline   centi50 

  • Newbie
  • Group: Members
  • Posts: 37
  • Joined: 25-January 10

Posted 08 May 2011 - 05:27 PM

What if am to install Win Xp Updates, is there something to edit in the Batch code Being Provided here

#23 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,363
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 09 May 2011 - 12:12 AM

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"


#24 User is offline   centi50 

  • Newbie
  • Group: Members
  • Posts: 37
  • Joined: 25-January 10

Posted 09 May 2011 - 07:00 AM

Thanks Yzöwl

Thanking Everyone for helping me out

PEACE AND LOVE TO ALL :thumbup

#25 User is offline   centi50 

  • Newbie
  • Group: Members
  • Posts: 37
  • Joined: 25-January 10

Posted 22 June 2011 - 04:47 PM

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

This post has been edited by Yzöwl: 23 June 2011 - 12:11 PM
Reason for edit: Merged New Topic with its directly related thread


#26 User is offline   xranger 

  • Group: Members
  • Posts: 2
  • Joined: 16-January 12
  • OS:none specified
  • Country: Country Flag

Posted 16 January 2012 - 07:50 AM

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.

View PostYzöwl, on 08 May 2011 - 04:27 PM, said:

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"



#27 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,363
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 16 January 2012 - 04:48 PM

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"


#28 User is offline   xranger 

  • Group: Members
  • Posts: 2
  • Joined: 16-January 12
  • OS:none specified
  • Country: Country Flag

Posted 17 January 2012 - 01:48 PM

Thank you,Yzowl..
You 're right..I retested the batch file, it works well now..that's odd,maybe I forgot restarting my system after patching Sp1 :P
Anyway.thank again :)

#29 User is offline   MrAnderson 

  • Group: Members
  • Posts: 1
  • Joined: 04-March 12
  • OS:Vista Home Premium x86
  • Country: Country Flag

Posted 04 March 2012 - 11:09 AM

@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

Attached File(s)



#30 User is offline   dsrtFx 

  • Group: Members
  • Posts: 1
  • Joined: 12-July 12
  • OS:none specified
  • Country: Country Flag

Posted 12 July 2012 - 03:02 PM

Yzowl,

What to do to run that batch file for a multiple computers? What commands need to be added?

#31 User is offline   submix8c 

  • Inconceivable!
  • Group: Patrons
  • Posts: 3,244
  • Joined: 14-September 05
  • OS:none specified
  • Country: Country Flag

Posted 16 March 2013 - 11:46 AM

HOLEY KROWS!!!

Sorry to bump this but SILLY ME went nutz trying to figure out why files/paths weren't being found!

HERE is the reason - I had a "!" (exclamation point) as part of a Folder Name! DO NOT DO THIS since
SET ENABLEDELAYEDEXPANSION
specifically EXPECTS to use that as a Variable Delimiter, same case as CMD.EXE "/V:ON".

Do NOT use that anywhere in any Pathname/Filename. (wasted time via experimentation...)

HTH some poor idee-yut that does the same thing. :(

This post has been edited by submix8c: 16 March 2013 - 11:47 AM


#32 User is offline   gohnick 

  • Group: Members
  • Posts: 2
  • Joined: 09-April 13
  • OS:none specified
  • Country: Country Flag

Posted 09 April 2013 - 08:59 PM

View PostYzöwl, on 09 May 2011 - 12:12 AM, said:

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"



Dear sir,

Is it possible to include a string to output the installed KB numbers into a text file within the directory? Pardon my newbie question as I am a newbie in scripting :(

This post has been edited by gohnick: 09 April 2013 - 09:03 PM


#33 User is offline   submix8c 

  • Inconceivable!
  • Group: Patrons
  • Posts: 3,244
  • Joined: 14-September 05
  • OS:none specified
  • Country: Country Flag

Posted 10 April 2013 - 12:33 PM

I won't swear to this, but putting
ECHO= == List of KB's installed == >KBLIST.TXT
right after the "SETLOCAL" and then placing
        ECHO= Installing KB!KB_NUM! >>KBLIST.TXT 
after the other one should do it. :unsure:

#34 User is offline   gohnick 

  • Group: Members
  • Posts: 2
  • Joined: 09-April 13
  • OS:none specified
  • Country: Country Flag

Posted 11 April 2013 - 02:55 AM

View Postsubmix8c, on 10 April 2013 - 12:33 PM, said:

I won't swear to this, but putting
ECHO= == List of KB's installed == >KBLIST.TXT
right after the "SETLOCAL" and then placing
        ECHO= Installing KB!KB_NUM! >>KBLIST.TXT 
after the other one should do it. :unsure:


Hi submix8c,

Thanks for the tip! it works! :)

#35 User is offline   agnimitra1980 

  • Group: Members
  • Posts: 1
  • Joined: Yesterday, 04:13 AM
  • OS:Server 2008R2
  • Country: Country Flag

Posted Yesterday, 04:26 AM

Hi using this script is it possible to capture status of each installation? like if there is any update which fails or does not apply, is there any way we can capture that?

#36 User is offline   centi50 

  • Newbie
  • Group: Members
  • Posts: 37
  • Joined: 25-January 10

Posted Yesterday, 07:47 AM

Hello to All

I use the following code Batch script to Install the updates in a folder in my Win 7 x64 laptop.

@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"


The above code will Install all updates of .MSU located in a folder same as the batch script one after the other. What it won't do is, it won't install updates of .EXE e.g .NetFramework 4 updates and also it won't install Silverlight Updates.

Please can some one Kindly re edit the above script for me to include the installation of .EXE files and Silverlight updates.

Also note am totally zero in scripting, so kindly if changes are made please reproduce the above script with changes so i can copy and paste it in my .BAT file.

Also note the code is not mine. i just got it in this thread, as i am the thread starter.


Thanking all in advance for their time and efforts to Help out people like me.

Share this topic:


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy