Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MAC addresse with VBSCRIPT

Status
Not open for further replies.

pjuk21

Technical User
Feb 10, 2008
1
0
0
GB
Help! What I have have been assigned to do is collect mac addresses from over 300 NAS servers on our company network, now I know I can use the below script to get a MAC of a local machne but I can't get it to work reading a text file with all the server names and output to an excel file with the hostname and mac address.

Hope some one can help, sorry if this is in the wrong area.
Paul

Sub PrintMacAddress


Dim strComputer
strComputer = "."


Dim objWMIService
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")


Dim colAdaptors
Set colAdapters = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")


Dim n
n = 1


For Each objAdapter In colAdapters
Rhino.Print ""
Rhino.Print "Network Adapter " & n
Rhino.Print " Description: " & objAdapter.Description
Rhino.Print " Physical (MAC) address: " & objAdapter.MACAddress
n = n + 1
Next


End Sub
 

How about something like this


'Written by David
'18 JAN 2008
'
' Inputs text file with workstation/Server/Laptop names one per line, File Name defaults to ProgramName.txt
'
' Outputs text csv file, file name defaults to ProgramName.csv
'
'

Option Explicit

Dim strFolder,strCommand,strProgramName
Dim objshell, objfso
Dim strInputFile, strinput, strComputerName
Dim strOutputFile, strOutput

Redim arrOS(0)

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

'initialize objects

Set objShell = wscript.createObject("wscript.shell")
Set objFSO = createObject("scripting.filesystemobject")

Call ScriptPath(strFolder,strCommand,strProgramName)


'********************************************************************************************
' Main Program loop

'test output
'wscript.echo "Called from folder : -> " & strFolder & vbCrlf & "Calling Program Line : -> " & strCommand & vbCrlf & "Calling Program Name : -> " & strProgramName & vbCrlf

'open read file
strInput = inputbox("Please enter input file name", "Input File", strfolder & strProgramName & ".txt")
If strInput = "" Then
wscript.quit
End If
Set strInputFile = objfso.opentextfile(strInput, ForReading, True)

'open output file
strOutput = inputbox("Please enter Output file name", "Output File", strfolder & strProgramName & ".csv")
If strOutput = "" Then
wscript.quit
End If

Set strOutputFile = objfso.opentextfile(strOutput, ForWriting, True)

'write csv header
strOutputFile.write ("Host Name" & ",")
strOutputFile.write ("IP Address" & ",")
strOutputFile.write ("MAC Address" & ",")
strOutputFile.write ("Operating System" & ",")
strOutputFile.write ("Service Pack" & ",")
strOutputFile.write ("Registered User" & ",")
strOutputFile.writeline()
strOutputFile.close

'loop through input file

Set strOutputFile = objfso.opentextfile(strOutput, ForAppending, True)

Do while strInputFile.atendofstream <> True
strComputerName = trim(strInputFile.readline)
If IsOnline(strComputerName) Then
' wscript.echo "computer name " & strComputerName
strOutputFile.write (strComputerName & ",")
strOutputFile.write (getipaddress(strComputerName) & ",")
strOutputFile.write (getMACaddress(strComputerName) & ",")
getOS(strComputerName)
strOutputFile.write (arrOS(0) & ",")
strOutputFile.write (arrOS(1) & ",")
strOutputFile.write (arrOS(2) & ",")
strOutputFile.writeline()
End If

Loop


strOutputFile.close

strInputFile.close

'close object
Set objfso = Nothing
Set objshell = Nothing

wscript.echo "Prgram Complete " & vbCrlf & "output file is located at" & vbCrlf & strfolder & strProgramName & ".csv"
wscript.quit
'
'end of program Functions and procedures below
'********************************************************************************************


'********************************************************************************************
'This function returns the path from which the script was called
'Call ScriptPath(strFolder,strCommand,strProgramName)
'wscript.echo "Called from folder : -> " & strFolder & vbCrlf & "Calling Program Line : -> " & strCommand & vbCrlf _
'& "Calling Program Name : -> " & strProgramName & vbCrlf
'wscript.quit

Function ScriptPath(callingfolder, callingcommandline, callingprogramname)
ScriptPath=Left(Wscript.scriptfullname,Instr(1,WScript.ScriptFullName,wscript.scriptname,1)-1)
callingfolder=scriptpath
callingcommandline=wscript.scriptfullname
callingprogramname=left(wscript.scriptname,len(wscript.scriptname)-4)
End Function
'********************************************************************************************

'********************************************************************************************
' Function returns true if lstrcomputer is online, does ping test for reply in console message
Function IsOnline(lstrComputer)
Dim objExecObject, strText
Set objShell = CreateObject("WScript.Shell"):
Set objExecObject = objShell.Exec ("%comspec% /c ping -n 1 -w 25 " & lstrComputer)
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadAll()
If Instr(strText, "Reply") > 0 Then
IsOnline=True
Else
IsOnline=False
End If
Loop
End Function
'********************************************************************************************

'********************************************************************************************
' Function returns IP address of strcomputer
Function getipaddress(strComputer)
Dim objExecObject, strText, searchstring1,searchstring2, position1, position2
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec ("%comspec% /c ping " & strComputer)
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadAll()
searchstring1 = "Reply from "
searchstring2 = ":"
position1=Instr(1, strText, searchstring1, 0)+11
position2=Instr(position1, strText, searchstring2, 0)
getipaddress = mid(strtext,position1,position2-position1)
Loop
End Function
'********************************************************************************************


'********************************************************************************************
' Function returns MAC address of strcomputer
Function getMACaddress(strComputer)
Dim objExecObject, strText, searchstring1,searchstring2, position1, position2
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec ("%comspec% /c nbtstat -a " & strComputer)
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadAll()
searchstring1 = "MAC Address ="
position1=Instr(1, strText, searchstring1, 0)+13
getMACaddress = mid(strtext,position1,18)
Loop
End Function
'********************************************************************************************


'********************************************************************************************
' Function getOS returns OS, User, Build Version address of strcomputer
Function getOS(strComputerName)
On Error Resume Next
Dim objWMI, colOS, strItem, strObject, strSearchString, replacedstring, re
Redim arrOS(3)

'store first part of WMI services
strObject = "winmgmts:{impersonationlevel=impersonate}!\\"

'initialize objects
Set objWMI = getobject(strobject & strcomputername & "\root\cimv2")
Set colOS = objWMI.execquery("select * from win32_operatingSystem")

'store values in array
For Each stritem In colOS
' take out the coma from description
strSearchString = stritem.caption
' wscript.echo "strSearchString ->" & strSearchString
replacedstring = replace(strSearchString,","," ")
' wscript.echo "replaced strSearchString ->" & replacedstring
arrOS(0) = replacedstring
' wscript.echo "Array 0 arrOS(0) ->" & arrOS(0)
arrOS(1) = stritem.CSDVersion
arrOS(2) = stritem.RegisteredUser
Next

'close object
Set objWMI = Nothing
Set colOS = Nothing

End Function
'********************************************************************************************

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top