几段非常有用的脚本(来自微软网站,由downmoon精心收集)
程序员文章站
2022-03-06 23:48:34
几段非常有用的脚本(来自微软网站,由downmoon精心收集) 一、在网络硬件故障或网络故障断开时发送警告 复制代码 代码如下:strcomputer...
几段非常有用的脚本(来自微软网站,由downmoon精心收集)
一、在网络硬件故障或网络故障断开时发送警告
strcomputer = "."
set objwmiservice = getobject("winmgmts:" & strcomputer & " ootwmi")
set colmonitoredevents = objwmiservice.execnotificationquery _
("select * from msndis_statusmediadisconnect")
do while true
set strlatestevent = colmonitoredevents.nextevent
wscript.echo "a network connection has been lost:"
wscript.echo strlatestevent.instancename, now
wscript.echo
loop
调用方法示例:cscript 网络断开.vbs >> f:\test\微软脚本\log.txt
二、在网络硬件连接成功或网络故障恢复连接时发送警告
strcomputer = "."
set objwmiservice = getobject("winmgmts:" & strcomputer & " ootwmi")
set colmonitoredevents = objwmiservice.execnotificationquery _
("select * from msndis_statusmediaconnect")
do while true
set strlatestevent = colmonitoredevents.nextevent
wscript.echo "a network connection has been made:"
wscript.echo strlatestevent.instancename, now
wscript.echo
loop
调用方法示例:cscript 网络连接.vbs >> f:\test\微软脚本\log.txt
三、获取所有域用户信息
const ads_scope_subtree = 2
set objconnection = createobject("adodb.connection")
set objcommand = createobject("adodb.command")
objconnection.provider = "adsdsoobject"
objconnection.open "active directory provider"
set objcommand.activeconnection = objconnection
objcommand.commandtext = _
"select name, location from 'ldap://dc=domainname,dc=com' " _
& "where objectclass='computer'"
objcommand.properties("page size") = 1000
objcommand.properties("searchscope") = ads_scope_subtree
set objrecordset = objcommand.execute
objrecordset.movefirst
do until objrecordset.eof
wscript.echo "computer name: " & objrecordset.fields("name").value
wscript.echo "location: " & objrecordset.fields("location").value
objrecordset.movenext
loop
调用方法示例:cscript 域用户信息.vbs >> f:\test\微软脚本\域用户信息.txt
四、修改文本文件内容
const forreading = 1
const forwriting = 2
set objfso = createobject("scripting.filesystemobject")
set objtextfile = objfso.opentextfile("sample.ini", forreading)
do until objtextfile.atendofstream
strnextline = objtextfile.readline
intlinefinder = instr(strnextline, "username")
if intlinefinder <> 0 then
strnextline = "username=邀月工作室"
end if
strnewfile = strnewfile & strnextline & vbcrlf
loop
objtextfile.close
set objtextfile = objfso.opentextfile("sample.ini", forwriting)
objtextfile.writeline strnewfile
objtextfile.close
调用方法示例:modifyfile.vbs
附件:
sample.ini:
[oem install]
proggroupname=
defaultdestdir=
username=
usercompanyname=
userserialnumber=
五、通过脚本发送电子邮件
从安装了 smtp service 的计算机中发送电子邮件的脚本。
脚本代码
set objemail = createobject("cdo.message")
objemail.from = "monitor1@fabrikam.com"
objemail.to = "admin1@fabrikam.com"
objemail.subject = "atl-dc-01 down"
objemail.textbody = "atl-dc-01 is no longer accessible over the network."
objemail.send
调用方法示例:sendmail.vbs
六、在没有 smtp service 的条件下发送电子邮件
脚本设计用来在 microsoft 的公司网络上进行工作。
set objemail = createobject("cdo.message")
objemail.from = "admin1@fabrikam.com"
objemail.to = "admin2@fabrikam.com"
objemail.subject = "server down"
objemail.textbody = "server1 is no longer accessible over the network."
objemail.configuration.fields.item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objemail.configuration.fields.item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
"smarthost"
objemail.configuration.fields.item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objemail.configuration.fields.update
objemail.send
调用方法示例:sendmailnosmtp.vbs
七、将新的记录添加到数据库中
通过脚本检索计算机声卡的信息,然后将这些信息保存到带有 dsn inventory 的 ado 数据库中。
const adopenstatic = 3
const adlockoptimistic = 3
const aduseclient = 3
set objconnection = createobject("adodb.connection")
set objrecordset = createobject("adodb.recordset")
objconnection.open "dsn=inventory;"
objrecordset.cursorlocation = aduseclient
objrecordset.open "select * from hardware" , objconnection, _
adopenstatic, adlockoptimistic
set colsoundcards = getobject("winmgmts:").execquery _
("select * from win32_sounddevice")
for each objsoundcard in colsoundcards
objrecordset.addnew
objrecordset("computername") = objsoundcard.systemname
objrecordset("manufacturer") = objsoundcard.manufacturer
objrecordset("productname") = objsoundcard.productname
objrecordset.update
next
objrecordset.close
objconnection.close
调用方法示例:addonerecord.vbs
一、在网络硬件故障或网络故障断开时发送警告
复制代码 代码如下:
strcomputer = "."
set objwmiservice = getobject("winmgmts:" & strcomputer & " ootwmi")
set colmonitoredevents = objwmiservice.execnotificationquery _
("select * from msndis_statusmediadisconnect")
do while true
set strlatestevent = colmonitoredevents.nextevent
wscript.echo "a network connection has been lost:"
wscript.echo strlatestevent.instancename, now
wscript.echo
loop
调用方法示例:cscript 网络断开.vbs >> f:\test\微软脚本\log.txt
二、在网络硬件连接成功或网络故障恢复连接时发送警告
复制代码 代码如下:
strcomputer = "."
set objwmiservice = getobject("winmgmts:" & strcomputer & " ootwmi")
set colmonitoredevents = objwmiservice.execnotificationquery _
("select * from msndis_statusmediaconnect")
do while true
set strlatestevent = colmonitoredevents.nextevent
wscript.echo "a network connection has been made:"
wscript.echo strlatestevent.instancename, now
wscript.echo
loop
调用方法示例:cscript 网络连接.vbs >> f:\test\微软脚本\log.txt
三、获取所有域用户信息
复制代码 代码如下:
const ads_scope_subtree = 2
set objconnection = createobject("adodb.connection")
set objcommand = createobject("adodb.command")
objconnection.provider = "adsdsoobject"
objconnection.open "active directory provider"
set objcommand.activeconnection = objconnection
objcommand.commandtext = _
"select name, location from 'ldap://dc=domainname,dc=com' " _
& "where objectclass='computer'"
objcommand.properties("page size") = 1000
objcommand.properties("searchscope") = ads_scope_subtree
set objrecordset = objcommand.execute
objrecordset.movefirst
do until objrecordset.eof
wscript.echo "computer name: " & objrecordset.fields("name").value
wscript.echo "location: " & objrecordset.fields("location").value
objrecordset.movenext
loop
调用方法示例:cscript 域用户信息.vbs >> f:\test\微软脚本\域用户信息.txt
四、修改文本文件内容
复制代码 代码如下:
const forreading = 1
const forwriting = 2
set objfso = createobject("scripting.filesystemobject")
set objtextfile = objfso.opentextfile("sample.ini", forreading)
do until objtextfile.atendofstream
strnextline = objtextfile.readline
intlinefinder = instr(strnextline, "username")
if intlinefinder <> 0 then
strnextline = "username=邀月工作室"
end if
strnewfile = strnewfile & strnextline & vbcrlf
loop
objtextfile.close
set objtextfile = objfso.opentextfile("sample.ini", forwriting)
objtextfile.writeline strnewfile
objtextfile.close
调用方法示例:modifyfile.vbs
附件:
sample.ini:
复制代码 代码如下:
[oem install]
proggroupname=
defaultdestdir=
username=
usercompanyname=
userserialnumber=
五、通过脚本发送电子邮件
从安装了 smtp service 的计算机中发送电子邮件的脚本。
脚本代码
复制代码 代码如下:
set objemail = createobject("cdo.message")
objemail.from = "monitor1@fabrikam.com"
objemail.to = "admin1@fabrikam.com"
objemail.subject = "atl-dc-01 down"
objemail.textbody = "atl-dc-01 is no longer accessible over the network."
objemail.send
调用方法示例:sendmail.vbs
六、在没有 smtp service 的条件下发送电子邮件
脚本设计用来在 microsoft 的公司网络上进行工作。
复制代码 代码如下:
set objemail = createobject("cdo.message")
objemail.from = "admin1@fabrikam.com"
objemail.to = "admin2@fabrikam.com"
objemail.subject = "server down"
objemail.textbody = "server1 is no longer accessible over the network."
objemail.configuration.fields.item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objemail.configuration.fields.item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
"smarthost"
objemail.configuration.fields.item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objemail.configuration.fields.update
objemail.send
调用方法示例:sendmailnosmtp.vbs
七、将新的记录添加到数据库中
通过脚本检索计算机声卡的信息,然后将这些信息保存到带有 dsn inventory 的 ado 数据库中。
复制代码 代码如下:
const adopenstatic = 3
const adlockoptimistic = 3
const aduseclient = 3
set objconnection = createobject("adodb.connection")
set objrecordset = createobject("adodb.recordset")
objconnection.open "dsn=inventory;"
objrecordset.cursorlocation = aduseclient
objrecordset.open "select * from hardware" , objconnection, _
adopenstatic, adlockoptimistic
set colsoundcards = getobject("winmgmts:").execquery _
("select * from win32_sounddevice")
for each objsoundcard in colsoundcards
objrecordset.addnew
objrecordset("computername") = objsoundcard.systemname
objrecordset("manufacturer") = objsoundcard.manufacturer
objrecordset("productname") = objsoundcard.productname
objrecordset.update
next
objrecordset.close
objconnection.close
调用方法示例:addonerecord.vbs