Powershell – Change DNS using Powershell error

domain-name-systempowershell

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)

Best Answer

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

Worked for me