Solved

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

Posted on 2009-04-09
7
564 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
ID: 24114375
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
ID: 24116089
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
ID: 24122105
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 41

Expert Comment

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

Author Comment

by:wally_davis
ID: 24124273
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
ID: 24124399
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
ID: 24130768
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

19 Experts available now in Live!

Get 1:1 Help Now