Thursday, February 28, 2008

How is CreateObject different from GetObject?

One of my junior colleague had asked the question "What is the difference between CreateObject and GetObject and in what situation GetObject is to be used?". This post is the answer that I have given him.

  1. CreateObject creates an instance of a class
  2. GetObject gets the reference of an running object to another object
  3. Always use CreateObject except for Windows Management Instrumentation (WMI) and Active Directory Service Interfaces (ADSI)

The below script demonstrates the use of these two VBScript functions.

Set objExcel = CreateObject("Excel.Application") 
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add

Set objExcel2 = GetObject(, "Excel.Application")
Set objWorkbook = objExcel2.Workbooks(1)
Set objWorksheet = objWorkbook.Worksheets(1)
objExcel2.Cells(1, 1).Value = "In god I TRUST everything else I TEST."


-- Lakshminarasimha Manjunatha Mohan

In God I TRUST everything else I TEST.

Windows Hotfixes and Patches Enumerator

Below is a simple VBScript developed using Windows WMI that can be executed from Windows Script Host to enumerate the installed hotfixes, patches and security updates on any given machine.

You might be wondering what for this script is useful. While product testing that supports different environments it happens so that suddenly one fine day you might notice some mis-behavior or unusual defects in the application. Actually these mis-behaviors or defects are not always related to the application. However, it may be a compatibility issue with one of the patches or security updates made by the windows automatic updates.

As a solution to this problem I am this script to find out all the patches, hot fixes and security updates on a server where the product is working fine and compare the that with another server where the mis-behavior or defects are seen and thus classify a defect from compatibility issue. This is really a very handy script for compatibility testing. Let me know your comments on this. I intend to extend this script for comparing two different lists of hotfixes and patches so as to make it easier to find the differences.

For executing this script it is necessary have an EXCEL file named test.xls at C:\

'Description: This script enumerates the Windows hotfixes
' and other security updates
'Author: Lakshminarasimha M.
strComputer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colQuickFixes = objWMIService.ExecQuery ("Select * from Win32_QuickFixEngineering")

sDataTable = "C:\test.xls"
sDataSheet = "Sheet1"

iReqRow = 2
Dim oXLApp 'As Excel.Application
Dim oWorkBook 'As Excel.Workbook
Dim oWorkSheet 'As Excel.Worksheet
Dim iRowsCount 'As Integer
Dim iColsCount 'As Integer
Dim sColHeader 'As String
Dim sReqColumn 'As String

Set oXLApp = CreateObject("Excel.Application")
'oXLApp.Visible = True
Set oWorkBook = oXLApp.Workbooks.Open(sDataTable)
Set oWorkSheet = oWorkBook.Sheets(sDataSheet)
iRowsCount = oWorkSheet.UsedRange.Rows.Count
iColsCount = oWorkSheet.UsedRange.Columns.Count
' Set Column Headers
oWorkSheet.Range("A1").Value = "Caption"
oWorkSheet.Range("B1").Value = "CSName"
oWorkSheet.Range("C1").Value = "Description"
oWorkSheet.Range("D1").Value = "FixComments"
oWorkSheet.Range("E1").Value = "HotFixID"
oWorkSheet.Range("F1").Value = "InstallDate"
oWorkSheet.Range("G1").Value = "InstalledBy"
oWorkSheet.Range("H1").Value = "InstalledOn"
oWorkSheet.Range("I1").Value = "Name"
oWorkSheet.Range("J1").Value = "ServicePackInEffect"
oWorkSheet.Range("K1").Value = "Status"

For Each objQuickFix in colQuickFixes
oWorkSheet.Range("A" & iReqRow).Value = objQuickFix.Caption
oWorkSheet.Range("B" & iReqRow).Value = objQuickFix.CSName
oWorkSheet.Range("C" & iReqRow).Value = objQuickFix.Description
oWorkSheet.Range("D" & iReqRow).Value = objQuickFix.FixComments
oWorkSheet.Range("E" & iReqRow).Value = objQuickFix.HotFixID
oWorkSheet.Range("F" & iReqRow).Value = objQuickFix.InstallDate
oWorkSheet.Range("G" & iReqRow).Value = objQuickFix.InstalledBy
oWorkSheet.Range("H" & iReqRow).Value = objQuickFix.InstalledOn
oWorkSheet.Range("I" & iReqRow).Value = objQuickFix.Name
oWorkSheet.Range("J" & iReqRow).Value = objQuickFix.ServicePackInEffect
oWorkSheet.Range("K" & iReqRow).Value = objQuickFix.Status
iReqRow = iReqRow + 1
Set oWorkSheet = Nothing
Set oWorkBook = Nothing
Set oXLApp = Nothing
MsgBox("---Hotfixes Enumerated Sucessfully.---")

-- Lakshminarasimha Manjunatha Mohan

In God I TRUST everything else I TEST.