肥龍 發表於 2008-12-26 13:07:43

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

本帖最後由 肥龍 於 2008-12-26 13:09 編輯


http://blog.blueshop.com.tw/images/blog_blueshop_com_tw/hammerchou/1484/o_PHcip.gif


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


[來源]http://blog.blueshop.com.tw/HammerChou/archive/2006/04/12/21368.aspx
頁: [1]
查看完整版本: 透過 WMI 更改IP、子網路遮罩、閘道、DNS