无需知道网络连接名称即可更改 IP 地址的脚本

无需知道网络连接名称即可更改 IP 地址的脚本

假设我有一些装有 Windows 的 PC。每个 PC 都只有 1 个本地连接,但我不知道全名。是否可以创建一个通用脚本来更改这些 PC 的 IP 地址?我尝试使用 netsh,但正如我所说,我不知道 netsh 所需的连接全名。(IP 地址、默认网关、DNS 服务器都是已知的,并且必须在脚本内,如果可能的话,只需检测连接名称)。

答案1

看一下Powershell:

$activeNICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE
# filter this more until you have the NIC that you really need

Foreach($NIC in $activeNICs) {
$ip = ($NIC.IPAddress[0])
$gateway = $NIC.DefaultIPGateway
$subnet = $NIC.IPSubnet[0]
$dns = $NIC.DNSServerSearchOrder
$NIC.EnableStatic($ip, $subnet)
$NIC.SetGateways($gateway)
$NIC.SetDNSServerSearchOrder($dns)
$NIC.SetDynamicDNSRegistration("FALSE")
} 

只需修改它以包含您的特定设置并删除您不需要的设置。

您可以在 Powershell ISE 中创建和编辑脚本(在开始菜单中搜索 ise)。一旦它按预期工作,请使用扩展名 .ps1 保存它。您可以使用

powershell -executionpolicy bypass C:\path\to\script.ps1 

以下是一篇涉及该主题的 Microsoft 文章:http://blogs.technet.com/b/danstolts/archive/2012/01/31/using-powershell-to-get-or-set-networkadapterconfiguration-view-and-change-network-settings- including-dhcp-dns-ip-address-and-more-dynamic-and-static-step-by-step.aspx

相关内容