![]() |
Table of Contents |
![]() |
Listing Hot Fixes and SoftwareWouldn't it be nice to have a script that you could run on each computer in your enterprise to get an inventory of hot fixes and software applications? It's not hard! Rather than showing you a single sample script, though, I want to walk through this example a bit more modularly. The first thing I need is a routine that determines the local computer's name, and then opens an output text file on a file server somewhere. Dim oNetwork Set oNetwork = CreateObject("WScript.Network") Dim sLocal sLocal = oNetwork.ComputerName Dim oFSO, oTS Set oFSO = CreateObject("Scripting.FileSystemObject") Set oTS = oFSO.CreateTextFile("\\server\" & _ sLocal & ".txt") This results in an object oTS, which is a TextStream object representing an output text file. The file is named after the computer on which it runs, and you can modify the location to be a file server in your environment. I just need to find a list of hot fixes and applications, and I don't need to turn any further than the Scriptomatic tool, or the WMI Query Wizard in PrimalScript. Hot fixes are formally known as QFEs, or Quick Fix Engineering patches, and there's a WMI class just for them. The following wizard-generated code queries it for me. On Error Resume Next Dim strComputer Dim objWMIService Dim colItems strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_QuickFixEngineering",,48) For Each objItem in colItems WScript.Echo "Caption: " & objItem.Caption WScript.Echo "CSName: " & objItem.CSName WScript.Echo "Description: " & objItem.Description WScript.Echo "FixComments: " & objItem.FixComments WScript.Echo "HotFixID: " & objItem.HotFixID WScript.Echo "InstallDate: " & objItem.InstallDate WScript.Echo "InstalledBy: " & objItem.InstalledBy WScript.Echo "InstalledOn: " & objItem.InstalledOn WScript.Echo "Name: " & objItem.Name WScript.Echo "ServicePackInEffect: " & _ objItem.ServicePackInEffect WScript.Echo "Status: " & objItem.Status Next Similarly, I can query for installed products (software packages) with the following code. On Error Resume Next Dim strComputer Dim objWMIService Dim colItems strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_Product",,48) For Each objItem in colItems WScript.Echo "Caption: " & objItem.Caption WScript.Echo "Description: " & objItem.Description WScript.Echo "IdentifyingNumber: " & objItem.IdentifyingNumber WScript.Echo "InstallDate: " & objItem.InstallDate WScript.Echo "InstallDate2: " & objItem.InstallDate2 WScript.Echo "InstallLocation: " & objItem.InstallLocation WScript.Echo "InstallState: " & objItem.InstallState WScript.Echo "Name: " & objItem.Name WScript.Echo "PackageCache: " & objItem.PackageCache WScript.Echo "SKUNumber: " & objItem.SKUNumber WScript.Echo "Vendor: " & objItem.Vendor WScript.Echo "Version: " & objItem.Version Next Again, that's straight from the wizard, so there's not much effort involved. Now, PrimalScript's WMI Query Wizard generates code that echoes to the command line or a message box; to write to my output file I can just replace the Wscript.Echo with oTS.WriteLine. I can eliminate any queried information that I don't care about, and eliminate redundant lines in the two segments of wizard-generated code. Listing 31.7 shows the completed script. Listing 31.7. Inventory.vbs. Lists all hot fixes and software on the local computer and outputs the list to a text file.Dim oNetwork Set oNetwork = CreateObject("WScript.Network") Dim sLocal sLocal = oNetwork.ComputerName Dim oFSO, oTS Set oFSO = CreateObject("Scripting.FileSystemObject") Set oTS = oFSO.CreateTextFile("\\server\" & _ sLocal & ".txt") On Error Resume Next Dim strComputer Dim objWMIService Dim colItems oTS.WriteLine oTS.WriteLine "INSTALLED HOTFIXES" oTS.WriteLine strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_QuickFixEngineering",,48) For Each objItem in colItems oTS.WriteLine "HotFixID: " & objItem.HotFixID oTS.WriteLine "ServicePackInEffect: " & _ objItem.ServicePackInEffect oTS.WriteLine "Status: " & objItem.Status oTS.WriteLine Next oTS.WriteLine oTS.WriteLine "INSTALLED SOFTWARE" oTS.WriteLine strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery("Select * from Win32_Product",,48) For Each objItem in colItems oTS.WriteLine "Caption: " & objItem.Caption oTS.WriteLine "Version: " & objItem.Version oTS.WriteLine Next I added a few extra lines to the text file to make it easier to read. You could modify this script to use ADO to write to a database or Excel spreadsheet, if you want, or to some other format. |
![]() |
Table of Contents |
![]() |