[powershell]有关IP的设置
#Runing in Administrator
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -Filter Index=10#index 要手动修改 Foreach($NIC in $NICS){ $NIC.EnableStatic("192.168.0.100","255.255.255.0")
#若返回值不为0,则是地址有误 $NIC.SetGateways("192.168.171.1") $DNSServers = "198.102.234.125","198.102.234.126" $NIC.SetDNSServerSearchOrder($DNSServers) $NIC.SetDynamicDNSRegistration("TRUE") $NIC.SetWINSServer("198.102.234.125","198.102.234.126") } pause
打开DHCP $NIC.enabledhcp()
关闭DHCP,就必须要配置静态IP地址:$NIC.enablestatic("192.168.0.2", "255.255.255.0")
自动DNS $NIC.SetDNSServerSearchOrder()
在多个适配器上打开DHCP
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=true -ComputerName . | ForEach-Object -Process {$_.EnableDHCP()}
重启网卡:
(gwmi Win32_NetworkAdapter -Filter Index=11).Disable()
重启网卡VBS版:
Set objWMIServer=GetObject("winmgmts:\\.\root\cimv2") Set colAdapters=objWMIServer.ExecQuery("select * from WIN32_NetworkAdapter where Index=10") For Each objAdapter In colAdapters objAdapter.Disable objAdapter.Enable Next
获得自定义信息
gwmi Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUe | select-object -Property Index,Description,DHCPEnabled,IPAddress,IPSubnet,DHCPServer,DefaultIPGateway,DNSServerSearchOrder
若用CMD
设置动态获取IP地址: netsh interface ip set address name="以太网" source=dhcp
设置固定IP: netsh interface ip set address name="以太网" source=static addr=192.168.0.10 mask=255.255.255.0 gateway=192.168.0.1
参数说明:
1.name:网络连接名称,一般为“本地连接”。你可以在“控制面板”->“网络连接”中看到。
2.source:获取IP的途径。动态获取,则为dhcp,手动设置,则为static。
3.addr:要设置的IP地址。
4.mask:子网掩码。
5.gateway:网关地址。
6.gwmetric:网关跃点数,可以设置为整型数值,也可以设置为“自动”:auto。
设置DNS:
自动获取DNS netsh interface ip set dns name="以太网" source=dhcp
手动设置单个DNS etsh interface ip set dns name="以太网" source=static addr=218.74.122.74 register=primary
手动设置多个DNS netsh interface ip set dns name="以太网" source=static addr=218.74.122.74 index=2
参数说明:
1.name:网络连接名称,一般为“本地连接”。你可以在“控制面板”->“网络连接”中看到。
2.source:获取IP的途径。动态获取,则为dhcp,手动设置,则为static。
3.addr:要设置的IP地址。
4.register:
5.none: 禁用动态 DNS 注册。
6.primary: 只在主 DNS 后缀下注册。
7.both: 在主 DNS 后缀下注册,也在特定连接后缀下注册。
8.index:设置的DNS的顺序号。
VBS:
'Runing in Administrator strIPAddress = Array("192.168.0.148") strSubnetMask = Array("255.255.255.0") strGateway = Array("192.168.0.1") strGatewayMetric = Array(1) arrDNSServers = Array("192.168.0.1","192.168.0.2") strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery _ ("Select * from Win32_NetworkAdapterConfiguration where Index=10") For Each objNetAdapter in colNetAdapters errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) errDNS = objNetAdapter.SetDNSServerSearchOrder(arrDNSServers) If errEnable = 0 Then WScript.Echo "The IP address has been changed." Else WScript.Echo "The IP address could not be changed." End If Next