Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 847
  • Last Modified:

need vbscript to include logfile with results

Hi,

I currently have a .vbs that perfroms various uninstall/installs and I would like to somehow create a logfile file that will output the results of everything.  See the basic code I have below.  I would like the code to including a check to see if the program is installed, if not installed, write to logfile "the program was not found".  If it is installed, write to logfile "the program was found performing uninstall...". After the uninstall command is executed, I would like to output the results of the uninstall whether it was successful or not.  If the uninstall was not successful I would like to output the error code associated with it.  The error code directly from the system log would be ideal.  Thanks in advance for your help.
Set oWShell = CreateObject("Wscript.Shell")
Set WSHShell = CreateObject("WScript.Shell")

	'Uninstall "Program1" and wait for it to finish
	oWShell.Run "msiexec /x ""{7005540C-A8C7-41C5-8717-6C683661BE31}"" /passive /norestart", 8, True

	'Uninstall "Program2" and wait for it to finish
	oWShell.Run "msiexec /x ""{808E24FF-1988-43EF-9DD9-42B224391E19}"" /passive /norestart", 8, True
	
	'Uninstall "Program3" and wait for it to finish
	oWShell.Run "msiexec /x ""{36BA2DD9-F51E-444C-8741-76022DF1B9C6}"" /passive /norestart", 8, True

	'Uninstall "Program4" and wait for it to finish
	oWShell.Run "msiexec /x ""{6F3124C8-FE22-48B3-86BF-B71BAD533444}"" /passive /norestart", 8, True

	'shutdown -t 0 -r -f call shutdown script
	oWShell.Run "InstallationFiles\Scripts\shutdown.vbs", 8, True

Open in new window

0
PKI_Program_Office
Asked:
PKI_Program_Office
  • 7
  • 5
1 Solution
 
rlandquistCommented:
Ok, here is a script that should work for you.

It is not the prettiest thing I have ever done, but I am short on time.

Let me know how it goes.
Set objShell = CreateObject("Wscript.Shell")
strComputer = "."

blnProgram1 = False
blnProgram2 = False
blnProgram3 = False
blnProgram4 = False
blnProgram5 = False

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile("c:\scripts\softwarelog.txt", True)

'Create connection via WMI to computer.
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

Set colSoftware = objWMIService.ExecQuery("Select * From Win32_Product")
For Each objSoftware In colSoftware
    strID = objSoftware.IdentifyingNumber
    Select Case strID
        Case "{7005540C-A8C7-41C5-8717-6C683661BE31}"
            blnProgram1 = True
            objTextFile.WriteLine objSoftware.Name & " was found. Performing uninstall ..."
            retCode = objSoftware.Uninstall
            If retCode = 0 Or retCode = 3010 Then
                objTextFile.WriteLine objSoftware.Name & " Uninstalled Successfully"
            Else
                objTextFile.WriteLine objSoftware.Name & " Uninstall Failed. Exit Code: " & retCode
            End If        
        Case "{808E24FF-1988-43EF-9DD9-42B224391E19}"
            blnProgram2 = True
            objTextFile.WriteLine objSoftware.Name & " was found. Performing uninstall ..."
            retCode = objSoftware.Uninstall
            If retCode = 0 Or retCode = 3010 Then
                objTextFile.WriteLine objSoftware.Name & " Uninstalled Successfully"
            Else
                objTextFile.WriteLine objSoftware.Name & " Uninstall Failed. Exit Code: " & retCode
            End If        
        Case "{36BA2DD9-F51E-444C-8741-76022DF1B9C6}"
            blnProgram3 = True
            objTextFile.WriteLine objSoftware.Name & " was found. Performing uninstall ..."
            retCode = objSoftware.Uninstall
            If retCode = 0 Or retCode = 3010 Then
                objTextFile.WriteLine objSoftware.Name & " Uninstalled Successfully"
            Else
                objTextFile.WriteLine objSoftware.Name & " Uninstall Failed. Exit Code: " & retCode
            End If        
        Case "{6F3124C8-FE22-48B3-86BF-B71BAD533444}"
            blnProgram4 = True
            objTextFile.WriteLine objSoftware.Name & " was found. Performing uninstall ..."
            retCode = objSoftware.Uninstall
            If retCode = 0 Or retCode = 3010 Then
                objTextFile.WriteLine objSoftware.Name & " Uninstalled Successfully"
            Else
                objTextFile.WriteLine objSoftware.Name & " Uninstall Failed. Exit Code: " & retCode
            End If
    End Select
