Set IP/Gateway/Dns

本文介绍了一段VBScript脚本,用于自动配置Windows系统的网络设置,包括静态IP地址、子网掩码、默认网关及DNS服务器等。通过WMI(Windows Management Instrumentation)接口实现对网络适配器配置的修改。

Set WshNetwork = WScript.CreateObject("WScript.Network")
strComputer = WshNetwork.ComputerName

Set WshNetwork = nothing

const IPADD = "172.16.1.139"
const Netmask = "255.255.255.0"
const Gateway = "172.16.1.1"
const Pdns = "172.16.1.10"
const Sdns = "172.16.1.12"
const dnsdetail1 = "nts01.worldchina.com.hk"
const dnsdetail2 = "greensward.com.hk"

 

 

Set objWMIService = GetObject("winmgmts://" & strComputer & "/root/cimv2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strIPAddress = Array(IPADD)
strSubnetMask = Array(Netmask)
strGateway = Array(Gateway)
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
    If errEnable = 0 Then
      WScript.Echo "IP地址已修正!"
    Else
      WScript.Echo "IP地址未修改!"
    End If
Next

Set colNetCards = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objNetCard in colNetCards
    arrDNSServers = Array(Pdns,Sdns)
    objNetCard.SetDNSServerSearchOrder(arrDNSServers)
        Wscript.Echo "DNS服務器地址已添加!"
Next
Set colNetCards = nothing

Set objNetworkSettings = objWMIService.Get("Win32_NetworkAdapterConfiguration")
arrDNSSuffixes = Array(dnsdetail1, dnsdetail2)
objNetworkSettings.SetDNSSuffixSearchOrder(arrDNSSuffixes)
Wscript.Echo "DNS服務器尾碼已添加!"
Set objNetworkSettings = nothing
Set objWMIService = nothing

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值