none
New to vbs, loop through servers check local admin account RRS feed

  • Question

  • 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
    Wednesday, August 19, 2015 4:02 PM

Answers

  • What is this? "ForAppending"

    It appears to be nothing.


    \_(ツ)_/

    • Marked as answer by ImMax Thursday, August 20, 2015 12:03 PM
    Wednesday, August 19, 2015 8:01 PM

All replies

  • What is the question?


    \_(ツ)_/

    Wednesday, August 19, 2015 4:21 PM
  • one is how do i know if remote was successful then other piece is can i make it a bit faster? the remoting can take 2 seconds or so.
    Wednesday, August 19, 2015 4:34 PM
  • If there is an error it is not successful.

    You cannot make it faster.  Fix network.  Fix servers.  New hardware.  Faster processor.

    You are running a very bad ping.  Use Win32_PinStatus as it is much faster.


    \_(ツ)_/

    Wednesday, August 19, 2015 4:41 PM
  • 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
    Wednesday, August 19, 2015 5:02 PM
  • You changed the script and the question.

    Remove this line: On Error Resume Next


    \_(ツ)_/

    Wednesday, August 19, 2015 5:21 PM
  • I am getting an invalid procedure call or procedure in these lines

    Set objfso = 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)

    createfile works but i am under impression since i set these to true, it would create them if files dont exist already

    Wednesday, August 19, 2015 7:55 PM
  • What is this? "ForAppending"

    It appears to be nothing.


    \_(ツ)_/

    • Marked as answer by ImMax Thursday, August 20, 2015 12:03 PM
    Wednesday, August 19, 2015 8:01 PM
  • oh i see i need to use value 8, thanks

    back to the catching servers that return access denied when i try to remote to them, how can i check that it failed - i am thinking based on the error message

    Wednesday, August 19, 2015 8:10 PM
  • Wednesday, August 19, 2015 8:12 PM
  • 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

    Thursday, August 20, 2015 12:02 PM
  • 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?


    -- Bill Stewart [Bill_Stewart]

    Thursday, August 20, 2015 2:11 PM
    Moderator