Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

VBScript to use partial computer name to find MAC and IP address - Syntax Error

$
0
0

Hi guys,

I found / wrote / modified a couple VB scripts with the goal in mind of:

  • Take txt list of serial numbers (serial numbers are last part of computer names)
  • Query each to find it's MAC and IP address.
  • Write results to .csv.

I started with a script that worked off the full computer name.  After some tweaking it worked great.

Dim Args, ComputerName, WMI, Configs, Config

Set Args = WScript.Arguments

If Args.Unnamed.length = 0 Then
  WScript.Echo "Please specify a computer name on the command line"
  WScript.Quit
End If

ComputerName = Args.Unnamed.Item(0)

On Error Resume Next
Set WMI = GetObject("winmgmts://" & ComputerName & "/root/cimv2")
If Err.Number <> 0 Then
  WScript.Echo "Error " & Hex(Err.Number) & " connecting to " & ComputerName
  WScript.Quit Err.Number
End If
On Error GoTo 0

Set Configs = WMI.ExecQuery("select IPAddress,MACAddress from " _& "Win32_NetworkAdapterConfiguration where IPEnabled=true")

Function IPToCSV(IPArray)
  Dim Result, N
  Result = ""
  For N = 0 To UBound(IPArray)
    If Result = "" Then
      Result = """" & IPArray(N) & """"
    Else
      Result = Result & ",""" & IPArray(N) & """"
    End If
  Next
  IPToCSV = Result
End Function

For Each Config In Configs
  WScript.Echo """" & ComputerName & """," _& IPToCSV(Config.IPAddress) _& ",""" & Config.MACAddress & """"
Next

Then I found and modified a script into the one above, that would grab the partial name and resolve it with AD to get the full name.  That portion of the script works, but I'm getting a syntax error when it's ran all together.  I've tried move and adding things but I can't get around the error. 

Dim Args, strName, WMI, Configs, Config

Set Args = WScript.Arguments
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

If Args.Unnamed.length = 0 Then
  WScript.Echo "Please specify a computer name on the command line"
  WScript.Quit
End If

strComputer = Args.Unnamed.Item(0)

On Error Resume Next

strFilter = "(&(objectCategory=Computer)(samAccountName=*" & strComputer & "$))"

Set objRootDSE = GetObject("LDAP://RootDSE")

strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

strAttributes = "sAMAccountName"

strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

Set adoRecordset = adoCommand.Execute

Do Until adoRecordset.EOF

strName = adoRecordset.Fields("sAMAccountName").Value

Set WMI = GetObject("winmgmts://" & strName & "/root/cimv2")
If Err.Number <> 0 Then
  WScript.Echo "Error " & Hex(Err.Number) & " connecting to " & strName
  WScript.Quit Err.Number
End If
On Error GoTo 0

Set Configs = WMI.ExecQuery("select IPAddress,MACAddress from " _& "Win32_NetworkAdapterConfiguration where IPEnabled=true")

Function IPToCSV(IPArray)
  Dim Result, N
  Result = ""
  For N = 0 To UBound(IPArray)
    If Result = "" Then
      Result = """" & IPArray(N) & """"
    Else
      Result = Result & ",""" & IPArray(N) & """"
    End If
  Next
  IPToCSV = Result
End Function

For Each Config In Configs
  WScript.Echo """" & strName & """," _& IPToCSV(Config.IPAddress) _& ",""" & Config.MACAddress & """"
Next

adoRecordset.MoveNext
Loop

adoRecordset.Close
adoConnection.Close

Any idea where I went wrong?

Thanks!


There's no place like 127.0.0.1


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>