• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

vb code to query MAC address

Is it possible to find the MAC address of a computer by running a VB script
0
cdjohn31
Asked:
cdjohn31
  • 2
2 Solutions
 
PeteEngineerCommented:
Could you try this ?
Dim fso
Dim tst
Dim strMachineName
 
Set fso = createObject("Scripting.FileSystemObject")
Set tst = fso.OpenTextFile("c:\listOfMachineNames.txt", 1, false)
 
While Not tst.AtEndOfStream
	strMachineName = tst.readLine
	
	echoMAC strMachineName
Wend
 
Sub echoMAC(strComputer)
 
	On error resume next
	' strComputer = (InputBox(" Computer name for MAC address", "Computer Name"))
	If strComputer <> "" Then
		strInput = True
	End if
 
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colItems = objWMIService.ExecQuery _
		("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
 
	For Each objItem in colItems
		Wscript.Echo objItem.MACAddress
	Next
End Sub

Open in new window

0
 
K4UCommented:
Try
            Dim nic As NetworkInterface = Nothing
            Dim mac_Address As String = ""

            For Each nic In NetworkInterface.GetAllNetworkInterfaces

                mac_Address = nic.GetPhysicalAddress().ToString
                If mac_Address <> "" Then
                    txtMac_Address.Text = mac_Address
                End If
            Next
               nic = Nothing
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
0
 
Joseph DalyCommented:
This will query WMI for the mac addresses on the machine. If the computer has multiple network cards or adapters it will return all of the mac addresses.

Save the code as a vbs.
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_NetworkAdapter",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_NetworkAdapter instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "MACAddress: " & objItem.MACAddress
Next

Open in new window

0
 
Joseph DalyCommented:
And if you want to run this remotely against a machine this code will do the trick for you.
strComputer = "FullComputerName" 
strDomain = "DOMAIN" 
Wscript.StdOut.Write "Please enter your user name:"
strUser = Wscript.StdIn.ReadLine 
Set objPassword = CreateObject("ScriptPW.Password")
Wscript.StdOut.Write "Please enter your password:"
strPassword = objPassword.GetPassword()
Wscript.Echo
 
Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator") 
Set objWMIService = objSWbemLocator.ConnectServer(strComputer, _ 
    "root\CIMV2", _ 
    strUser, _ 
    strPassword, _ 
    "MS_409", _ 
    "ntlmdomain:" + strDomain) 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_NetworkAdapter",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_NetworkAdapter instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "MACAddress: " & objItem.MACAddress
Next

Open in new window

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

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now