Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have this powershell script that changes the DNS settings. The below script works fine, but I want to filter by MAC address to make sure I'm changing the correct NIC.


$computer = "pc01"

$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Computer $computer -Filter "IPEnabled=TRUE"

$DNSServers = "192.168.1.1","192.168.1.2"

foreach($NIC in $NICs) { $NICs.SetDNSServerSearchOrder($DNSServers)


So I changed the filter to use the MACaddress. But I get the following error.


$computer = "pc01"

$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Computer $computer -Filter "MACAddress=00:1E:55:40:70:E8"

$DNSServers = "192.168.1.1","192.168.1.2"

foreach($NIC in $NICs) { $NICs.SetDNSServerSearchOrder($DNSServers)

Output:

Invalid query + $NICs = Get-WmiObject <<<< -Class Win32_NetworkAdapterConfiguration -Computer $computer -Filter "MACAddress=00:1E:65:40:80:E4"

You cannot call a method on a null-valued expression. + $NICs.SetDNSServerSearchOrder <<<< ($DNSServers)

share|improve this question

2 Answers 2

Try a Where clause for MAC Address: $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Computer $computer | Where {$_.MACAddress -eq $MAC}

Worked for me

share|improve this answer

Put the ' marks around what is being filtered. Using -filter is preferable over using where {} because of how the data is handled. Using the where {} involves loading all the objects from the Get-WmiObject and then filtering them. Using the -filter will do the filtering right away, so you end up with less data being worked through and less time to run the script.

$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Computer $computer -Filter "MACAddress='00:1E:55:40:70:E8'"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.