' Install or uninstall MSU update
' If no argument or with argument INSTALL, script will install all MSUs found in folder auto selecting platform.
' USAGE: cscript runMSU.vbs -> install all MSU updates in folder where is script
' or
' USAGE: cscript runMSU.vbs UNINSTALL 123456 -> to Uninstall specified MSU update
'========================================================================================
'// ---------------------------------------------------------------------------------
'// Define critical global objects
'// ---------------------------------------------------------------------------------
Dim WshShell, WShSysEnv, strLogPath1, sFileSize, objFS
Dim objShell, objFso, sSMSLogShare, sLogFile
Set objFS = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("PROCESS")
Set ws = CreateObject ("Scripting.FileSystemObject")
strScriptName = wscript.scriptfullname
strScriptPath = replace(strScriptName,wscript.scriptname,"")
strComputer = wshShell.ExpandEnvironmentStrings("%Computername%")
sSMSLogShare = "\\" & strComputer & "\SMS_Logs"
'// ---------------------------------------------------------------------------------
'// Define Constants
'// ---------------------------------------------------------------------------------
Const SCRIPT_TITLE = "Install MSU"
Const VERSION = "1.3"
Const AUTHOR = "Neven Radic"
Const CREATED = "Dec-08-2009"
Const LAST_MODIFIED = "Jan-12-2010"
Const LAST_MODIFIED_BY = "Neven Radic"
'// OpenTextFile constants
Const FILE_READ = 1
Const FILE_APPEND = 8
Const FILE_WRITE = 2
Const MAX_LOG_SIZE = 300000 ' Set max log file size here
'// ---------------------------------------------------------------------------------
'// Main
'// ---------------------------------------------------------------------------------
'// Log path logic ------------------------------------------------------------------
Call GetLogPath 'Define LogPath
If objFS.FolderExists(sSMSLogShare) Then 'If SMS_Logs share exit , puts logs there
strLogPath1 = sSMSLogShare
sLogFile = sSMSLogShare
End If
sLogFile = strLogPath1 & "\" & strComputer & "_InstallMSU.log"
WScript.Echo "(sLogFile): " & sLogFile
Call CheckLogSize 'and delete if biger then 'MAX_LOG_SIZE'
If (objFS.FileExists(sLogFile)) Then
Else
Log("-----------------------------------------------------------------------------")
Log(SCRIPT_TITLE & " version " & VERSION)
Log("By: " & AUTHOR)
Log("Created: " & CREATED & " " &", Last modified: " & LAST_MODIFIED & ", By: " & LAST_MODIFIED_BY)
Log("Log file: " & sLogFile)
Log("-----------------------------------------------------------------------------" & vbCrLf)
End If
On Error Resume Next
'\\ SCRIPT LOGIC HERE --------------------------------------------------------------
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'Dim wshShell: Set wshShell = CreateObject("Wscript.Shell")
scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
On Error Resume Next
Set scriptFolder = fso.GetFolder(ScriptDir)
Set scriptFolderFiles =scriptFolder.Files
Dim scriptDir, scriptFolder, scriptFolderFiles, file, objArgs
DIM sARC, strComputer, objWMIService, colSettings, objProcessor
Dim CMDSW : CMDSW = " /quiet /norestart"
DIM totalInstalled, totalNotApp
totalInstalled = 0 : totalNotApp = 0
exitcode = 0
WScript.Echo "Script started: -- " & Now
Log("Script started... -- " & Now)
Log("Script Path: " & strScriptPath)
Log(" ")
Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1
WScript.Echo "Arguments: " & objArgs(I)
If objArgs(0) <> "" Then
sARG0 = UCase(objArgs(0))
End If
If objArgs(1) <> "" Then
sARG1 = UCase(objArgs(1))
End If
Next
Log("Arguments: " & objArgs(I))
If sARG0 = "UNINSTALL" Then
WScript.Echo "Uninstall CMD: wusa.exe /uninstall /kb:"& sARG1 & " /quiet /norestart"
Log("Processing Uninstall-- CMD: wusa.exe /uninstall /kb:"& sARG1 & " /quiet /norestart")
UnInst = WshShell.Run("wusa.exe /uninstall /kb:"& sARG1 & " /quiet /norestart", 0 ,True)
If UnInst = 0 Then
WScript.Echo "The MSU update uninstalled successfuly: " & file.name
Log("The MSU update uninstalled successfuly: " & file.name)
ElseIf UnInst = 2359303 Then
WScript.echo "The MSU update can't be found: " & sARG1
Log("The MSU update can't be found: " & sARG1)
Else
Wscript.echo "An error occurred: '" & UnInst & "' while trying to uninstall the MSU update."
Log("An error occurred: '" & UnInst & "' while trying to uninstall the MSU update.")
End If
Log("script exiting...")
WScript.Quit
ElseIf sARG0 = "INSTALL" Or sARG0 = "" Then
Call GetCPU 'return sARC = X64|X86
Log("Platform detected: " & sARC)
WScript.Echo "Platform detected: " & sARC
'Loop thru all files in the folder that the script is ran in
For Each file in scriptFolderFiles
If Ucase(Right(file.name, 4)) = ".MSU" Then
If InStr(Ucase(file.name),sARC) <> 0 Then
WScript.Echo "Processing: " & file.Name
WScript.Echo "CMD: wusa.exe " & ScriptDir & "\" & file.name & CMDSW
Log("Processing CMD: wusa.exe " & ScriptDir & "\" & file.Name & CMDSW)
iRetVal = WshShell.Run("wusa.exe " & ScriptDir & "\" & file.Name & CMDSW, 0, True)
If iRetVal = 0 Or iRetVal = 3010 Then
WScript.Echo "The MSU update installed successfuly: " & file.Name
Log("The MSU update installed successfuly: " & file.Name)
totalInstalled = totalInstalled + 1
ElseIf iRetVal = 2359302 Then
totalNotApp = totalNotApp +1
WScript.echo "The MSU update installed already: " & file.Name
Log("The MSU update was installed already: " & file.Name)
Else
totalNotApp = totalNotApp + 1
WScript.Echo "The Update is not applicabe or there was an error during install: " & file.Name
Log("The Update is not applicable, or there was an error during install: " & file.Name)
exitcode = iRetVal
End If
End If
End If
Log(" ")
Next
WScript.Echo "A total MSU installed: " & totalInstalled & ", not applicable or error(s): " & totalNotApp
Log("Total MSU installed: " & totalInstalled & ", not applicable or error(s): " & totalNotApp )
Log(" ")
End If
If iRetVal = 0 Or 3010 Or 2359302 Or -2145124329 Or 2149842967 Then
WScript.Echo "Script completed successfully. -- " & Now
Log("Script completed successfully -- " & Now)
WScript.Quit(exitcode)
Else
WScript.Echo "Script completed with ExitCode: '" & exitcode & "' -- " & Now
Log("Script completed with ExitCode: '" & exitcode & "' -- " & Now)
Log("For MSU Install detail information of MSU install, please see the Setup Events logs")
WScript.Quit(exitcode)
End If
'\\ SUBs & Functions ----------------------------------------------------------------
Sub GetCPU 'return sARC = X64|X86
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,authenticationLevel=Pkt}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
For Each objProcessor In colSettings
Wscript.Echo "OSArchitecture: " & objProcessor.OSArchitecture
If InStr(objProcessor.OSArchitecture,"86")Then
sARC = "X86"
ElseIf InStr(objProcessor.OSArchitecture,"64") Then
sARC = "X64"
End If
Next
End Sub
' Loging ---------------------------------------------------------------
Sub Log (sMsg)
If sLogFile <> "" Then WriteTextFile sLogFile, sMsg
End Sub
'========================================================================
' Functions: Log
' Writes a specified string to a text file.
' Will append if the file exists, and create the file if it doesn't.
'========================================================================
Function WriteTextFile(sFileName, sMsg)
WriteTextFile = TRUE
On Error Resume Next
Dim objFile : Set objFile = objFS.OpenTextFile(sFileName, FILE_APPEND, TRUE)
objFile.Writeline(sMsg)
If ( Err.Number <> 0 ) Then
WriteTextFile = FALSE
End If
objFile.Close
Set objFile = Nothing
On Error Goto 0
End Function
'========================================================================
' Delete logfile function
' Function to delete log file (if log bigger then MAX specified
'========================================================================
Function CheckLogSize
If (objFS.FileExists(sLogFile)) Then
Set f = objFS.GetFile(sLogFile)
sFileSize = UCase(f.Name) & " is: " & f.size & " bytes."
If f.Size > MAX_LOG_SIZE Then
objFS.DeleteFile(sLogFile)
End If
End If
If (objFS.FileExists(StatusFile)) Then
Set f = objFS.GetFile(StatusFile)
sFileSize = UCase(f.Name) & " is: " & f.size & " bytes."
If f.Size > MAX_LOG_SIZE Then
objFS.DeleteFile(StatusFile)
End If
End If
End Function
'========================================================================
' Sub Get LogPath
' Set variables for temporary location of output and status files.
' Output : strLogPath1
'========================================================================
Sub GetLogPath
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOSes = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOS in colOSes
If InStr(UCase(objOS.Caption),"XP") Then
strLogPath1="C:\XME\Utils" 'This is XP System
If ReportFolderStatus(strLogPath1) = "does not exist" Then
strLogPath1 = WshSysEnv("TEMP")
End If
ElseIf InStr(UCase(objOS.Caption),"7") Then ' This is Win 7
strLogPath1="C:\XOM\LOGS\SYSTEM"
If ReportFolderStatus(strLogPath1) = "does not exist" Then
strLogPath1 = WshSysEnv("TEMP")
End If
ElseIf InStr(UCase(objOS.Caption),"SERVER") then
strLogPath1="C:\NTUTILS"
If ReportFolderStatus(strLogPath1) = "does not exist" Then
strLogPath1 = WshSysEnv("TEMP")
End If
Else
strLogPath1 = WshSysEnv("TEMP")
End If
Next
WScript.Echo "(strLogPath1):" & strLogPath1
End Sub
'====================================================================
'function: checkin if folder exist ot not
'input: fldr
'output: exist | does not exist
'====================================================================
Function ReportFolderStatus(fldr)
Dim fso, msg
msg = ""
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FolderExists(fldr)) Then
msg = "exists"
Else
msg = "does not exist"
End If
ReportFolderStatus = msg
End Function
'-------------------------- END ------------------------------------