I have written a VBScript that queries a list of servers from a text file and produces a list of the services and there status and stores it in database. I need the script to update the tables everytime it runs and to add any new server or services to the database. I cant seem to get the update part of the script to work. If anyone has any ideas I appricate it.
The script is below
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
objConnection.Open "DSN=script;"
objRecordset.CursorLocation = adUseClient
objRecordset.Open "SELECT * FROM Services" , objConnection, adOpenStatic, adLockOptimistic
On Error Resume Next
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("servers.txt", ForReading)
i = 0
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
For Each objItem in objDictionary
StrComputer = objDictionary.Item(objItem)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPrintServices = objWMIService.ExecQuery ("Select * from Win32_Service")
bFoundService = false
For Each objService in colprintServices
if (objRecordSet("ServerName") = strComputer & objRecordSet("ServiceName") = objService.DisplayName) then
bFoundService = true
objRecordSet("ServiceState") = objService.State
objRecordSet("Date") = FormatDateTime(Now(), 2)
objRecordSet("Time") = Time
objRecordSet.Update
end if
Next
if bFoundService = false then
objRecordSet.AddNew
objRecordSet("ServerName") = strComputer
objRecordSet("ServiceName") = objService.DisplayName
objRecordSet("ServiceState") = objService.State
objRecordSet("Date") = FormatDateTime(Now(), 2)
objRecordSet("Time") = Time
objRecordSet.Update
end if
Next
objRecordset.Close
objConnection.Close
Cheers
The script is below
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")
objConnection.Open "DSN=script;"
objRecordset.CursorLocation = adUseClient
objRecordset.Open "SELECT * FROM Services" , objConnection, adOpenStatic, adLockOptimistic
On Error Resume Next
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("servers.txt", ForReading)
i = 0
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
objDictionary.Add i, strNextLine
i = i + 1
Loop
For Each objItem in objDictionary
StrComputer = objDictionary.Item(objItem)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPrintServices = objWMIService.ExecQuery ("Select * from Win32_Service")
bFoundService = false
For Each objService in colprintServices
if (objRecordSet("ServerName") = strComputer & objRecordSet("ServiceName") = objService.DisplayName) then
bFoundService = true
objRecordSet("ServiceState") = objService.State
objRecordSet("Date") = FormatDateTime(Now(), 2)
objRecordSet("Time") = Time
objRecordSet.Update
end if
Next
if bFoundService = false then
objRecordSet.AddNew
objRecordSet("ServerName") = strComputer
objRecordSet("ServiceName") = objService.DisplayName
objRecordSet("ServiceState") = objService.State
objRecordSet("Date") = FormatDateTime(Now(), 2)
objRecordSet("Time") = Time
objRecordSet.Update
end if
Next
objRecordset.Close
objConnection.Close
Cheers