Next

If blnProgram1 = False Then
    objTextFile.WriteLine "Program 1 not found."
End If
If blnProgram2 = False Then
    objTextFile.WriteLine "Program 2 not found."
End If
If blnProgram3 = False Then
    objTextFile.WriteLine "Program 3 not found."
End If
If blnProgram4 = False Then
    objTextFile.WriteLine "Program 4 not found."
End If

objTexFile.Close

'shutdown -t 0 -r -f call shutdown script
objShell.Run "InstallationFiles\Scripts\shutdown.vbs", 8, True

Open in new window

0
 
Daz_1234Commented:
Hi,

Here's my version.  I do not see the need to check first if it already installed, since if it is not msiexec will just return 1605.  For all return codes see here: http://support.microsoft.com/kb/229683

Hope this helps,
Daz.

Option Explicit

Dim arrGUIDs(3), strGUID, ret, wshShell, fso, strLogFile

Set wshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

strLogFile = "C:\Uninstall.log"

arrGUIDs(0) = "{7005540C-A8C7-41C5-8717-6C683661BE31}"
arrGUIDs(1) = "{808E24FF-1988-43EF-9DD9-42B224391E19}"
arrGUIDs(2) = "{36BA2DD9-F51E-444C-8741-76022DF1B9C6}"
arrGUIDs(3) = "{6F3124C8-FE22-48B3-86BF-B71BAD533444}"


For Each strGUID In arrGuids
    WLog "Attempting uninstall of GUID " & strGUID
    ret = fUninstall(strGUID)
    WLog "Uninstall returned: " & ret
Next

wshShell.Run "shutdown -t 300 -r -f", 0, False



Function fUninstall(sGUID)
    Dim ret
    ret = wshShell.Run ("msiexec /x " & sGUID & " /passive /norestart", 8, True)
    fUninstall = ret
End Function

Sub WLog(sTxt)
    Dim ts
    Set ts = fso.OpenTextFile(strLogFile, 8, True)
    ts.WriteLine Now() & " - " & sTxt
    ts.Close
End Sub

Open in new window

0
 
PKI_Program_OfficeAuthor Commented:
Daz_1234,

Your script works perfect.  Thanks.  One more question.  What if I wanted to install 4 new programs as well.  Can I just put the path of the .msi in place of the GUIDs for example:

installmsi(0) = "c:/pathtomsi/file1.msi"
installmsi(1) = "c:/pathtomsi/file2.msi"
'and so on

and create a new function for install.  I would also need to output results in the same way.

thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Daz_1234Commented:
Yes you are on the right track:

1. Dim the array, e.g. Dim arrInstallMSI(4) before using, e.g. arrInstallMSI(0) = "c:/pathtomsi/file1.msi".  The number after the variable name in the Dim is the highest element number.  Elements start at zero.

2. Copy and adapt the For Each loop and the fUninstall functions from the ones above.

3. You can just keep using the WLog sub as much as you like to log useful info.

4. Common return codes:
   0 = Success
   3010 = Success, reboot required
   1618 = Another installation is already in progress (Msiexec is busy)

Post your code when done if you want me to give it the once-over.

Good luck!
Daz.
0
 
PKI_Program_OfficeAuthor Commented:
Ok thanks.  Just thought of adding a bit more logic to check if programs are installed before executing the install.  I have implemented a function to check for registry keys and if they dont exist, I would like the install to execute.  If they do exist, the install should be bypassed but only the patch should be installed.

Hopefully I havent made things too complicated now where the script needs to be reorganized.  Again thanks for the help.
If Not RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\40A9A3A1D391DF54D85B3D6C4BADA707\InstallProperties\") Then
	
		'Code to perform install of arrinstallMSI(0)
		'Code to perform install of arrinstallMSI(1)

		Else

		'Code to perform install of arrinstallMSI(1)
