华域联盟 vbs 用VBS设置静态IP和DNS服务器地址的代码

用VBS设置静态IP和DNS服务器地址的代码

核心代码一:

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 IPEnabled=TRUE") 

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 

记得把参数改成自己想要的。

代码二:

将计算机的 IP 地址设置为 192.168.1.111,并将 IP 网关设置为 192.168.1.1。

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\" & strComputer & " ootcimv2") 
Set colNetAdapters = objWMIService.ExecQuery _ 
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") 
strIPAddress = Array("192.168.1.111") 
strSubnetMask = Array("255.255.255.0") 
strGateway = Array("192.168.1.1") 
strGatewayMetric = Array(1) 
For Each objNetAdapter in colNetAdapters 
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) 
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) 
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

原文:http://demon.tw/programming/vbs-modify-ip-dns-setting.html

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » 用VBS设置静态IP和DNS服务器地址的代码

转载请保留出处和原文链接:https://www.cnhackhy.com/16005.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部