1

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)

2 Answers 2

1

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

Worked for me

0

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'" 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.