Sie sind auf Seite 1von 3

'*********************************************************************************

'Script to remotely inventory installed services from a list of machines from a


' text file, put the services into an Excel workbook, and then save the
' workbook to the desktop.
' Created by: Brian Bohanon
' Created: 6/17/2008
'*********************************************************************************

Option Explicit

Dim arrFileLines()
Dim tst
Dim i, j, k, l
Dim objFSO, objFile, objDialog, objExcel, objWMIService, objService, objWorkbook
Dim WshShell
Dim colServices
Dim srcFileName 'source file
Dim intResult
Dim strComputer 'computer to inventory

'Constants
Const xlLeft = -4131
Const xlHorizontal = -4128

'Set Objects
Set objExcel = CreateObject("Excel.Application")
Set objDialog = CreateObject("UserAccounts.CommonDialog")
Set WshShell = WScript.CreateObject("WScript.Shell")

'Open the source file that contains the list of computers


objDialog.Filter = "Text Files|*.txt"
objDialog.InitialDir = WshShell.SpecialFolders("Desktop")
intResult = objDialog.ShowOpen

If intResult = 0 Then
Wscript.Quit
Else
'Set selected file to srcFileName
srcFileName = objDialog.FileName

i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(srcFileName, 1)
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

'*********************************************************************************
' Create a new Excel Workboot
'*********************************************************************************
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()

'*********************************************************************************
'Loop through the array of computers, create a worksheet with the computer name,
' get the list of services,
' and write out the services to the excel sheet
'*********************************************************************************

k = 3
objExcel.Worksheets("Sheet1").Delete
objExcel.Worksheets("Sheet2").Delete

For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1


On Error Resume Next
if k = 3 then
objExcel.Worksheets("Sheet" & k).Activate
objExcel.Worksheets("Sheet" & k).Name = arrFileLines(l)
else
objExcel.Worksheets.Add
objExcel.Worksheets("Sheet" & k).Activate
objExcel.Worksheets("Sheet" & k).Name = arrFileLines(l)
end if

'*********************************************************************************
'Services Information
'*********************************************************************************

'Select server to inventory


Set objWMIService = GetObject("winmgmts:\\" & arrFileLines(l) &
"\root\cimv2")
Set colServices = objWMIService.ExecQuery("Select * From
Win32_Service")

i = 1
j = 1

'Column headers
objExcel.Cells(i, j).Value = "Service"
objExcel.Cells(i, j).Font.Bold = TRUE
j = j + 1
objExcel.Cells(i, j).Value = "Status"
objExcel.Cells(i, j).Font.Bold = TRUE
j = j + 1
objExcel.Cells(i, j).Value = "Start Mode"
objExcel.Cells(i, j).Font.Bold = TRUE
j = j + 1
objExcel.Cells(i, j).Value = "Start Name"
objExcel.Cells(i, j).Font.Bold = TRUE
j = j + 1
objExcel.Cells(i, j).Value = "Path Name"
objExcel.Cells(i, j).Font.Bold = TRUE

'Services data
For Each objService in colServices
i = i + 1
j = 1
objExcel.Cells(i, j) = objService.Name
j = j + 1
objExcel.Cells(i, j) = objService.State
j = j + 1
objExcel.Cells(i, j) = objService.StartMode
j = j + 1
objExcel.Cells(i, j) = objService.StartName
j = j + 1
objExcel.Cells(i, j) = objService.PathName
Next

' Autofit the first column to fit the longest service name
objExcel.Columns("A:Z").EntireColumn.AutoFit
objExcel.Columns("A:Z").HorizontalAlignment = xlLeft

k = k + 1
Next

'Save the workbook to the desktop


objWorkbook.SaveAs(WshShell.SpecialFolders("Desktop") & "\ServerServices.xls")

'Close Excel
objExcel.Quit

End If

'*********************************************************************************
'Cleanup
'*********************************************************************************

'Cleanup objects

Set objExcel = nothing


Set objFSO = nothing
Set objWMIService = nothing
Set objService = nothing
Set objFile = nothing
Set objDialog = nothing
Set WshShell = nothing

'Inform the user that the process is complete


Wscript.Echo("Finished")

Wscript.Quit 0

Das könnte Ihnen auch gefallen