用vbscript脚本实现返回 IP 配置数据的代码
程序员文章站
2022-04-10 08:01:15
用以返回配置数据(类似于 ipconfig 命令返回信息)的 wmi 脚本。' returning ip&nbs...
用以返回配置数据(类似于 ipconfig 命令返回信息)的 wmi 脚本。
' returning ip configuration data
' wmi script that returns configuration data similar to that returned by ipconfig.
strcomputer = "."
set objwmiservice = getobject("winmgmts:\\"& strcomputer & "\root\cimv2")
set coladapters = objwmiservice.execquery _
("select * from win32_networkadapterconfiguration where ipenabled = true")
n = 1
wscript.echo
for each objadapter in coladapters
wscript.echo "network adapter " & n
wscript.echo "================="
wscript.echo " description: " & objadapter.description
wscript.echo " physical (mac) address: " & objadapter.macaddress
wscript.echo " host name: " & objadapter.dnshostname
if not isnull(objadapter.ipaddress) then
for i = 0 to ubound(objadapter.ipaddress)
wscript.echo " ip address: " & objadapter.ipaddress(i)
next
end if
if not isnull(objadapter.ipsubnet) then
for i = 0 to ubound(objadapter.ipsubnet)
wscript.echo " subnet: " & objadapter.ipsubnet(i)
next
end if
if not isnull(objadapter.defaultipgateway) then
for i = 0 to ubound(objadapter.defaultipgateway)
wscript.echo " default gateway: " & objadapter.defaultipgateway(i)
next
end if
wscript.echo
wscript.echo " dns"
wscript.echo " ---"
wscript.echo " dns servers in search order:"
if not isnull(objadapter.dnsserversearchorder) then
for i = 0 to ubound(objadapter.dnsserversearchorder)
wscript.echo " " & objadapter.dnsserversearchorder(i)
next
end if
wscript.echo " dns domain: " & objadapter.dnsdomain
if not isnull(objadapter.dnsdomainsuffixsearchorder) then
for i = 0 to ubound(objadapter.dnsdomainsuffixsearchorder)
wscript.echo " dns suffix search list: " & objadapter.dnsdomainsuffixsearchorder(i)
next
end if
wscript.echo
wscript.echo " dhcp"
wscript.echo " ----"
wscript.echo " dhcp enabled: " & objadapter.dhcpenabled
wscript.echo " dhcp server: " & objadapter.dhcpserver
if not isnull(objadapter.dhcpleaseobtained) then
utcleaseobtained = objadapter.dhcpleaseobtained
strleaseobtained = wmidatestringtodate(utcleaseobtained)
else
strleaseobtained = ""
end if
wscript.echo " dhcp lease obtained: " & strleaseobtained
if not isnull(objadapter.dhcpleaseexpires) then
utcleaseexpires = objadapter.dhcpleaseexpires
strleaseexpires = wmidatestringtodate(utcleaseexpires)
else
strleaseexpires = ""
end if
wscript.echo " dhcp lease expires: " & strleaseexpires
wscript.echo
wscript.echo " wins"
wscript.echo " ----"
wscript.echo " primary wins server: " & objadapter.winsprimaryserver
wscript.echo " secondary wins server: " & objadapter.winssecondaryserver
wscript.echo
n = n + 1
next
function wmidatestringtodate(utcdate)
wmidatestringtodate = cdate(mid(utcdate, 5, 2) & "/" & _
mid(utcdate, 7, 2) & "/" & _
left(utcdate, 4) & " " & _
mid (utcdate, 9, 2) & ":" & _
mid(utcdate, 11, 2) & ":" & _
mid(utcdate, 13, 2))
end function
' returning ip configuration data
' wmi script that returns configuration data similar to that returned by ipconfig.
strcomputer = "."
set objwmiservice = getobject("winmgmts:\\"& strcomputer & "\root\cimv2")
set coladapters = objwmiservice.execquery _
("select * from win32_networkadapterconfiguration where ipenabled = true")
n = 1
wscript.echo
for each objadapter in coladapters
wscript.echo "network adapter " & n
wscript.echo "================="
wscript.echo " description: " & objadapter.description
wscript.echo " physical (mac) address: " & objadapter.macaddress
wscript.echo " host name: " & objadapter.dnshostname
if not isnull(objadapter.ipaddress) then
for i = 0 to ubound(objadapter.ipaddress)
wscript.echo " ip address: " & objadapter.ipaddress(i)
next
end if
if not isnull(objadapter.ipsubnet) then
for i = 0 to ubound(objadapter.ipsubnet)
wscript.echo " subnet: " & objadapter.ipsubnet(i)
next
end if
if not isnull(objadapter.defaultipgateway) then
for i = 0 to ubound(objadapter.defaultipgateway)
wscript.echo " default gateway: " & objadapter.defaultipgateway(i)
next
end if
wscript.echo
wscript.echo " dns"
wscript.echo " ---"
wscript.echo " dns servers in search order:"
if not isnull(objadapter.dnsserversearchorder) then
for i = 0 to ubound(objadapter.dnsserversearchorder)
wscript.echo " " & objadapter.dnsserversearchorder(i)
next
end if
wscript.echo " dns domain: " & objadapter.dnsdomain
if not isnull(objadapter.dnsdomainsuffixsearchorder) then
for i = 0 to ubound(objadapter.dnsdomainsuffixsearchorder)
wscript.echo " dns suffix search list: " & objadapter.dnsdomainsuffixsearchorder(i)
next
end if
wscript.echo
wscript.echo " dhcp"
wscript.echo " ----"
wscript.echo " dhcp enabled: " & objadapter.dhcpenabled
wscript.echo " dhcp server: " & objadapter.dhcpserver
if not isnull(objadapter.dhcpleaseobtained) then
utcleaseobtained = objadapter.dhcpleaseobtained
strleaseobtained = wmidatestringtodate(utcleaseobtained)
else
strleaseobtained = ""
end if
wscript.echo " dhcp lease obtained: " & strleaseobtained
if not isnull(objadapter.dhcpleaseexpires) then
utcleaseexpires = objadapter.dhcpleaseexpires
strleaseexpires = wmidatestringtodate(utcleaseexpires)
else
strleaseexpires = ""
end if
wscript.echo " dhcp lease expires: " & strleaseexpires
wscript.echo
wscript.echo " wins"
wscript.echo " ----"
wscript.echo " primary wins server: " & objadapter.winsprimaryserver
wscript.echo " secondary wins server: " & objadapter.winssecondaryserver
wscript.echo
n = n + 1
next
function wmidatestringtodate(utcdate)
wmidatestringtodate = cdate(mid(utcdate, 5, 2) & "/" & _
mid(utcdate, 7, 2) & "/" & _
left(utcdate, 4) & " " & _
mid (utcdate, 9, 2) & ":" & _
mid(utcdate, 11, 2) & ":" & _
mid(utcdate, 13, 2))
end function
下一篇: 用vbs实现配置静态 IP 地址