Solved

Using WMI. Only need to return one MAC Address but foreach loop is returning many

Posted on 2009-04-09
7
563 Views
Last Modified: 2013-12-17
I'm creating a C# Console application using WMI and the NetworkAdapter Win32 API to pull the MAC Address from a number of computers. However, I'm trying to re-write the code to only grab the "main" MAC Address. Not any virtual addresses. After performing tests on a multitude of Workstations, it appears that the first MAC in the return list is the original but I want to be sure.
Below is the code. I'm trying to figure out how to incorporate the "for" statement to just get the original MAC Address. How would I go about doing that?
Much appreciated,
Wally
ConnectionOptions mgmtConn = new ConnectionOptions();

            mgmtConn.Impersonation = ImpersonationLevel.Impersonate;

            mgmtConn.Authentication = AuthenticationLevel.Packet;
 

ManagementScope scope = new ManagementScope();
 

scope = new ManagementScope("\\root\\cimV2", mgmtConn);

scope.Connect();
 

ObjectQuery queryNA = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter");
 

ManagementObjectSearcher objNaSearcher = new ManagementObjectSearcher(scope, queryNA);
 

foreach (ManagementObject objQueried in objNaSearcher.Get())

            {

                Console.WriteLine("------------------------------------");

                Console.WriteLine("Win32_NetworkAdapter instance");

                Console.WriteLine("------------------------------------");

                Console.WriteLine("MAC Address: {0}", objQueried["MACAddress"]);

            }
 

            Console.ReadLine();

Open in new window

0
Comment
Question by:wally_davis
  • 4
  • 2
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Hmmm...great question...I believe what you need to do is to check the PhysicalAdapter property, but that only appears to be visible with Vista.

Win32_NetworkAdapter Class
http://msdn.microsoft.com/en-us/library/aa394216(VS.85).aspx

PhysicalAdapter
Data type: boolean
Access type: Read-only

PhysicalAdapter
Data type: boolean
Access type: Read-only

Indicates whether the adapter is a physical or a logical adapter. If True, the adapter is physical.

Windows Server 2003, Windows XP, Windows 2000, and Windows NT 4.0:  This property is not available.
0
 
LVL 41

Expert Comment

by:graye
Comment Utility
They way that I distinguish them is by looking at the PnPDeviceID field...  if it starts with "ROOT" then it's not a physical device.
0
 

Author Comment

by:wally_davis
Comment Utility
I appreciate the suggested recommendations and will look to potentially incorporate some of those ideas.
However, the root of my problem is getting through to the remote computer in order to start pulling back data.
I tried another test that indicates that there are no Firewall or DCOM issues. I go to Computer Management, right-click on the "Computer Management (Local)" icon, select "Connect to another computer", change to the computer name, and voila, I'm in. So, connecting to WMI on another machine works. Just not in my code. Any suggestions one where the potential cause might be would be great. :)
0
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

 
LVL 41

Expert Comment

by:graye
Comment Utility
Tell us a bit more about your scenario...  WinXP?  Active Directory Domain?
0
 

Author Comment

by:wally_davis
Comment Utility
I'll be running this executable eventually from an Windows 2003 Server that runs SQL. We're going to be running some checks on data from SQL and of course, I need to be able to get this data into the Database first before we run some other processes.
The Remote workstations I will need to connect to are XP SP2 and Vista. Any other information let me know.
0
 

Author Comment

by:wally_davis
Comment Utility
Oh, and yes, the Server and the workstations are in an Active Directory Domain environment.
0
 

Accepted Solution

by:
wally_davis earned 0 total points
Comment Utility
I needed to add the ManagementOptions "Username" and "Password" options and then also discovered I needed to pass in the Domain\Username correctly using the correct path (and not an escape sequence character).
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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

17 Experts available now in Live!

Get 1:1 Help Now