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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.