none
VBScript- find IP and gateway

    Question

  • Hey,

    I am trying to run a VB script but it is not generating any output. My script is as follows:-

    On Error Resume Next 
     
    'open the file system object 
    Set oFSO = CreateObject("Scripting.FileSystemObject"
    set WSHShell = wscript.createObject("wscript.shell"
    'open the data file 
    Set oTextStream = oFSO.OpenTextFile("computers.txt"
    'make an array from the data file 
    RemotePC = Split(oTextStream.ReadAll, vbNewLine) 
    'close the data file 
    oTextStream.Close 
    For Each strComputer In RemotePC 
     
       WScript.Echo 
       WScript.Echo "==========================================" 
       WScript.Echo "Computer: " & strComputer 
       WScript.Echo "==========================================" 
     
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")  
    Set colItems = objWMIService.ExecQuery( _ 
        "SELECT * FROM Win32_NetworkAdapterConfiguration",,48)  
    For Each objItem in colItems  
        'Wscript.Echo "-----------------------------------" 
        'Wscript.Echo "Win32_NetworkAdapterConfiguration instance" 
        'Wscript.Echo "-----------------------------------" 
         If isNull(objItem.DNSHostName) Then 
        '    Wscript.Echo "DNSHostName: " 
         Else 
             Wscript.Echo "DNS Host Name: " & objItem.DNSHostName 
             Wscript.Echo "Description: " & objItem.Description 
        Wscript.Echo "IP Address: " & Join(objItem.IPAddress, ","
             Wscript.Echo "IP Subnet: " & Join(objItem.IPSubnet, ","
        Wscript.Echo "DNS Server Search Order: " & Join(objItem.DNSServerSearchOrder, " , "
        Wscript.Echo "DNS Suffix: " & objItem.DNSDomain 
        Wscript.Echo "WINS Primary Server: " & objItem.WINSPrimaryServer 
        Wscript.Echo "WINS Secondary Server: " & objItem.WINSSecondaryServer 
         End If 
    Next 
    Next

    Can someone let me know where am I going wrong?

     

    Monday, November 08, 2010 12:23 AM

Answers

  • Hi

    See the MSDN documentation for the Win32_NetworkAdapterConfiguration here:

    http://msdn.microsoft.com/en-us/library/aa394217(VS.85).aspx

    So apart from your origonal script being a javascript, and attempting to enumerate the IP configuration of all network devices including ones that may not be in use, the actual reason the gateway is not shown is because the property should be "DefaultIPGateway" not "objItem.Defaultgateway". Change that line to:

    Wscript.Echo "Default Gateway: " & Join(objItem.DefaultIPGateway, ",")

    Cheers Matt :)

    Tuesday, November 09, 2010 8:43 PM
    Moderator

All replies

  • Hi,

    Assuming your file containing the computer names is in the same directory as the script, try using some error checking to display the error.  For example:

    Option Explicit
    
    Dim objFSO, wshShell, textFile
    
    Dim hostNames, hostName, wmi, results, result
    
    On Error Resume Next 
    
      Set objFSO  = CreateObject("Scripting.FileSystemObject") 
    
      Set wshShell = wscript.createObject("wscript.shell") 
    
      Set textFile = objFSO.OpenTextFile("hostnames.txt") 
    
      hostNames  = Split(textFile.ReadAll, vbNewLine) 
    
      textFile.Close
    
      For Each hostName In hostNames
    
       Do
    
         Set wmi   = GetObject("winmgmts:\\" & hostName & "\root\CIMV2") 
    
         Set results = wmi.ExecQuery("Select * From Win32_NetworkAdapterConfiguration",,48)
    
         If Err.Number <> 0 Then
    
          MsgBox hostName & ". Error " & Err.Number & " (Hex " & Hex(Err.Number) & "). " & Err.Description, vbCritical
    
          Exit Do
    
         End If
    
         For Each result in results 
    
          If Not isNull(result.DNSHostName) Then 
    
            MsgBox "DNS Host Name: "      & result.DNSHostName,            vbInformation 
    
            MsgBox "Description: "       & result.Description,            vbInformation 
    
            MsgBox "IP Address: "       & Join(result.IPAddress, ","),       vbInformation 
    
            MsgBox "IP Subnet: "        & Join(result.IPSubnet, ","),        vbInformation
    
            MsgBox "DNS Server Search Order: " & Join(result.DNSServerSearchOrder, " , "), vbInformation
    
            MsgBox "DNS Suffix: "       & result.DNSDomain,             vbInformation
    
            MsgBox "WINS Primary Server: "   & result.WINSPrimaryServer,         vbInformation
    
            MsgBox "WINS Secondary Server: "  & result.WINSSecondaryServer,        vbInformation 
    
          End If 
    
         Next
    
       Loop Until True
    
      Next
    
    On Error Goto 0

    Hope that helps

    Cheers Matt :)

    Monday, November 08, 2010 1:20 AM
    Moderator
  • Hey Matt,

    So, I will surely use your script. But now I am trying to run the same script from wscript instead of cscript, and the error displayed is :-

    Script:says the path

    Line:1

    Char:4

    Error: Expected";"

    Code:800A03EC

    Source:Microsoft Jscript Compilation Error

    Monday, November 08, 2010 1:28 AM
  • That is a peculiar result for a .VBS file.  It is saved with the VBS extension (as the final part after the dot)?  You may need to change the Folder Options/View settings in explorer to stop hiding extensions to be sure.  I say that because the error message indicates the Jscript compiler is processing the file, which surely won't work.
    Tom Lavedas
    Monday, November 08, 2010 1:17 PM
    Moderator
  • @Matt,

    When I try with your script with .js extension, it says the same error but on Line1,char8.

    Script:says the path

    Line:1

    Char:8

    Error: Expected";"

    Code:800A03EC

    Source:Microsoft Jscript Compilation Error

     

    @Tom,

    Just now I changed the Folder Optons in Control Panel, basically I unticked the option:- Hide extensions for known file types. But still get the same error at Line1,char4.

    Monday, November 08, 2010 3:23 PM
  • Hi,

    You didn't specify that you want to use javascript. The solution provided only works in vbscript. I'm assuming your embedding this code in a web application? I think the reason it gives you the error is because the backslash character "\" is an escape symbol in javascript. You need to replace it with double backslashes. I believe the syntax should be:

    wmi = GetObject("winmgmts:\\\\" + hostname + "\\root\\cimv2");

    Note the expected ";" at the end of the code, hence why your script is complaining about the missing character. Also there are security limitations of what information you can enumerate from a remote system via a browser. Assuming your building a web application, you'd be best targeting such questions on a web development forum.

    Matt

     

    Tuesday, November 09, 2010 10:46 AM
    Moderator
  • Hey Matt,

    Your script is absolutely working fine. I am running via .vbs only and now I am able to see  DNS Host Name, NIC, IP subnet,DC, alternate DC but not able to see gateway. Can you correct me where am I going wrong?

    Tuesday, November 09, 2010 3:18 PM
  • Hi

    Sure, your origonal code fails because the "On Error Resume Next" statement catchs the error and moves to the next line so there is nothing to display. The error is that you are attempting to Join an array for an invalid datatype. Because your WMI query asks the system for all network adapter configuration, it's possible that some networking devices installed are not in use and therefore don't have an IP address, therefore when you attempt to use the Join statement to join an array on a null value then you get an error. Here is some slight modification to your origonal code to help you. Note the modification to the WMI query "Where IPEnabled = True"

    On Error Resume Next 
      Set oFSO = CreateObject("Scripting.FileSystemObject") 
      set WSHShell = wscript.createObject("wscript.shell") 
      Set oTextStream = oFSO.OpenTextFile("computers.txt") 
      RemotePC = Split(oTextStream.ReadAll, vbNewLine) 
      oTextStream.Close 
      For Each strComputer In RemotePC
       Do
         WScript.Echo 
         WScript.Echo "==========================================" 
         WScript.Echo "Computer: " & strComputer 
         WScript.Echo "==========================================" 
         Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
    
         'Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration",,48) 
    
         Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True",,48) 
         For Each objItem in colItems
          'Wscript.Echo "-----------------------------------" 
          'Wscript.Echo "Win32_NetworkAdapterConfiguration instance" 
          'Wscript.Echo "-----------------------------------" 
          'If isNull(objItem.DNSHostName) Then 
          '  Wscript.Echo "DNSHostName: " 
          'Else
          If Not isNull(objItem.DNSHostName) Then
            Wscript.Echo "DNS Host Name: " & objItem.DNSHostName 
            Wscript.Echo "Description: " & objItem.Description 
            Wscript.Echo "IP Address: " & Join(objItem.IPAddress, ",") 
            Wscript.Echo "IP Subnet: " & Join(objItem.IPSubnet, ",") 
            Wscript.Echo "DNS Server Search Order: " & Join(objItem.DNSServerSearchOrder, " , ") 
            Wscript.Echo "DNS Suffix: " & objItem.DNSDomain 
            Wscript.Echo "WINS Primary Server: " & objItem.WINSPrimaryServer 
            Wscript.Echo "WINS Secondary Server: " & objItem.WINSSecondaryServer 
          End If 
         Next 
         If Err.Number <> 0 Then
          Exit Do
         End If
       Loop Until True
      Next
    On Error Goto 0
    
    Cheers Matt :)
    Tuesday, November 09, 2010 7:50 PM
    Moderator
  • Hey,

    The script runs fine but what I was asking for is default gateway . Can you tell me how should I include that in my code?

    On Error Resume Next
      Set oFSO = CreateObject("Scripting.FileSystemObject")
      set WSHShell = wscript.createObject("wscript.shell")
      Set oTextStream = oFSO.OpenTextFile("computers.txt")
      RemotePC = Split(oTextStream.ReadAll, vbNewLine)
      oTextStream.Close
      For Each strComputer In RemotePC
       Do
        
         WScript.Echo "Computer: " & strComputer
         Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

         'Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration",,48)

         Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True",,48)
         For Each objItem in colItems
          'Wscript.Echo "-----------------------------------"
          'Wscript.Echo "Win32_NetworkAdapterConfiguration instance"
          'Wscript.Echo "-----------------------------------"
          'If isNull(objItem.DNSHostName) Then
          '  Wscript.Echo "DNSHostName: "
          'Else
          If Not isNull(objItem.DNSHostName) Then
            Wscript.Echo "DNS Host Name: " & objItem.DNSHostName
            Wscript.Echo "Description: " & objItem.Description
            Wscript.Echo "IP Address: " & Join(objItem.IPAddress, ",")
            Wscript.Echo "IP Subnet: " & Join(objItem.IPSubnet, ",")
            Wscript.Echo "Default Gateway: " & Join(objItem.Defaultgateway, ",")
            Wscript.Echo "DNS Server Search Order: " & Join(objItem.DNSServerSearchOrder, " , ")
            Wscript.Echo "DNS Suffix: " & objItem.DNSDomain
            Wscript.Echo "WINS Primary Server: " & objItem.WINSPrimaryServer
            Wscript.Echo "WINS Secondary Server: " & objItem.WINSSecondaryServer
          End If
         Next
         If Err.Number <> 0 Then
          Exit Do
         End If
       Loop Until True
      Next
    On Error Goto 0

    Tuesday, November 09, 2010 8:30 PM
  • Hi

    See the MSDN documentation for the Win32_NetworkAdapterConfiguration here:

    http://msdn.microsoft.com/en-us/library/aa394217(VS.85).aspx

    So apart from your origonal script being a javascript, and attempting to enumerate the IP configuration of all network devices including ones that may not be in use, the actual reason the gateway is not shown is because the property should be "DefaultIPGateway" not "objItem.Defaultgateway". Change that line to:

    Wscript.Echo "Default Gateway: " & Join(objItem.DefaultIPGateway, ",")

    Cheers Matt :)

    Tuesday, November 09, 2010 8:43 PM
    Moderator
  • Earlier, the script was working fine. But now what I get to see is the script shows the output only for one host name. The script looks like this:-



    Option Explicit

    Dim objFSO, wshShell, textFile

    Dim outfile

    Dim hostNames, hostName, wmi, results, result

    On Error Resume Next

      Set objFSO  = CreateObject("Scripting.FileSystemObject")

      Set wshShell = wscript.createObject("wscript.shell")

      Set textFile = objFSO.OpenTextFile("Computers.txt")

      hostNames  = Split(textFile.ReadAll, vbNewLine)

      textFile.Close

      set outfile = objFSO.OpenTextFile("output.txt",2,true)

      For Each hostName In hostNames

       Do

         Set wmi   = GetObject("winmgmts:\\" & hostName & "\root\CIMV2")

         Set results = wmi.ExecQuery("Select * From Win32_NetworkAdapterConfiguration",,48)

         If Err.Number <> 0 Then

          MsgBox hostName & ". Error " & Err.Number & " (Hex " & Hex(Err.Number) & "). " & Err.Description, vbCritical

          Exit Do

         End If

         For Each result in results

          If Not isNull(result.DNSHostName) Then

            outfile.writeline "DNS Host Name: "& result.DNSHostName

            outfile.writeline "Description: "  & result.Description
             
            outfile.writeline "IP Address: "   & Join(result.IPAddress, ",")

            outfile.writeline "Default Gateway: "   & Join(result.DefaultIPgateway, ",")

            outfile.writeline "IP Subnet: "    & Join(result.IPSubnet, ",")

            outfile.writeline "DNS Server Search Order: " & Join(result.DNSServerSearchOrder, " , ")

          
          End If

         Next

       Loop Until True

      Next

    On Error Goto 0

    Wednesday, November 10, 2010 1:39 AM
  • Hi ,

    I want IP address and subnet details of multiple computers in an excel or text file.

    Can someone provide me script for that.

    Friday, April 27, 2012 10:07 PM
  • Hi,

    Did you search before asking? This question is already marked answered. If you need help, please start a new question, and please tell what you have already tried, what isn't working (exact error messages, etc.).

    Bill

    Friday, April 27, 2012 10:09 PM
    Moderator
  • Hi

    I need output in below format or as in excel report.

    DNS Host Name Description  IP Address  Default Gateway   IP Subnet  DNS Server Search Order

    • Proposed as answer by Henryenriquez Friday, March 01, 2013 1:15 PM
    Friday, April 27, 2012 10:17 PM
  • Hi

    I need output in below format or as in excel report.

    DNS Host Name Description  IP Address  Default Gateway   IP Subnet  DNS Server Search Order

    Please start a new topic.  This topic has been answered and closed for two years.


    ¯\_(ツ)_/¯

    Friday, April 27, 2012 10:37 PM