Solved

Script to extract MAC addresses

Posted on 2011-03-23
6
732 Views
Last Modified: 2012-05-11
Hi Everyone,
Im trying to compile a list of DNS names and their coresponding MAC addresses from AD in server 08 R2, for the purposes of WOL.

I found this helpful post here-->
http://blog.jeffharbert.com/index.php/2009/03/a-vbscript-wake-on-lan-project/

However, I can't seem to extract the MAC addresses using the script provided. Im hoping someone can help me work out whats going on.

Thanks!
'—– Let's Dim our variables —————————————————————-

Dim strComputer, objFSO

'—– Set some constants used for manipulating text files————————————-

Const ForReading = 1
Const ForAppending = 8

'—– Let's open the list of workstations we grabbed from Active Directory ——————-

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scriptomatic2\workstations.txt", ForReading)

'—– We want to loop through the text file line by line, grabbing MAC addresses as we go —

Do Until objFile.AtEndOfStream
strComputer = objFile.ReadLine

'—– Let's see if we can ping the workstation ———————————————–

Set WshShell = CreateObject("WScript.Shell")
PINGFlag = Not CBool(WshShell.run("ping -n 2 -w 250 " & strComputer,0,True))

'—– If we can ping the workstation, grab the MAC address and save it in a text file ——–

If PINGFlag = True Then
wScript.Echo strComputer & " pings"
Dim objShell,objExec
Set objShell=CreateObject("wscript.shell")
strCommand="nbtstat -a " & strComputer
Set objExec=objShell.Exec(strCommand)
Do While objExec.StdOut.AtEndOfStreamTrue
strLine=objExec.StdOut.ReadLine
If InStr(strLine,"MAC Address") Then
arrFields = Split(strLine, " ")
strMAC1 = arrFields(7)
strMACAddress = Replace(strMAC1, "-", "")
strCompEntry = strComputer & "," & strMACAddress
Set objFile2 = objFSO.OpenTextFile("C:\Scriptomatic2\MAC_Addresses.txt", ForAppending)
objFile2.WriteLine strCompEntry
objFile2.close
End If
Loop
Else

'—– If we can't ping the workstation, let's log that so we can get the MAC address later —-

Set objFile3 = objFSO.OpenTextFile("C:\Scriptomatic2\cannot_ping.txt", ForAppending)
objFile3.WriteLine strComputer
objFile3.close

End If

Loop

Open in new window

0
Comment
Question by:kingdingathing
  • 3
  • 2
6 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 35203359
Hi, try this.

Regards,

Rob.
'—– Let's Dim our variables —————————————————————-

Dim strComputer, objFSO

'—– Set some constants used for manipulating text files————————————-

Const ForReading = 1
Const ForAppending = 8

'—– Let's open the list of workstations we grabbed from Active Directory ——————-

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("computers.txt", ForReading)

'—– We want to loop through the text file line by line, grabbing MAC addresses as we go —

Do Until objFile.AtEndOfStream
	strComputer = objFile.ReadLine

	'—– Let's see if we can ping the workstation ———————————————–

	Set WshShell = CreateObject("WScript.Shell")
	PINGFlag = Not CBool(WshShell.run("ping -n 2 -w 250 " & strComputer,0,True))

	'—– If we can ping the workstation, grab the MAC address and save it in a text file ——–

	If PINGFlag = True Then
		'wScript.Echo strComputer & " pings"
		Dim objShell,objExec
		Set objShell=CreateObject("wscript.shell")
		strCommand="nbtstat -a " & strComputer
		Set objExec=objShell.Exec(strCommand)
		Do While Not objExec.StdOut.AtEndOfStream
			strLine=objExec.StdOut.ReadLine
			If InStr(strLine,"MAC Address") Then
				arrFields = Split(strLine, " ")
				strMAC1 = arrFields(7)
				strMACAddress = Replace(strMAC1, "-", "")
				strCompEntry = strComputer & "," & strMACAddress
				Set objFile2 = objFSO.OpenTextFile("MAC_Addresses.txt", ForAppending, True)
				objFile2.WriteLine strCompEntry
				objFile2.close
			End If
		Loop
	Else

		'—– If we can't ping the workstation, let's log that so we can get the MAC address later —-

		Set objFile3 = objFSO.OpenTextFile("cannot_ping.txt", ForAppending, True)
		objFile3.WriteLine strComputer
		objFile3.close

	End If

Loop

WScript.Echo "Done"

Open in new window

0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35203363
Note: I changed the file paths to be relative to where the VBS is.

Rob.
0
 
LVL 10

Assisted Solution

by:wls3
wls3 earned 250 total points
ID: 35203958
Maybe I am missing something, but, a lot can be accomplished with Powershell here:

foreach($strComputer in $someInputObjectOrFile)
{
    $colItems = Get-wmiobject -class "Win32_NetworkAdapterConfiguration" -computername $strComputer | Where{$_.IpEnabled -Match "True"}
    foreach ($objItem in $colItems) 
    {
        $objItem.MACAddress + " " + ([System.Net.Dns]::GetHostEntry($strComputer)).HostName
    }
}

Open in new window

Additional scripting can be added for the logging and ping testing.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 2

Author Comment

by:kingdingathing
ID: 35204010
I feel stupid...I was able to extract the data i needed my exporting out of dhcp manager.

Thanks anyway guys!
0
 
LVL 2

Author Closing Comment

by:kingdingathing
ID: 35204016
Resolved it myself but want to give credit where credits due :)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35204240
Oh yeah, I forgot about DHCP export!  Thanks for the grade anyhow.

Rob.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

OfficeMate Freezes on login or does not load after login credentials are input.
This article explains how to install and use the NTBackup utility that comes with Windows Server.
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…

790 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