scripting patch installation

S

surferboy

hello,
i am trying to create a script so that i might install a
patch with out answering all the questions that it asks.
i am going to be doing this for about 30 of the ms updates
all at once and i need a script to install the patches.
this is what i currently have but it is not functioning
write.


Set objService = GetObject("winmgmts:")
Set objSoftware = objService.Get("Win32_Product")
errReturn = objSoftware.Install("q824145.exe", "ALLUSERS=2
REBOOT=ReallySuppress" , True)

If errReturn = 0 Then
WScript.Echo "Installation success!"
Else
WScript.Echo "Installation failure!"
End If


thanks in advance.
 
T

Torgeir Bakken (MVP)

surferboy said:
i am trying to create a script so that i might install a
patch with out answering all the questions that it asks.
i am going to be doing this for about 30 of the ms updates
all at once and i need a script to install the patches.
this is what i currently have but it is not functioning
write.

Set objService = GetObject("winmgmts:")
Set objSoftware = objService.Get("Win32_Product")
errReturn = objSoftware.Install("q824145.exe", "ALLUSERS=2
REBOOT=ReallySuppress" , True)

Hi

First, some more general links that might be of interest for you
(copy and paste the URL address lines into your IE address bar):

http://unattended.sourceforge.net/

http://groups.google.com/groups?selm=s5gk74srdje.fsf@patl=users.sf.net

http://groups.google.com/[email protected]

Command-Line Switches For Windows Software Update Packages (1)
http://support.microsoft.com/default.aspx?kbid=262841

Command-Line Switches for Microsoft Software Update Packages (2)
http://support.microsoft.com/default.aspx?kbid=824687

New File Naming Schema for Microsoft Windows Software Update Packages
http://support.microsoft.com/default.aspx?kbid=816915



Back to your original problem:

The Win32_Product class is for Windows Installer packages only (.msi files).

I suggest you use WSH's Run method instead (VBScript)

Download WSH 5.6 documentation from here:
http://msdn.microsoft.com/downloads/list/webdev.asp

An example:

' --------- script start ---------
Set objShell = CreateObject("WScript.Shell")

' with no spaces in the path
strCmd = "L:\Patches\Q824145.exe /q:a /r:n"
' with no spaces in the path
'strCmd = """L:\Some Patches\Q824145.exe"" /q:a /r:n"

' Setting the 3rd parameter to True to make
' the script wait for the installation to finish
intReturnCode = objShell.strCmd(sCmd, 1, True)

' When reboot is needed, some patches returns 3010
If intReturnCode = 0 Or intReturnCode = 3010 Then
WScript.Echo "Installation success!"
Else
WScript.Echo "Installation failure!"
End If
' --------- script end ---------


If you want to use WMI, you need to use the Win32_Process class and it's Create
method:

Create Method of the Win32_Process Class
http://msdn.microsoft.com/library/en-us/wmisdk/wmi/create_method_in_class_win32_process.asp

But it is more complicated to make the script wait for the installation to
finish, and you will not get the return code from the application:

' --------- script start ---------
Set objService = GetObject("winmgmts:")
Set objSoftware = objService.Get("Win32_Process")
errReturn = objSoftware.Create _
("L:\Patches\Q824145.exe /q:a /r:n", Null, Null, intProcessID)

' errReturn will not give any install status, it only indicates
' if the app was successfully launched or not
If errReturn = 0 Then
' wait for the process to finish
On Error Resume Next
Do
WScript.Sleep 1000
Set objMyProcess = objService.Get("Win32_Process='" & intProcessID & "'")
If Err.Number <> 0 Then
' Process has finished or terminated
bolDone = True
Else
bolDone = False
End If
Loop Until bolDone
On Error Goto 0
End If

' --------- script end ---------
 
S

surferboy

thanks again for the help could i perhaps check first to
see if the patch is installed then run througth the code
that installes the patch.
Thanks
 
T

Torgeir Bakken (MVP)

surferboy said:
thanks again for the help could i perhaps check first to
see if the patch is installed then run througth the code
that installes the patch.

Hi

The safest is of course to check file versions, but it is a bit complicated
because each OS version and even each OS Service Pack level usually have
different file versions for the same hotfix.

For an example on this, take a look here:
http://groups.google.com/[email protected]

At least for all NT based OSes, usually a hotfix install is creating a
registry key under
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\HotFix\
with a value "installed" that is set to 1 when it is installed (for IE updates
it is under HKLM\Software\Microsoft\Active Setup\Installed Components\)

Read the Knowledge base article or security bulletin for each update to see
what to check for.

VBScript RegRead example:
http://groups.google.com/[email protected]

VBScript/WMI example:
http://groups.google.com/[email protected]
 
G

Guest

ive been using regread to read the registry value and
compare it to the value that would exist after
installation. my problem is that it gives an error when
the key doesn't exist at all but compare fine when it is
installed.
thanks in advance
 
T

Torgeir Bakken (MVP)

ive been using regread to read the registry value and
compare it to the value that would exist after
installation. my problem is that it gives an error when
the key doesn't exist at all but compare fine when it is
installed.

Hi

Please post your code (at least the relevant part of it)...
 
S

surferboy

just a quick note. the installation works fine. but on the
line where it reads the registry it errors if that reg
doesn't exist. what i want is for it to just install the
patch if that registry value is non existant.
thanks again


Dim location
Dim WshShell
Dim Version
Set WshShell = WScript.CreateObject("WScript.Shell")




' Blaster Patch
' install script for patch q824146

location = WshShell.CurrentDirectory +"\blasterfix.exe -u -
z"
version = WshShell.RegRead
("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Updates\Windows
XP\SP2\KB824146\Type")

If WshShell.RegRead
("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Updates\Windows
XP\SP2\KB824146\Type") <> "Update" Then

Set objService = GetObject("winmgmts:")
Set objSoftware = objService.Get("Win32_Process")
errReturn = objSoftware.Create _
(location, Null, Null, intProcessID)

' errReturn will not give any install status, it only
indicates
' if the app was successfully launched or not
If errReturn = 0 Then
' wait for the process to finish
On Error Resume Next
Do
WScript.Sleep 1000
Set objMyProcess = objService.Get("Win32_Process='" &
intProcessID & "'")
If Err.Number <> 0 Then
' Process has finished or terminated
bolDone = True
Else
bolDone = False
End If
Loop Until bolDone
On Error Goto 0
End If
End If
 
T

Torgeir Bakken (MVP)

surferboy said:
just a quick note. the installation works fine. but on the
line where it reads the registry it errors if that reg
doesn't exist. what i want is for it to just install the
patch if that registry value is non existant.
thanks again

Some general comments first. I would suggest you use the WScript.Shell's Run
method instead of WMIs' Win32_Process Create method to launch the exe with.
The example I showed you in a previous post had an error (a hasty copy/paste
one), instead of

intReturnCode = objShell.strCmd(sCmd, 1, True)

it should have been

intReturnCode = objShell.Run(strCmd, 1, True)


As long as you are not operating on remote computers, WMI is overkill to use
for this. WScript.Shell's Run method is more lightweight and better to use in
this case.

If you want a script that also can handle different OS version and Service pack
levels when installing the KB824146 (MS03-039) on a local computer, take a look
here:

http://groups.google.com/[email protected]

' Blaster Patch
' install script for patch q824146

location = WshShell.CurrentDirectory +"\blasterfix.exe -u -
z"
version = WshShell.RegRead
("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Updates\Windows
XP\SP2\KB824146\Type")

Ok, back to your issue. WshShell.RegRead will err if the value you try to read
does not exist, so you need to error handle it with "On Error Resume Next", and
then turn on the internal error handling again with "On Error Goto 0". Instead
of doing this error handling every time you need to do a registry read, it is
easier to make a function that handles this internally (se Function RegRead
below)

Also, your script line above (version = ...) isn't used for anything, so you
can remove it.

Below is an updated version of your script that uses the function RegRead to
read from registry. I also have changed it to test on another value in
registry, the one Microsoft recommends you should use to see if the KB824146 is
installed or not. I also have changed it to use the Run method and allow spaces
in the path to the exe file.


'--------------------8<----------------------
' Blaster Patch
' install script for patch q824146

Set WshShell = CreateObject("WScript.Shell")

location = WshShell.CurrentDirectory & "\blasterfix.exe"
params = " -u -z"

sRegValue = "HKLM\SOFTWARE\Microsoft\Windows NT\" _
& "CurrentVersion\HotFix\KB824146\Installe"

If RegRead(sRegValue) <> "1" Then
' Setting the 3rd parameter to True to make
' the script wait for the installation to finish
WshShell.Run """" & location & """" & params, 1, True
End If


Function RegRead(sRegValue)
Set oShell = CreateObject("WScript.Shell")
On Error Resume Next
RegRead = oShell.RegRead(sRegValue)
' If the value does not exist, error is raised
If Err Then
RegRead = ""
Err.clear
End If
' If a value is present but uninitialized the RegRead method
' returns the input value in Win2k.
If VarType(RegRead) < vbArray Then
If RegRead = sRegValue Then
RegRead = ""
End If
End If
On Error Goto 0
End Function
'--------------------8<----------------------
 
T

Torgeir Bakken (MVP)

Torgeir Bakken (MVP) said:
(snip)
' Blaster Patch
' install script for patch q824146

Set WshShell = CreateObject("WScript.Shell")

location = WshShell.CurrentDirectory & "\blasterfix.exe"
params = " -u -z"

sRegValue = "HKLM\SOFTWARE\Microsoft\Windows NT\" _
& "CurrentVersion\HotFix\KB824146\Installe"

and that needs to be:
& "CurrentVersion\HotFix\KB824146\Installed"

(a missing d at the end)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top