Disclaimer: I am doing this because I’m bored and looking for an excuse to code.
Anyway, the logic is severely flawed for most scenarios that I can think of, such as iterating a list of servers/clients and wanting their LAN and WLAN MAC addresses (which seems to be what’s wanted). First of all, you don’t account for foreign versions of Windows (and using only default names), such as on my Norwegian work computer, where “Wireless Network Connection” actually has the default name “Trådløs nettverkstilkobling”. This might be fine in your environment where you might be able to assume only English Windows exists (well, except for that foreign guy who brought his own laptop and had it joined to the domain) - but unless you actually have a script that sets these names to these assumed defaults, it’s a pretty horribly unreliable way of determining what adapter is LAN and WLAN. They could have been renamed. And what about “Local Area Connection 2” (default name for second adapter)? 3? I added support for handling multiple values in my little cmdlet which was initially based on Dave and Richard’s code.
Another thing you’re ignoring is Windows versions. On Windows 8.1 (and probably 8), the default name is no longer “Local Area Connection”, but “Ethernet”. Again, you might be in a position to make assumptions, but I wrote something a bit more robust and flexible anyway.
As I’m writing this I got to thinking that I bet there’s a better way to determine this rather than NetConnectionID. Looking at the properties returned from the WMI class: maybe the ProductName or Manufacturer properties against a list you hard-code, but it doesn’t seem convenient at all. I’m already done writing up the code, so I’ll just post it along with some examples.
If you remove the “[ordered]” part before the hash literal (object properties) this should also work with PowerShell v2, unless I overlooked something. Some of the syntax in the examples below assumes v3 or above.
Here is a test run on a laptop with Norwegian Windows 7 that demonstrates the “parameterized” LAN filter and WLAN filter, and how it doesn’t return anything with the defaults. I use the -Verbose parameter to display the WQL query that’s formed. Multiple values for the parameters are supported for each filter, and you can use the -Like wildcards “*” and “?”.
PS P:\PowerShell> 'localhost' | Get-MacCrap -Verbose | ft -a
VERBOSE: Using filter: NetConnectionID LIKE "%Local Area Connection%" OR NetConnectionID LIKE "%Wireless Network Connection%"
ComputerName LAN WLAN Serial
------------ --- ---- ------
localhost 5XGX422
There you can see that no MAC addresses are listed, because the filters don’t match the list which looks like this:
PS P:\PowerShell> gwmi win32_networkadapter | % netconnectionid
Lokal tilkobling
Trådløs nettverkstilkobling
Bluetooth-nettverkstilkobling
VMware Network Adapter VMnet1
VMware Network Adapter VMnet8
Now specify custom, localized filters:
PS P:\PowerShell> 'localhost' | Get-MacCrap -Verbose -WlanFilter 'Trådløs*' -LanFilter 'Lokal tilkobling' | ft -a
VERBOSE: Using filter: NetConnectionID LIKE "Lokal tilkobling" OR NetConnectionID LIKE "Trådløs%"
ComputerName LAN WLAN Serial
------------ --- ---- ------
localhost 5C:27:0A:82:42:B7 19:13:96:54:29:71 5XGX422
Now try something where you get multiple hits (skipping WLAN now) and observe that multiple values are semicolon-joined:
PS P:\PowerShell> 'localhost' | Get-MacCrap -LanFilter 'VMware*' | ft -a
ComputerName LAN WLAN Serial
------------ --- ---- ------
localhost 00:50:56:D0:00:02;00:50:56:D0:00:06 5XGX422
Ok, that’s about it. I’m uploading the code in a Get-MacCrap.ps1.txt file. … I’m having trouble uploading (getting an error), so I uploaded it to my own site here: http://www.powershelladmin.com/wiki/File:Get-MacCrap.ps1.txt
Cheers.