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

Wayne-Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HubasanCommented:
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

0
Wayne-Author Commented:
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
0
Wayne-Author Commented:
I would also like to place the computer name in the output text file to see which computer is running symantec
0
Get Certified for a Job in Cybersecurity

Want an exciting career in an emerging field? Earn your MS in Cybersecurity and get certified in ethical hacking or computer forensic investigation. WGU’s MSCSIA degree program was designed to meet the most recent U.S. Department of Homeland Security (DHS) and NSA guidelines.  

HubasanCommented:
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?
0
Wayne-Author Commented:
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

0
HubasanCommented:
Sure, I have to make a small modification, but gotta get some sleep now. I'll have this modified for you tomorrow.
0
Wayne-Author Commented:
Is there a way to put in the script if the computer has not responed in so many sec go to the next PC
0
Wayne-Author Commented:
Ok thanks
0
Wayne-Author Commented:
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
0
HubasanCommented:
Here you go. This script will now log the PC's that are not online and PC's that doesn't exist (due to misspelling of the computer name..) and go to the next PC.
'define a constant
Const ForReading = 1
 
'Create FileSystemObject
Set filesys = CreateObject("Scripting.FileSystemObject")
 
'define input file and open it for reading
sInputFile = "C:\Support\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        
  sIsOnline = Ping(strComputer)
 
	If IsNull(sIsOnline) Then 
		logFile.WriteLine Now() & vbCrLf & strComputer & vbCrLf & "********************"
		logFile.WriteLine "Computer doesn't exist in current domain"
  	logfile.WriteLine "********************"
  	logfile.WriteBlankLines(1)					
	ElseIf sIsOnline = 11010 Then
		logFile.WriteLine Now() & vbCrLf & strComputer & vbCrLf & "********************"
		logFile.WriteLine "Computer OFF-Line"
  	logfile.WriteLine "********************"
  	logfile.WriteBlankLines(1)
	ElseIf sIsOnline = 0 Then
 
    ' 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)
   End If 
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
 
Function Ping(strComputer)
Set oWMI = GetObject(_ 
    "winmgmts:\\.\root\cimv2")
Set colPings = oWMI.ExecQuery _
    ("Select * From Win32_PingStatus where Address = '" & strComputer & "'")
 
For Each oStatus in colPings
	Ping = oStatus.StatusCode
Next
 
'StatusCode
'    Data type: uint32
'    Access type: Read-only
 
'    Ping command status codes.
'    Value 	Meaning
 
'    0    Success
'    Null			Could not find host
'    11001    Buffer Too Small
'    11002    Destination Net Unreachable
'    11003    Destination Host Unreachable
'    11004    Destination Protocol Unreachable
'    11005    Destination Port Unreachable
'    11006    No Resources
'    11007    Bad Option
'    11008    Hardware Error
'    11009    Packet Too Big
'    11010    Request Timed Out
'    11011    Bad Request
'    11012    Bad Route
'    11013    TimeToLive Expired Transit
'    11014    TimeToLive Expired Reassembly
'    11015    Parameter Problem
'    11016    Source Quench
'    11017    Option Too Big
'    11018    Bad Destination
'    11032    Negotiating IPSEC
'    11050    General Failure
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wayne-Author Commented:
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
0
HubasanCommented:
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?
0
Wayne-Author Commented:
I just copy the script from the page
0
Wayne-Author Commented:
I see there is some text below that i copy by mistake let me try it again
0
Wayne-Author Commented:
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
0
HubasanCommented:
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.
0
XTOSysOpsCommented:
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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Network Security

From novice to tech pro — start learning today.