Monday, April 4, 2011

VB Script to get IP and MAC from remote computer

The following script gets the IP and MAC address form computers in C:\temp\computers.txt and writes them to files in C:\temp\desktop

'Gathers the IP address and MAC from
'Computers in c:\temp\computers.txt
'Outputs to c:\temp\desktop

Option Explicit
Dim objDictionary, objFSO, objTextFile, strRemoteComputer
Dim strNextLine, arrServiceList, i, strCantConnect, strLogFileLocation
Dim objFile, objFolder, objTextFile1, n, objWMIService, colAdapters, objAdapter

' Read a Text File into an Array
Const ForAppending = 8
Const ForReading = 1
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:\temp\computers.txt", ForReading)

Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    'Wscript.Echo "Server name: " & arrServiceList(0)
    For i = 1 to Ubound(arrServiceList)
        Wscript.Echo "Service: " & arrServiceList(i)
    Next

'Get Computer name via input
'strComputer =Inputbox("Get MAC Address and write to c:\Temp" & chr(10) & chr(13) & chr(10) & chr(13) & "Enter PC Number","Input Required")

 ' Verify connection to the Desktop
 strCantConnect = "0"
 Set objFSO = CreateObject("Scripting.FileSystemObject")
  If objFSO.FolderExists("\\"& arrServiceList(0) & "\Admin$") Then
      Set objFolder = objFSO.GetFolder("\\" & arrServiceList(0) & "\Admin$")
  Else
      WScript.Echo "Can't Connect to " & arrServiceList(0) & chr(10) & chr(13) & "Please verify it is contactable via DNS and you have Admin rights"
      strCantConnect = "1"
  End If
' Create the File System Object
strLogFileLocation = "C:\Temp\Desktop\" & arrServiceList(0) & ".txt"
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(strLogFileLocation) Then
 'objFSO.DeleteFile(strLogFileLocation)
 'Set objFile = objFSO.CreateTextFile(strLogFileLocation)
Else
   Set objFile = objFSO.CreateTextFile(strLogFileLocation)
End If
set objFile = nothing
set objFolder = nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
If strCantConnect = "0" Then
Set objTextFile1 = objFSO.OpenTextFile _
(strLogFileLocation, ForAppending, True)
objTextFile1.WriteLine()
objTextFile1.WriteLine("#################################################################################")
End if

'Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objFile = objFSO.CreateTextFile(strLogFileLocation)

' List IP Configuration Data
strRemoteComputer = arrServiceList(0)
If strCantConnect = "0" Then
 Set objWMIService = GetObject("winmgmts:" _
     & "{impersonationLevel=impersonate}!\\" & strRemoteComputer & "\root\cimv2")

 Set colAdapters = objWMIService.ExecQuery _
     ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 n = 1
 End If
   
 For Each objAdapter in colAdapters
If strCantConnect = "0"  Then
 '   WScript.Echo "Network Adapter " & n
 objTextFile1.WriteLine("=================")
 objTextFile1.WriteLine("Host name: " & objAdapter.DNSHostName)
 objTextFile1.WriteLine("Description: " & objAdapter.Description)
 objTextFile1.WriteLine("Physical (MAC) address: " & objAdapter.MACAddress)

  Else
  End If 
    If Not IsNull(objAdapter.IPAddress) Then
       For i = 0 To UBound(objAdapter.IPAddress)
  If strCantConnect = "0" Then
  objTextFile1.WriteLine("IP address:             " & objAdapter.IPAddress(i))
  objTextFile1.WriteLine()
  End if
       Next
    End If

    n = n + 1
  
Next
objTextFile1.Close
If strCantConnect = "0" Then
'WScript.Echo "Please review " & strLogFileLocation & " for MAC Address"
Else
End If
'

Loop
WScript.Echo "Script Finished! Please Review c:\Temp\Desktop for results"

No comments:

Post a Comment