Link to home
Start Free TrialLog in
Avatar of Wayne-
Wayne-

asked on

Need to pull host from a text file to read script and output results to a text file

I have a Vbscript that I need to alter so that I can have it pull the computer from a text file and output the results to a text file
' Global Variables
' ****************
strComputer = "." 
 
' Create Log File
'****************
' Get Script Path
Set filesys = CreateObject("Scripting.FileSystemObject")
scriptPath = Left(WScript.ScriptFullName,InstrRev(WScript.ScriptFullName,".") -1)
logName = scriptPath & "_" & "log.txt"
Set logFile = filesys.CreateTextFile(logName, True)
logFile.WriteLine Now() & vbCrLf & strComputer & vbCrLf & "********************"
 
 
' Check SAV processes
' *******************
Call CheckService(strComputer,"Symantec AntiVirus")
Call CheckService(strComputer,"DefWatch")
Call CheckService(strComputer,"ccEvtMgr")
Call CheckService(strComputer,"ccSetMgr")
Call CheckService(strComputer,"SPBBCSvc")
 
 
' Check Process Sub
' *****************
Sub CheckService(strComputer,strService)
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colServiceList = objWMIService.ExecQuery _
        ("Select * from Win32_Service where Name = '" & strService & "'")
 
    If colServiceList.Count = 0 Then
        logFile.WriteLine("Service: " & strService & vbTab & "Status: !~ERROR~! - Service Doesn't Exist!")
        Exit Sub
    End If
    For Each objService in colServiceList
        If objService.State = "Running" Then
            logFile.WriteLine("Service: " & strService & vbTab & "Status: OK - " & objService.State)
        Else
            logFile.WriteLine("Service: " & strService & vbTab & "Status: !~ERROR~! - " & objService.State)
        End If
    Next
End Sub

Open in new window

Avatar of Hubasan
Hubasan
Flag of United States of America image

Hi Wayne-,

Here is your modified script:

Just change sInputFile variable to point to a text file with computer names you have. Script assumes that each line contains a computer name you want to check.

Good Luck :-)
'define a constant
Const ForReading = 1
 
'Create FileSystemObject
Set filesys = CreateObject("Scripting.FileSystemObject")
 
'define input file and open it for reading
sInputFile = "C:\inputfile.txt"
Set oInputFile = filesys.OpenTextFile(sInputFile,ForReading)
 
' Get Script Path and crate a log file
scriptPath = Left(WScript.ScriptFullName,InstrRev(WScript.ScriptFullName,".") -1)
logName = scriptPath & "_" & "log.txt"
Set logFile = filesys.CreateTextFile(logName, True)
 
Do Until oInputFile.AtEndOfStream
	strComputer = Empty
	strComputer = oInputFile.ReadLine
 
	' Create Log File
	'****************
	logFile.WriteLine Now() & vbCrLf & strComputer & vbCrLf & "********************"
		 
	' Check SAV processes
	' *******************
	Call CheckService(strComputer,"Symantec AntiVirus")
	Call CheckService(strComputer,"DefWatch")
	Call CheckService(strComputer,"ccEvtMgr")
	Call CheckService(strComputer,"ccSetMgr")
	Call CheckService(strComputer,"SPBBCSvc")
	logfile.WriteLine "********************"
	logfile.WriteBlankLines(1)
 
Loop
 
' Check Process Sub
' *****************
Sub CheckService(strComputer,strService)
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colServiceList = objWMIService.ExecQuery _
        ("Select * from Win32_Service where Name = '" & strService & "'")
 
    If colServiceList.Count = 0 Then
        logFile.WriteLine("Service: " & strService & vbTab & "Status: !~ERROR~! - Service Doesn't Exist!")
        Exit Sub
    End If
    For Each objService in colServiceList
        If objService.State = "Running" Then
            logFile.WriteLine("Service: " & strService & vbTab & "Status: OK - " & objService.State)
        Else
            logFile.WriteLine("Service: " & strService & vbTab & "Status: !~ERROR~! - " & objService.State)
        End If
    Next
End Sub

Open in new window

Avatar of Wayne-
Wayne-

ASKER

I have create the inputfile.txt file and place the pc's in that txt file but I don't get an output file stated if the service is running on not
Avatar of Wayne-

ASKER

I would also like to place the computer name in the output text file to see which computer is running symantec
Hi Wayne-

I have just tested this and it works on my end and it does log a computer name in the log file.
Your input file has to be like this:

COMPUTER1
COMPUTER2
COMPUTER3

Each computer name is on the next line.

Also do you have access rights to those PC's that you are trying to connect to and query their service?
Avatar of Wayne-

ASKER

It does work thanks but it does seem to error out if the computer is not on, should it do that if not is there a way to keep the script going even if the computer is not on so I can keep query the whole network

Sure, I have to make a small modification, but gotta get some sleep now. I'll have this modified for you tomorrow.
Avatar of Wayne-

ASKER

Is there a way to put in the script if the computer has not responed in so many sec go to the next PC
Avatar of Wayne-

ASKER

Ok thanks
Avatar of Wayne-

ASKER

I have attached the error the script gets when is can not find a computer. When that happens I need for it to keep going and not lock the script up
ErrorVBscript.doc
ASKER CERTIFIED SOLUTION
Avatar of Hubasan
Hubasan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wayne-

ASKER

Hubasan I'm getting a new error message when I'm trying to run script
Script: c:\Symantec_Script.vbs
Line: 110
Char: 6
Error: Syntax error
code: 800A03eEA
Source: Microsoft  VbScript compilation error

Not sure if I'm doing somthing wrong
Wayne-,

My script above has 108 lines, so this means you modified something? What did you add to the bottom of the script that causes this error?
Avatar of Wayne-

ASKER

I just copy the script from the page
Avatar of Wayne-

ASKER

I see there is some text below that i copy by mistake let me try it again
Avatar of Wayne-

ASKER

It seems to be working great thank one more question how hard is it to add other Services to the script if I need it to check then
That should not be a problem at all...These lines here:

    Call CheckService(strComputer,"Symantec AntiVirus")
    Call CheckService(strComputer,"DefWatch")
    Call CheckService(strComputer,"ccEvtMgr")
    Call CheckService(strComputer,"ccSetMgr")
    Call CheckService(strComputer,"SPBBCSvc")

They call the SUB that checks for the serice name specified.

All you would have to do is add additional line like this:

Call CheckService(strComputer,"ServiceToCheckHere")

Thanks and good luck.
Great script but I am having a small problem running it in our environment.  I apparently have a bunch of systems with DCOM disabled (I have fixed a few so I know this is the problem) so when I run it and it gets to a system with the problem the scripts stops and returns the error:  

C:\...\svc_discovery.vbs(61, 3) Microsoft VBScript runtime error: Permission denied: 'GetObject'

If I put in error handling in the Sub CheckService section then it completes successfully, but it just puts the message 'Status: !~ERROR~! - Service Doesn't Exist!' on those systems with the DCOM error.  I really want to know which systems I need to fix.  Does anyone have a suggestion on how I can modify the script so that if it gets the GetObject error it will write a line that reads something like: 'Status: !~ERROR~! - DCOM not working.  Please check system.'?

Thanks!