New to vbs, loop through servers check local admin account

Hi guys, i do my scripting mostly in powershell, looking to do this project in vbs since a lot of servers don't allow remoting. I have a file with host names, i want to read each server, ping, if pingable, use local admin credentials, if successful write to one log file, if failed write to another log file. This is what i have so far:

Dim fso, file, fileLine
 
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\Users\mkond\Desktop\hosts.txt")
Set WshShell = CreateObject("WScript.Shell")
strUser = "mmhs\Administrator"
strPass = "pass"
do while not file.AtEndOfStream
    strServer =  file.ReadLine()
    PINGFlag = Not CBool(WshShell.run("ping -n 1 " & strServer,0,True))
      	If PINGFlag = True Then
  	WScript.Echo strServer & " responded to ping."
  	Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
	Set objSWbemServices = objSWbemLocator.ConnectServer _
    (strServer, "root\cimv2", strUser, strPass)
	objSWbemServices.Security_.ImpersonationLevel = 3
		Else
  	WScript.Echo strServer & " did not respond to ping."
		End If

Loop

any help appreciated


  • Edited by ImMax Wednesday, August 19, 2015 4:02 PM
August 19th, 2015 4:02pm

May i ask why its not creating my log files and writing to them with below code?

On Error Resume Next 

Dim fso, file, fileLine

Set fso = CreateObject("Scripting.FileSystemObject")
Set myErrorLog = objFSO.OpenTextFile("C:\Users\mkond\Desktop\testcreds\Error.log", ForAppending, True)
Set myGoodLog = objFSO.OpenTextFile("C:\Users\mkond\Desktop\testcreds\Good.log", ForAppending, True)
Set myPingLog = objFSO.OpenTextFile("C:\Users\mkond\Desktop\testcreds\Ping.log", ForAppending, True)
Set file = fso.OpenTextFile("C:\Users\mkond\Desktop\testcreds\hosts.txt")
Set WshShell = CreateObject("WScript.Shell")
strUser = "domain\mkond"
strPass = "pass"
do while not file.AtEndOfStream
    strServer =  file.ReadLine()
    PINGFlag = Not CBool(WshShell.run("ping -n 1 " & strServer,0,True))
      	If PINGFlag = True Then
  	WScript.Echo strServer & " responded to ping."
  	Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
	Set objSWbemServices = objSWbemLocator.ConnectServer _
    (strServer, "root\cimv2", strUser, strPass)
	objSWbemServices.Security_.ImpersonationLevel = 3
  	myGoodLog.WriteLine strServer
		Else
  	WScript.Echo strServer & " did not respond to ping."
  	myPingLog.LogError "Server" &strServer & " did not respond to ping."
		End If

Loop


  • Edited by ImMax Wednesday, August 19, 2015 7:58 PM
Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 5:02pm

this is what i ended up with

Dim fso, file, fileLine
On Error Resume Next

Set objfso = CreateObject("Scripting.FileSystemObject")
Set myErrorLog = objfso.OpenTextFile("C:\Users\mkond\Desktop\testcreds\Error.log", 8, True)
Set myGoodLog = objfso.OpenTextFile("C:\Users\mkond\Desktop\testcreds\Good.log", 8, True)
Set myPingLog = objfso.OpenTextFile("C:\Users\mkond\Desktop\testcreds\DidntPing.log", 8, True)
Set file = objfso.OpenTextFile("C:\Users\mkond\Desktop\testcreds\hosts.txt")
Set WshShell = CreateObject("WScript.Shell")
strUser = "\administrator"
strPass = "pass"
do while not file.AtEndOfStream
    strServer =  file.ReadLine()
    If Ping(strServer) Then
  	WScript.Echo strServer & " responded to ping."
  	Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")
	Set objSWbemServices = objSWbemLocator.ConnectServer _
    	(strServer, "root\cimv2",strServer & strUser, strPass)
	objSWbemServices.Security_.ImpersonationLevel = 3
	If Err.Number = 424 Then
	myErrorLog.WriteLine strServer
	WScript.Echo strServer & " Access Denied"
	Err.Clear
	Else
	myGoodLog.WriteLine strServer
	End If
	Else
  	WScript.Echo strServer & " did not respond to ping."
  	myPingLog.WriteLine strServer
		End If

Loop
myPingLog.Close
myGoodLog.Close
myErrorLog.Close

Function Ping( myHostName )
    ' Standard housekeeping
    Dim colPingResults, objPingResult, strQuery
    ' Define the WMI query
    strQuery = "SELECT * FROM Win32_PingStatus WHERE Address = '" & myHostName & "'"
    ' Run the WMI query
    Set colPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery( strQuery )
    ' Translate the query results to either True or False
    For Each objPingResult In colPingResults
        If Not IsObject( objPingResult ) Then
            Ping = False
        ElseIf objPingResult.StatusCode = 0 Then
            Ping = True
        Else
            Ping = False
        End If
    Next

    Set colPingResults = Nothing
End Function

August 20th, 2015 8:06am

The first thing you should do is remove the line On Error Resume Next from your script.

Exercise: What does that line of code do, and why is it bad to insert it in a script without understanding exactly what it does?

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 10:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics