Announcement

Collapse
No announcement yet.

Remote WINS setting output to file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Remote WINS setting output to file

    I want to run a script that takes computer names listed in a file, checks their wins settings, and outputs to a text file. I pieced together two different scripts I found in the TechNet and crossed my fingers. I'm really bad at scripting and my output file is crazy. I looks like it appends to the previous output instead of making a new entry. How can I get the output to create a new entry instead of concatenating all previous entries?

    I have attached both the input and output text files.

    Thanks,

    Andy

    Script:
    ____________________________________________
    On Error Resume Next

    Const FOR_READING = 1
    strFilename = "c:\ec.txt"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(strFilename) Then
    Set objFile = objFSO.OpenTextFile(strFilename, FOR_READING)
    Else
    WScript.Echo "Input file " & strFilename & " not found."
    WScript.Quit
    End If

    Do Until objFile.AtEndOfStream

    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colNicConfigs = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

    strComputer = objFile.ReadLine

    For Each objNicConfig In colNicConfigs
    strDNSHostName = objNicConfig.DNSHostName
    intNetBIOS = objNicConfig.TcpipNetbiosOptions
    Select Case intNetBIOS
    Case 0 strNetBIOS = "Use NetBIOS setting from the DHCP server."
    Case 1 strNetBIOS = "Enable NetBIOS over TCP/IP."
    Case 2 strNetBIOS = "Disable NetBIOS over TCP/IP."
    Case Else strNetBIOS = "Unable to determine setting for NetBIOS " & _
    "Over TCP/IP."
    End Select
    strWINSSettings = strWINSSettings & VbCrLf & VbCrLf & _
    " Network Adapter " & objNicConfig.Index & VbCrLf & _
    " " & objNicConfig.Description & VbCrLf & VbCrLf & _
    " NetBIOS: " & strNetBIOS & VbCrLf & _
    " WINS Primary Server: " & _
    objNicConfig.WINSPrimaryServer & VbCrLf & _
    " WINS Secondary Server: " & _
    objNicConfig.WINSSecondaryServer & VbCrLf & _
    " WINS Scope ID: " & _
    objNicConfig.WINSScopeID & VbCrLf & _
    " WINS Enable LMHosts Lookup: " & _
    objNicConfig.WINSEnableLMHostsLookup & VbCrLf & _
    " WINS Host Lookup File: " & _
    objNicConfig.WINSHostLookupFile & VbCrLf & _
    " DNS Enabled For WINS Resolution: " & _
    objNicConfig.DNSEnabledForWINSResolution
    Next

    WScript.Echo VbCrLf & "WINS Settings" & VbCrLf & VbCrLf & _
    "Host Name: " & strDNSHostName & strWINSSettings

    Loop
    objFile.Close
    ___________________________________________
    Attached Files

  • #2
    Re: Remote WINS setting output to file

    The line: strComputer = objFile.ReadLine
    must be before the statement: Set objWMIService = ... (because here the variable strComputer will be used to connect to the WMI service on that computer)

    In the For_Each-loop the variable strWINSSettings is filled with the previous strWINSSettings & the new information (strWINSSettings = strWINSSettings & ... ). This variable must be emptied before you use it for the next computer (strWINSSettings = Empty) in the Do-loop

    The scipt would be then;
    Code:
    Const FOR_READING = 1
    
    strFilename = "D:\COMPUTERS.TXT"
    
    ' Connect to WMI service on the local computer.
    Set objLocalWMIService = GetObject("winmgmts:" _
             & "{impersonationLevel=impersonate}!\\" _
             & ".\root\cimv2")
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FileExists(strFilename) Then
    Set objFile = objFSO.OpenTextFile(strFilename, FOR_READING)
    Else
    WScript.Echo "Input file " & strFilename & " not found."
    WScript.Quit
    End If
    
    Do Until objFile.AtEndOfStream
    
       strWINSSettings = Empty
       strComputer = objFile.ReadLine
    
       ' Ping remote computer to see if online.
       If (PingMachine(strComputer) = True) Then
    
          Set objWMIService = GetObject("winmgmts:" _
             & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
          Set colNicConfigs = objWMIService.ExecQuery _
             ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
    
          For Each objNicConfig In colNicConfigs
             strDNSHostName = objNicConfig.DNSHostName
             intNetBIOS = objNicConfig.TcpipNetbiosOptions
    
             Select Case intNetBIOS
                Case 0 strNetBIOS = "Use NetBIOS setting from the DHCP server."
                Case 1 strNetBIOS = "Enable NetBIOS over TCP/IP."
                Case 2 strNetBIOS = "Disable NetBIOS over TCP/IP."
                Case Else strNetBIOS = "Unable to determine setting for NetBIOS " & _
                  "Over TCP/IP."
             End Select
    
             strWINSSettings = strWINSSettings & VbCrLf & VbCrLf & _
                " Network Adapter " & objNicConfig.Index & VbCrLf & _
                " " & objNicConfig.Description & VbCrLf & VbCrLf & _
                " NetBIOS: " & strNetBIOS & VbCrLf & _
                " WINS Primary Server: " & _
                objNicConfig.WINSPrimaryServer & VbCrLf & _
                " WINS Secondary Server: " & _
                objNicConfig.WINSSecondaryServer & VbCrLf & _
                " WINS Scope ID: " & _
                objNicConfig.WINSScopeID & VbCrLf & _
                " WINS Enable LMHosts Lookup: " & _
                objNicConfig.WINSEnableLMHostsLookup & VbCrLf & _
                " WINS Host Lookup File: " & _
                objNicConfig.WINSHostLookupFile & VbCrLf & _
                " DNS Enabled For WINS Resolution: " & _
                objNicConfig.DNSEnabledForWINSResolution
          Next
    
          WScript.Echo VbCrLf & "WINS Settings" & VbCrLf & VbCrLf & _
             "Host Name: " & strDNSHostName & strWINSSettings
    
       End If
    Loop
    objFile.Close
    
    WScript.Quit
    
    Function PingMachine(ByVal strHost)
        ' (requires Windows XP or above)
        ' www.rlmueller.net/PingComputers.htm
        ' Returns True if strHost can be pinged.
        
        ' Variable objLocalWMIService has global scope
        ' and must be declared in the main program.
        If Not IsObject(objLocalWMIService) _
              then exit function
    
        Dim colPings, objPing
    
        Set colPings = objLocalWMIService.ExecQuery _
              ("SELECT * FROM Win32_PingStatus " _
               & "WHERE Address = '" & strHost & "'")
        For Each objPing In colPings
            If objPing.StatusCode = 0 Then
                ' Computer responded to ping.
                PingMachine = True
                Exit Function
            Else
                ' No reponse.
                PingMachine = False
            End If
        Next
    
    End Function
    In this script I added a ping test, this test is performed before the script tries to connect to the WMI service on a remote computer. The line On Error Resume next won't be needed any more then. And, performing this ping test against every remote computer on the list is faster than waiting for a time out when some of the computers are not connectable that time.

    A similar Ping-Function for older versions of Windows is available:
    http://www.rlmueller.net/PingComputers.htm

    \Rems
    Last edited by Rems; 8th February 2008, 01:10.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Remote WINS setting output to file

      Brilliant! That is exactly what I was looking for. And more.

      It works like a charm.

      Thanks,

      Andy

      Comment


      • #4
        Re: Remote WINS setting output to file

        Glad I could help.


        I did made a few changes afterwaed in the script for better performance.
        The ping test in the script is for Windows Xp or later, it uses the local WMI service on your computer. This Function however is called every time again before conneting to the next computer. So that gives unnesseary calls to the local WMI service. To Limit the calls to the local WMI service to just once, is must be Set at the beginning of the script, before the loop. But you cannot give this instance the same variable name as the one using for the remote objWMIService.
        So the statement is;

        ' Connect to WMI service on the local computer.
        Set objLocalWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" _
        & ".\root\cimv2")


        I have made these changes in the sample script.

        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment

        Working...
        X