Sample usage of automation objects script

The following script connects with excel automation Object, creates a new sheet and exports the list of services and their respective status. This script requires Excel and also WMI.

NOTE

Windows Management Instrumentation, comes pre-installed on Windows 2000 upwards. It must be installed on Windows 9x and Windows NT. download link: http://go.gfi.com/?pageid=_wmi

Function Main

Dim excel As Object 'Creation of objects needed by the script

Dim book As Object

Dim sheet As Object

Dim range As Object

Dim columns As Object

Dim wmiobj As Object

Dim objswbemobject As Object

strComputer = "127.0.0.1"

Set wmiobj = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

'Getting a wmi Object which allows retrieval of various information

If wmiobj is Nothing Then

echo ("Error1 Unable to create WMI Object")

Else

Set excel = CreateObject("Excel.application")

'Connecting to the Excel Automation Object

If excel is Nothing Then

Echo("Unable to create Excel Object")

Else

'display excel's Version

echo ("initalised session with Excel Version "&excel.version)

'Add workbook

Set book = excel.workbooks.add

'Add worksheet to workbook

Set sheet = Book.Worksheets.add

sheet.cells(1,1) = "This Sheet has been generated from with a GFI LanGuard Script"

'Setup Column names

sheet.cells(3,1) = "Service Name"

sheet.cells(3,2) = "State"

sheet.cells(3,3) = "Started"

'Retrieve Services info

Set wmiinst=wmiobj.instancesof("Win32_Service")

If wmiinst is Nothing Then

echo ("error2: Unable to retrieve services information")

Else

lnpos = 4

For Each objswbemobject In wmiinst

'Loop through all services objects

lnpos = lnpos + 1

sheet.cells(lnpos,1) = objswbemobject.DisplayName

'Enter services info into the excel sheet

sheet.cells(lnpos,2) = objswbemobject.State

sheet.cells(lnpos,3) = objswbemobject.Started

Next

'Auto fit Columns

sheet.columns.AutoFit

'Display the excel sheet

sheet.application.visible = true

End If

End If

End If

End Function