Solved

Collect Ethernet Adapter Wireless Network MAC address

Posted on 2010-09-02
10
817 Views
Last Modified: 2012-05-10
I need help to collect Ethernet Adapter Wireless Network MAC address for a list of laptops. I will have an input file with all the Service Tag of the laptops in there. I need a VB Script that will read through the input file and export the following to a text or excel file

Service Tag & Ethernet Adapter Wireless Network MAC address

Eg  The output file will look like
23N7V1S    00-1F-3B-01-49-BD
CZNS22S   00-22-FB-CD-4A-18

Thanks
Cheers



0
Comment
Question by:Bianchi928
  • 5
  • 3
  • 2
10 Comments
 
LVL 8

Expert Comment

by:spinzr0
ID: 33593059
If you have admin access to all of the PCs then you can use WMI as in the example below.
On Error Resume Next

sInputFile = "Input.txt"
sOutputFile = "Output.csv"
sAdminUserName = "username"
sAdminPassword = "password"

Set oFS = CreateObject("Scripting.FilesystemObject")

Set oInputFile = oFS.OpenTextFile(sInputFile, 1, True)
Set oOutputFile = oFS.CreateTextFile(sOutputFile, True)

sSerialQuery = "Select IdentifyingNumber From Win32_ComputerSystemProduct"
sWiFiQuery = "Select MACAddress from Win32_NetworkAdapterConfiguration WHERE Description Like '%WiFi%' OR Description Like '%Wireless%' OR Description Like '%AGN%'"

Set oComputerObject = CreateObject("WbemScripting.SWbemLocator")
oComputerObject.Security_.ImpersonationLevel = 3
oComputerObject.Security_.AuthenticationLevel = 0

