使用 WMI 更改IP、子網路遮罩、閘道、DNS

使用 WMI 更改IP、子網路遮罩、閘道、DNS

透過 WMI 更改IP、子網路遮罩、閘道、DNS

PH CIP

Dim strWMIcls As String

Dim objNetAdt As Object

Dim strIPaddress As String, strSubMask As String, strGateway As String

Dim strDNS1 As String, strDNS2 As String

strWMIcls = "Win32_NetworkAdapterConfiguration" ' WMI 類別

strIPaddress = "192.168.10.99" ' IP 位址

strSubMask = "255.255.255.0" ' 子網路遮罩

strGateway = "192.168.10.254" ' 預設閘道

strDNS1 = "168.95.1.1" ' 慣用 DNS 伺服器

strDNS2 = "168.95.192.1" ' 其他 DNS 伺服器

Set objNetAdt = GetObject("winmgmts:").InstancesOf(strWMIcls)(strWMIcls & ".Index=1")

' Index=1 , 1 是可變的 , 可為 1 ~ N

With objNetAdt

' 變更 IP Address

If .EnableStatic(Array(strIPaddress), Array(strSubMask)) = 0 Then MsgBox "IP 變更成功 !"

' 變更 Gateway

If .SetGateways(Array(strGateway), Array(1)) = 0 Then MsgBox "Gateway 變更成功 !"

' 變更 DNS Server

If .SetDNSServerSearchOrder(Array(strDNS1, strDNS2)) = 0 Then MsgBox "DNS 變更成功 !"

End With