Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script to extract MAC addresses

Posted on 2011-03-23
6
Medium Priority
?
750 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 1000 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 1000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

885 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