Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
570 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

670 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