您的位置:首页 > 编程语言 > VB

VBS 访问WMI读取IP地址,并将其设置为静态IP

2010-10-29 15:35 405 查看
最近机子IP很不稳定,方便期间,全部设置为静态的。机子很多,就写个vbs脚本:

strComputer = "."
Set objWMIService = GetObject( _
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colNics = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapter " _
& "Where NetConnectionID = " & _
"'Local Area Connection'")

For Each objNic in colNics
Set colNicConfigs = objWMIService.ExecQuery _
("ASSOCIATORS OF " _
& "{Win32_NetworkAdapter.DeviceID='" & _
objNic.DeviceID & "'}" & _
" WHERE AssocClass=Win32_NetworkAdapterSetting")
For Each objNicConfig In colNicConfigs
For Each strIPAddress in objNicConfig.IPAddress
strIPAddress1 = strIPAddress
Exit For
Next
For Each strGateway in objNicConfig.DefaultIPGateway
strGateway1 = strGateway
Exit For
Next
For Each strSubnet in objNicConfig.IPSubnet
strSubnet1 = strSubnet
Exit For
Next
Next
Next

strComputer1 = "."
Set objWMIService1 = GetObject( _
"winmgmts:\\" & strComputer1 & "\root\cimv2")
Set colNetAdapters = objWMIService1.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration " _
& "where IPEnabled=TRUE")

strGatewaymetric = Array(1)
aSubnet = Array(strSubnet1)
aIPAddress = Array(strIPAddress1)
aGateway = Array(strGateway1)

For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic( _
aIPAddress , aSubnet)
errGateways = objNetAdapter.SetGateways(_
aGateway, strGatewaymetric)
Next


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: