Ejemplo de uso de script de objetos de automatización

El siguiente script se conecta con el objeto de automatización de Excel, crea una nueva planilla y exporta la lista de servicios y el estado respectivo de estos. Este script requieres Excel y también WMI.

Nota

Windows Management Instrumentation viene preinstalada de Windows 2000 en adelante. Se debe instalar en Windows 9x y Windows NT. Enlace de descarga: 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")

'Obtención de un objeto wmi que permita la recuperación de varios datos

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

'mostrar la versión de Excel

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

'Agregar libro

Set book = excel.workbooks.add

'Agregar hoja de cálculo al libro

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"

'Recuperar información de servicios

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

'Realizar ciclos de todos los objetos de servicios

lnpos = lnpos + 1

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

'Introducir servicios en la planilla de Excel

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

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

Siguiente

'Ajustar columnas de forma automática

sheet.columns.AutoFit

'Mostrar la planilla de Excel

sheet.application.visible = true

End If

End If

End If

End Function