简体   繁体   English

编辑Windows DNS时出错

[英]Error editing Windows DNS

I've read on several different reliable tech blogs and discussions that the following code should allow me to target my NICs with static IPs that are using specific DNS servers, and update the servers they're using. 我已经阅读了几个不同的可靠技术博客和讨论,其中以下代码应允许我使用使用特定DNS服务器的静态IP定位NIC,并更新它们使用的服务器。

$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "DHCPEnabled='FALSE'" |
        Select-Object -Property DNSServerSearchOrder |
        Where {
            $_.DNSServerSearchOrder -contains "OldDNS1" -or
            $_.DNSServerSearchOrder -contains "OldDNS2"
        }

foreach ($NIC in $NICs) {
    $NIC.SetDNSServerSearchOrder("NewDNS1","NewDNS2")
}

However, when I run that code I get the following error: 但是,当我运行该代码时,出现以下错误:

Method invocation failed because [Selected.System.Management.ManagementObject]
doesn't contain a method named 'SetDNSServerSearchOrder'. 
At line:2 char:33
+ $NIC.SetDNSServerSearchOrder <<<< ("NewDNS1","NewDNS2")
    + CategoryInfo          : InvalidOperation: (SetDNSServerSearchOrder:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

I haven't been able to figure out or properly find a solution and I'm not sure what I'm missing. 我一直无法弄清或找到合适的解决方案,而且我不确定自己缺少什么。 Does anyone have any suggestions? 有没有人有什么建议?

Edit: After @FoxDeploy's suggestion I was getting another error: 编辑:@FoxDeploy的建议后,我得到另一个错误:

Cannot find an overload for "SetDNSServerSearchOrder" and the argument count: "2".
At line:2 char:33
+ $NIC.SetDNSServerSearchOrder <<<< ("172.16.1.50","172.16.210.53")
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

I had to change: 我不得不改变:

$NIC.SetDNSServerSearchOrder("NewDNS1","NewDNS2")

It is now: 就是现在:

$NIC.SetDNSServerSearchOrder($("NewDNS1","NewDNS2"))

It's because of the select statement. 这是由于select语句。

Using Select-Object changes an object from it's previous configuration, and makes it into a System.Management.ManagementObject , which is basically just a type of PowerShell custom object. 使用Select-Object可以从其先前的配置中更改对象,并将其变为System.Management.ManagementObject ,这基本上只是PowerShell自定义对象的一种。

If you remove the Select-Object statement, this will work. 如果删除Select-Object语句,这将起作用。

$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "DHCPEnabled='FALSE'" |
        Where {
            $_.DNSServerSearchOrder -contains "OldDNS1" -or
            $_.DNSServerSearchOrder -contains "OldDNS2"
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM