List MAC-address for wired ethernet cards

The script tries to identify wired network cards and list the mac address(es) to a file. By default it creates one file in the current directory per computer name, but you can change it to write everything to one large file. This can lead to write locks if you try to run the script at login, but if you let it run for a couple of days you should get most of them. The script was created to aid in importing computers into MS CCM collections. The idea was to put the script on a flashdrive, run around to all the computers being added and run the script manually. This was to make sure that every computer was logged.

Please note: Disabled or disconnected ethernet cards are not listed, so make sure the card for which you want the mac address is connected and enabled when you run the script.

 

 

'***************************************************************************
'** List LAN MAC addresses and output them to a file named [Computername] **
'** Output format tailored for CCM (csv): ComputerName, , MAC, CardName   ** 
'**                             Jan Kåre Lokna                         **
'***************************************************************************
option explicit
on error goto 0

'Get computername for filename.
Dim wshShell
Set wshShell = WScript.CreateObject( "WScript.Shell" )
Dim strFilename
strFilename = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" ) 'change this to a filename on a network share if you want to use a common file for all computers, e.g strFilename="\\server\share\filename.txt"

'Get cards from WMI
Dim objWMIService
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") 
Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapter where MACAddress != null AND AdapterTypeID like '0'") 
'Create / open file
Dim fso
Dim objFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(strFilename) Then
	Set objFile = fso.CreateTextFile(strFilename)
Else
	Set objFile = fso.OpenTextFile(strFilename, ForReading, True)
	Dim strReadFile
	strReadFile = objFile.ReadAll
	objFile.Close
	Set objFile = fso.OpenTextFile(strFilename, ForAppending, True)
  End If
'Process cards
Dim colItems
Dim objItem
For Each objItem in colItems 
	Dim strMAC
	strMAC = objItem.MACAddress
	Dim strName 
	strName = objItem.Name
	'Check for bluetooth or wifi
	If InStr(strName, "Bluetooth") > 0 Then
		'Do nothing
	ElseIf InStr(strName, "WiFi") > 0 Then
		'Do nothing
	else
		'Format output
		Dim strInfo
		strInfo = objItem.SystemName & ", " & "," & strMAC & "," & strName
		wscript.echo strInfo
		'write file
		'Check if MAC exists in file
		If InStr(strReadFile, strMAC) = 0 Then
			'if not, then append to file
			objFile.WriteLine strInfo
			wscript.echo (strMAC & " written to " & strFileName)
		 End If
	 end if
Next
'Close file
objFile.Close


Author: DizzyBadger

SQL Server DBA, Cluster expert, Principal Analyst

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.