Solved

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

Posted on 2009-04-09
7
568 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
[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
  • 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Starting to use Git with Visual  Studio Online 1 71
Visual C# Windows Project - Clone Form 4 39
Call to SQL server times out 5 55
.NET universe documentation poster 2 25
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

751 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