End If
If Not RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\EF96139C65413AD4D9522FFE43A039A8\InstallProperties\") Then
	
		'Code to perform install of arrinstallMSI(2)
		'Code to perform install of arrinstallMSI(3)
	
		Else

		'Code to perform install of arrinstallMSI(3)
End If


'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function

Open in new window

0
 
Daz_1234Commented:
Something like the below should sort you out this time:
Option Explicit

Dim arrInstallMSI(3), ret, wshShell, fso, strLogFile

Set wshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

strLogFile = "C:\Install_MSI.log"

arrInstallMSI(0) = "c:/pathtomsi/file1.msi"
arrInstallMSI(1) = "c:/pathtomsi/file2.msi"
arrInstallMSI(2) = "c:/pathtomsi/file3.msi"
arrInstallMSI(3) = "c:/pathtomsi/file4.msi"


If Not RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\40A9A3A1D391DF54D85B3D6C4BADA707\InstallProperties\") Then
    WLog "About to Install: " & arrInstallMSI(0)
    ret = fInstall(arrInstallMSI(0))
    WLog "Return: " & ret
    WLog "About to Install: " & arrInstallMSI(1)
    ret = fInstall(arrInstallMSI(1))
    WLog "Return: " & ret
Else
    WLog "About to Install: " & arrInstallMSI(1)
    ret = fInstall(arrInstallMSI(1))
    WLog "Return: " & ret
End If

If Not RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\EF96139C65413AD4D9522FFE43A039A8\InstallProperties\") Then
    WLog "About to Install: " & arrInstallMSI(2)
    ret = fInstall(arrInstallMSI(2))
    WLog "Return: " & ret
    WLog "About to Install: " & arrInstallMSI(3)
    ret = fInstall(arrInstallMSI(3))
    WLog "Return: " & ret
Else
    WLog "About to Install: " & arrInstallMSI(3)
    ret = fInstall(arrInstallMSI(3))
    WLog "Return: " & ret
End If


'************************************************************************************
' Checks if the passed key exists
'************************************************************************************
Function RegKeyExists(sKey)
    On Error Resume Next

    If Not IsObject(oWShell) Then _
        Set oWShell = CreateObject("Wscript.Shell")

    Err.Clear
    oWShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    Err.Clear
End Function

