欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-11-14 10:50:24
核心代码一: stripaddress = array("192.168.0.148") strsubnetmask = array("255.255.255...

核心代码一:

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

原文: