• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 529
  • 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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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