Function fInstall(sPath)
    Dim fso, ret
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.FileExists(sPath) Then
        WLog "ERROR: installation file does not exist: " & sPath
        WScript.Quit(1)
    End If
    ret = wshShell.Run ("msiexec /i """ & sPath & """ ALLUSERS=1 /norestart /qb", 8, True)
    fUninstall = ret
End Function

Sub WLog(sTxt)
    Dim ts
    Set ts = fso.OpenTextFile(strLogFile, 8, True)
    ts.WriteLine Now() & " - " & sTxt
    ts.Close
End Sub

Open in new window

Please note that the code in this post this time has not been fully tested.

Regards,
Daz.
0
 
Daz_1234Commented:
Spotted a problem.  Your RegKeyExists function does not Dim its variables, but the error is hidden behind the ON Error Resume Next.  I have amended the function slightly below:
Function RegKeyExists(sKey)
    Dim wshShell
    Set wshShell = CreateObject("Wscript.Shell")

    On Error Resume Next
    wshShell.RegRead sKey
    If Err.Number <> 0 Then
        RegKeyExists = False
    Else
        RegKeyExists = True
    End If
    On Error Goto 0
End Function

Open in new window


Regards,
Daz.
0
 
PKI_Program_OfficeAuthor Commented:
Daz,

Thanks.  One more slight adjustment that Im having a problem with.  I actually am trying to install the .msi from http but when I add the URL and run the script, Im getting "ERROR: installation file does not exist: ".

I suspect the reason is it's looking for a file on the c:/.  Also I think I might have to use msiexec /p to install my patch files .msp.  How would I create another function for this?  Thanks for all your help.  It's been very educational.
0
 
Daz_1234Commented:
You're getting your points-worth out of this one ;o)

Before running the installation, the function I provided checks for the existence of a file in the sPath variable.
I will add to ensure that it does not check for a URL.  Below is a replacement fInstall function:

Function fInstall(sPath)
    Dim fso, ret
    If LCase(Left(sPath, 4)) <> "http" Then
        Set fso = CreateObject("Scripting.FileSystemObject")
        If Not fso.FileExists(sPath) Then
            WLog "ERROR: installation file does not exist: " & sPath
            WScript.Quit(1)
        End If
    End If
    ret = wshShell.Run ("msiexec /i """ & sPath & """ ALLUSERS=1 /norestart /qb", 8, True)
    fUninstall = ret
End Function

Open in new window


Regards,
Daz

For patching you could add another function like this:

Function fPatch(sPath)
    Dim fso, ret
    If LCase(Left(sPath, 4)) <> "http" Then
        Set fso = CreateObject("Scripting.FileSystemObject")
        If Not fso.FileExists(sPath) Then
            WLog "ERROR: installation file does not exist: " & sPath
            WScript.Quit(1)
        End If
    End If
    ret = wshShell.Run ("msiexec /p """ & sPath & """ /norestart /qb", 8, True)
    fPatch = ret
End Function

Open in new window

0
 
PKI_Program_OfficeAuthor Commented:
Daz - I was hoping you could help me with one more thing with this.  I am now trying to implement installing a .msi with a .mst from the http site.  I am receiving a 1619 error now.  my code is the following:

arrInstallMSI(0) = "http://pathtomsi.com/myfile1.msi TRANSFORMS=http://pathtomsi.com/myfile1.mst"

and I am calling the fInstall(arrInstallMSI(0)) using the fInstall(sPath) function.  Any ideas.  Thanks.
0
 
Daz_1234Commented:
You can't do that directly because I added extra quotes in the msiexec.exe line to cater for spaces in the msi name.

If you Replace this line:
    ret = wshShell.Run ("msiexec /i """ & sPath & """ ALLUSERS=1 /norestart /qb", 8, True)
... with:
    ret = wshShell.Run ("msiexec /i " & sPath & " ALLUSERS=1 /norestart /qb", 8, True

Open in new window

)

And then make sure that IF you have any spaces in paths you add the extra quotes in the array entry, you'll be Ok.

E.g.
arrInstallMSI(8) = """f:\path with\spaces in it\program.msi"" TRANSFORMS=""f:\path with\spaces in it\myfile1.mst"""

Open in new window


Your line pasted above has no spaces so just replacing the msiexec line will be Ok.

Regards,
Daz.
0
 
PKI_Program_OfficeAuthor Commented:
Thanks for all the help Daz.  Question - If I wanted to go back to installing these files from the local computer is there a way to make it work so that I dont have to put the entire path.  For example, if I ran the script from the "path with" folder, couldn't I just put:

arrInstallMSI(8) = """spaces in it\program.msi"" TRANSFORMS=""spaces in it\myfile1.mst"""

I cant seem to get it to work.  It keeps giving me the ERROR: installation file does not exist:

thanks.
arrInstallMSI(8) = """c:\path with\spaces in it\program.msi"" TRANSFORMS=""f:\path with\spaces in it\myfile1.mst"""

Open in new window

0
 
Daz_1234Commented:
You then run into the problem with what is the current working directory.  For example:

You got folders:

C:\installsource\msi\

and the script is:

C:\installsource\installscript.vbs

... and the msi/mst is:

C:\installsource\msi\program.msi
C:\installsource\msi\program.mst

... and you double-click the .vbs from the C:\installsource folder then this should work:

arrInstallMSI(8) = """msi\program.msi"" TRANSFORMS=""mst\myfile1.mst"""

... BUT - if you call the script from somewhere else, like C:\Windows like this (meant to look like a command window line):

C:\WINDOWS>c:\installsource\installscript.vbs

... then it will fail.  This is because the msi folder will not be relative to script, but relative to the calling folder and then it will look in C:\WINDOWS\msi  and it will not exist.


This is what I do:

1. Set a variable with the current script location (will end in a backslash):
strCurrDir = Replace(WScript.ScriptFullName, WScript.ScriptName, "")

2. use the new variable in the path:
arrInstallMSI(8) = """" & strCurrDir & "msi\program.msi"" TRANSFORMS=""" & strCurrDir & "mst\myfile1.mst"""

... then it doesn't matter where you call it from.

Good luck,
Daz.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now