LISTING 3: Code to Retrieve OS Data from All PCs Option Explicit On Error Resume Next BEGIN COMMENT LINE ' Declare constants and variables. END COMMENT LINE Const FSO_FILE_FORCE_DELETE = TRUE Const xlNormal = &HFFFFEFD1 Const xlSolid = 1 Const xlShiftDown = &HFFFFEFE7 Const xlShiftUp = &HFFFFEFBE Const xlToLeft = &HFFFFEFC1 Const SHELL_RUN_HIDE_WINDOW = 0 Const SHELL_RUN_WAIT_UNTIL_FINISHED = TRUE Const PATH = "C:\" Const CSV = "Machines.csv" Const CSV_LOG = "CSV.LOG" Const XLS = "Results.xls" Const AD_ROOT = "dc=mycorp,dc=com" Dim wshShell, appExcel, fso, intRowIndex, strComputer Dim objWMIService, colItems, objItem , strCommand BEGIN COMMENT LINE ' Use Csvde to export all client PC DNS names from AD ' to a CSV file. END COMMENT LINE # BEGIN CALLOUT A strCommand = "csvde.exe -f " & PATH & CSV & " -d """ & _ AD_ROOT & """ -r ""(&(objectClass=Computer))"_ " -p SubTree -l dNSHostName" Set wshShell=WScript.CreateObject("WScript.Shell") wshShell.run strCommand, SHELL_RUN_HIDE_WINDOW, _ SHELL_RUN_WAIT_UNTIL_FINISHED # END CALLOUT A BEGIN COMMENT LINE ' Open the CSV file in Excel and save the .xls file. END COMMENT LINE Set appExcel = CreateObject("Excel.Application") appExcel.Workbooks.Open PATH & CSV appExcel.ActiveWorkbook.SaveAs PATH & XLS, xlNormal BEGIN COMMENT LINE ' Delete old CSV file and log file. END COMMENT LINE Set fso = CreateObject("Scripting.FileSystemObject") fso.DeleteFile PATH & CSV, FSO_FILE_FORCE_DELETE fso.DeleteFile PATH & CSV_LOG, FSO_FILE_FORCE_DELETE BEGIN COMMENT LINE ' Delete unnecessary first column and first row. END COMMENT LINE appExcel.Columns("A:A").Select appExcel.Selection.Delete xlToLeft appExcel.Rows("1:1").Select appExcel.Selection.Delete xlShiftUp BEGIN COMMENT LINE ' Set up the OS data sheet and insert a heading row. END COMMENT LINE appExcel.Worksheets.Select appExcel.ActiveSheet.Name = "OS Details for All PCs" appExcel.Rows("1:1").Select appExcel.Selection.Insert xlShiftDown BEGIN COMMENT LINE ' Print and format the first row column headers. END COMMENT LINE appExcel.Cells(1,1).Value="PC Name" appExcel.Cells(1,2).Value="Caption" appExcel.Cells(1,3).Value="Version" appExcel.Range("A1:C1").Select appExcel.Selection.Interior.ColorIndex = 5 appExcel.Selection.Interior.Pattern = xlSolid appExcel.Selection.Font.ColorIndex = 2 appExcel.Selection.Font.Bold = True BEGIN COMMENT LINE ' Loop through the .xls file and read PC DNS names. END COMMENT LINE intRowIndex = 2 While appExcel.Cells(intRowIndex,1).Value <> "" BEGIN COMMENT LINE ' Attempt to use WMI to connect to each PC and write out the results. END COMMENT LINE strComputer = appExcel.Cells(intRowIndex,1).Value Set objWMIService = GetObject("winmgmts:\\" & strComputer _ & "\root\cimv2") If Not (objWMIService Is Nothing) Then Set colItems = objWMIService.ExecQuery("Select * from "& _ "Win32_OperatingSystem",,48) For Each objItem in colItems appExcel.Cells(intRowIndex,1).Value = strComputer appExcel.Cells(intRowIndex,2).Value = objItem.Caption appExcel.Cells(intRowIndex,3).Value = objItem.Version appExcel.Rows(intRowIndex + 1 & ":" & intRowIndex +1)._ Insert xlShiftDown intRowIndex = intRowIndex + 1 Next appExcel.Rows(intRowIndex & ":" & intRowIndex).Delete _ xlShiftUp Set objWMIService = Nothing Else intRowIndex = intRowIndex + 1 End If BEGIN COMMENT LINE ' Autofit the columns and rows, save the .xls file, and display Excel. END COMMENT LINE appExcel.Cells.Select appExcel.Cells.EntireColumn.AutoFit appExcel.Cells.EntireRow.AutoFit appExcel.ActiveWorkbook.Save appExcel.Visible = True