Solved

Script to extract MAC addresses

Posted on 2011-03-23
6
733 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

685 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