Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-04-09
7
Medium Priority
?
571 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

886 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