Using VBScript to get correct MAC Address from computer name

I have a function that gets MAC Address from computer name that runs on Windows XP Embedded but apparently it does not get the correct MAC Address every time. What is wrong with my function. Please help!
Function GetMACAddress(strComputer)
	Dim objWMIService, objItem, colItems, mac_addr

	On Error Resume Next

	'' WMI Connection to the object in the CIM namespace
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

	'' WMI Query to the Win32_OperatingSystem
	Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")

	For Each objItem in colItems
	If objItem.macaddress <> "" Then  
		mac_addr= Replace(objItem.macaddress,":","")  
	End If  
	Set objWMIService = Nothing
End Function

Open in new window

LVL 35
Who is Participating?
khashayar01Connect With a Mentor Commented:
Change the following:

Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

instead of 15,16,17,18,19:
Wscript.Echo objItem.MACAddress

unless you want to change the format of the output
David LeeCommented:
Hi, YZlat.

What do you consider to be "the right MAC address"?  There's likely more than one and your function is only returning one.  My first thought is that it doesn't return the same one every time because there's more than one and it occasionally gets them in different sequences.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.