Do While Not oInputFile.AtEndOfStream
    sPC = oInputFile.ReadLine
    Set oWMI = oComputerObject.ConnectServer(sPC, "root\cimv2", sPC & "\" & sAdminUserName, sAdminPassword)

    Set cTemp1 = oWMI.ExecQuery(sSerialQuery)
    Set cTemp2 = oWMI.ExecQuery(sWiFiQuery)
    For Each oItem In cTemp1
        oOutputFile.Write oItem.IdentifyingNumber & ","
    Next
    For Each oItem In cTemp2
        oOutputFile.WriteLine oItem.MACAddress
        Exit For
    Next
    Set cTemp1 = Nothing
    Set cTemp2 = Nothing
    Set oWMI = Nothing
Loop
Set oComputerObject = Nothing
Wscript.Echo "Complete."

Function Ping(sHost)
    Dim oWMILocal, cPings, oPing, iPingResult

    Set oWMILocal = GetObject("winmgmts:\\.\root\cimv2")

    Set cPings = oWMILocal.ExecQuery("Select * from Win32_PingStatus " &_
        "Where Address = '" & sHost & "' AND timeout = 500")
    For Each oPing In cPings
        iPingResult = oPing.StatusCode
        If (iPingResult = 0) Then
            Ping = True
            Exit Function
        Else
            Ping = False
        End If
    Next
End Function

Open in new window

0
 
LVL 7

Expert Comment

by:Waseems
ID: 33593244
check this script you might, I don't have wireless network card on my computer to check it but you might need to change line 51 if it didn't work remove  WHERE AdapterType like '%wireless%' from the code and try it I thing adaptertype should be wireless but you can try it
Dim objFSO, objFolder, objShell, objTextFile, objFile

Dim strOutputDir, strOutputFile, strComputersList 

On Error Resume Next



Const ForAppending = 8

Const ForReading = 1



'Variables to change

strOutputDir = "d:\desktop"	'folder to store outputfile should be written without end \

strOutputFile = "output.txt"	'file to store output data should start with \

strComputersList = "d:\desktop\ServerList.txt"





Set objFSO = CreateObject("Scripting.FileSystemObject")



If objFSO.FolderExists(strOutputDir) Then

   Set objFolder = objFSO.GetFolder(strOutputDir)

Else

   Set objFolder = objFSO.CreateFolder(strOutputDir)

End If



If objFSO.FileExists(strOutputDir & "\" & strOutputFile) Then

   Set objFolder = objFSO.GetFolder(strOutputDir)

Else

   Set objFile = objFSO.CreateTextFile(strOutputDir & "\" & strOutputFile)

End If 



set objFile = nothing

set objFolder = nothing



Set objTextFile = objFSO.OpenTextFile (strOutputDir & "\" & strOutputFile, ForAppending, True)







Set objFile = objFSO.OpenTextFile(strComputersList, ForReading)

Do Until objFile.AtEndOfStream



strComputer = objFile.ReadLine 

tLine = strComputer



Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 



select case err.number

  case 0

    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_SystemEnclosure",,48) 

    For Each objItem in colItems 

        tLine= tLine & vbtab & objItem.SerialNumber

    Next



    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE AdapterType like '%wireless%'",,48) 



    For Each objItem in colItems 

        if objItem.MACAddress <>"" then tLine= tLine & vbtab & objItem.AdapterType & " (" objItem.MACAddress & ")"

    Next

  case 70

    tLine=tLine & vbtab & "Access denied"

  case 462

    tLine=tLine & vbtab & "Cannot be Contacted"

  case else

    wscript.echo err.number

end select



  objTextFile.WriteLine(tLine)

   

  strComputer = Null

  objWMIService = Null

  colItems = Null

  objItem = Null



Loop

objFile.Close



objTextFile.Close

wscript.echo "Script execution Completed, check the file " & strOutputDir & "\" & strOutputFile & " for output results"

Open in new window

0
 

Author Comment

by:Bianchi928
ID: 33593331
spinzr0:

it's almost there...Here's the content of my input file

745362S
9P7HR1S
B45362S
5H1VY1S
155362S

and the following is my output file

745362S      00:23:14:A8:A4:08
00:23:14:A8:A4:08      
B45362S      00:23:14:A8:63:88
00:23:14:A8:63:88      
155362S      00:23:14:A5:DE:A4

As you can see it repeats the Mac address of the previous because the laptops 9P7HR1S & 5H1VY1S were turned off.

I would rather see the output look like the following

745362S      00:23:14:A8:A4:08
9P7HR1S      Not Found
B45362S      00:23:14:A8:63:88
5H1VY1S      Not Found
155362S      00:23:14:A5:DE:A4

Thanks
Cheers



0
 
LVL 8

Expert Comment

by:spinzr0
ID: 33596537
Give this a shot
On Error Resume Next



sInputFile = "Input.txt"

sOutputFile = "Output.csv"

sAdminUserName = "username"

sAdminPassword = "password"



Set oFS = CreateObject("Scripting.FilesystemObject")



Set oInputFile = oFS.OpenTextFile(sInputFile, 1, True)

Set oOutputFile = oFS.CreateTextFile(sOutputFile, True)



sSerialQuery = "Select IdentifyingNumber From Win32_ComputerSystemProduct"

sWiFiQuery = "Select MACAddress from Win32_NetworkAdapterConfiguration WHERE Description Like '%WiFi%' OR Description Like '%Wireless%' OR Description Like '%AGN%'"



Set oComputerObject = CreateObject("WbemScripting.SWbemLocator")

oComputerObject.Security_.ImpersonationLevel = 3

oComputerObject.Security_.AuthenticationLevel = 0



Do While Not oInputFile.AtEndOfStream

    sPC = oInputFile.ReadLine

    Err.Clear

    Set oWMI = oComputerObject.ConnectServer(sPC, "root\cimv2", sPC & "\" & sAdminUserName, sAdminPassword)

    If Err.Number <> 0 Then

        oOutputFile.WriteLine sPC & ",Not Found"

        Err.Clear

    Else

        Set cTemp1 = oWMI.ExecQuery(sSerialQuery)

        Set cTemp2 = oWMI.ExecQuery(sWiFiQuery)

        For Each oItem In cTemp1

            oOutputFile.Write oItem.IdentifyingNumber & ","

        Next

        For Each oItem In cTemp2

            oOutputFile.WriteLine oItem.MACAddress

            Exit For

        Next

        Set cTemp1 = Nothing

        Set cTemp2 = Nothing

    End If

    Set oWMI = Nothing

Loop

Set oComputerObject = Nothing

Wscript.Echo "Complete."



Function Ping(sHost)

    Dim oWMILocal, cPings, oPing, iPingResult



    Set oWMILocal = GetObject("winmgmts:\\.\root\cimv2")



    Set cPings = oWMILocal.ExecQuery("Select * from Win32_PingStatus " &_

        "Where Address = '" & sHost & "' AND timeout = 500")

    For Each oPing In cPings

        iPingResult = oPing.StatusCode

        If (iPingResult = 0) Then

            Ping = True

            Exit Function

        Else

            Ping = False

        End If

    Next

End Function

Open in new window

0
 

Author Comment

by:Bianchi928
ID: 33608046
spinzr0:

Since it was weekend, I gave it a shot this morning and the following is the result..It seems that "spc" is not returning any value (service tag).

,Not Found
,Not Found
,Not Found
,Not Found
1R43T1S,00:1C:BF:8B:7F:CF
,Not Found
,Not Found
DMSDT1S,00:1D:E0:79:A5:75

Cheers
Christian

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:Waseems
ID: 33608186

have you tried my code or spinzr0
0
 
LVL 8

Accepted Solution

by:
spinzr0 earned 500 total points
ID: 33608204
sPC is just the read in line.  Does the input file ahve blank lines?  I've added a messagebox so you can see as it reads in if its getting valid data.
On Error Resume Next



sInputFile = "Input.txt"

sOutputFile = "Output.csv"

sAdminUserName = "username"

sAdminPassword = "password"



Set oFS = CreateObject("Scripting.FilesystemObject")



Set oInputFile = oFS.OpenTextFile(sInputFile, 1, True)

Set oOutputFile = oFS.CreateTextFile(sOutputFile, True)



sSerialQuery = "Select IdentifyingNumber From Win32_ComputerSystemProduct"

sWiFiQuery = "Select MACAddress from Win32_NetworkAdapterConfiguration WHERE Description Like '%WiFi%' OR Description Like '%Wireless%' OR Description Like '%AGN%'"



Set oComputerObject = CreateObject("WbemScripting.SWbemLocator")

oComputerObject.Security_.ImpersonationLevel = 3

oComputerObject.Security_.AuthenticationLevel = 0



Do While Not oInputFile.AtEndOfStream

    sPC = oInputFile.ReadLine

    Msgbox sPC

    Err.Clear

    Set oWMI = oComputerObject.ConnectServer(sPC, "root\cimv2", sPC & "\" & sAdminUserName, sAdminPassword)

    If Err.Number <> 0 Then

        oOutputFile.WriteLine sPC & ",Not Found"

        Err.Clear

    Else

        Set cTemp1 = oWMI.ExecQuery(sSerialQuery)

        Set cTemp2 = oWMI.ExecQuery(sWiFiQuery)

        For Each oItem In cTemp1

            oOutputFile.Write oItem.IdentifyingNumber & ","

        Next

        For Each oItem In cTemp2

            oOutputFile.WriteLine oItem.MACAddress

            Exit For

        Next

        Set cTemp1 = Nothing

        Set cTemp2 = Nothing

    End If

    Set oWMI = Nothing

Loop

Set oComputerObject = Nothing

Wscript.Echo "Complete."



Function Ping(sHost)

    Dim oWMILocal, cPings, oPing, iPingResult



    Set oWMILocal = GetObject("winmgmts:\\.\root\cimv2")



    Set cPings = oWMILocal.ExecQuery("Select * from Win32_PingStatus " &_

        "Where Address = '" & sHost & "' AND timeout = 500")

    For Each oPing In cPings

        iPingResult = oPing.StatusCode

        If (iPingResult = 0) Then

            Ping = True

            Exit Function

        Else

            Ping = False

        End If

    Next

End Function

Open in new window

0
 

Author Comment

by:Bianchi928
ID: 33608280
spinzr0:

No, there's no blank in the input file. So I ran again the latest version you gave me and that's the result.
Very Strange !

Input File

155362S
1DCM52S
1R43T1S
23N7V1S
355362S
3LV132S
3RN1W1S
69WW12S

Output CSV file

155362S,Not Found
1DCM52S,00:24:D6:83:3C:D6
1R43T1S,00:1C:BF:8B:7F:CF
23N7V1S,Not Found
355362S,00:23:14:A6:07:B4
3LV132S,00:24:D6:09:72:E4
3RN1W1S,00:1F:3B:7B:49:51
69WW12S,Not Found
,Not Found

There's a little mistake on the last record...it shouldn't be there

Cheers
0
 

Author Comment

by:Bianchi928
ID: 33608346
spinzr0:

Thanks for your help.
0
 

Author Closing Comment

by:Bianchi928
ID: 33608349
Well Done
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now