Solved

Script to extract MAC addresses

Posted on 2011-03-23
6
738 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
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…
Suggested Courses

623 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