Solved

Script to extract MAC addresses

Posted on 2011-03-23
6
729 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
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…
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…

708 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

11 Experts available now in Live!

Get 1:1 Help Now