几段非常有用的脚本(来自微软网站,由downmoon精心收集)
一、在网络硬件故障或网络故障断开时发送警告
复制代码 代码如下:
strComputer="."
SetobjWMIService=GetObject("winmgmts:"&strComputer&"ootwmi")
SetcolMonitoredEvents=objWMIService.ExecNotificationQuery_
("Select*fromMSNdis_StatusMediaDisconnect")
DoWhileTrue
SetstrLatestEvent=colMonitoredEvents.NextEvent
Wscript.Echo"Anetworkconnectionhasbeenlost:"
WScript.EchostrLatestEvent.InstanceName,Now
Wscript.Echo
Loop
调用方法示例:cscript网络断开.vbs>>F:test微软脚本log.txt
二、在网络硬件连接成功或网络故障恢复连接时发送警告
复制代码 代码如下:
strComputer="."
SetobjWMIService=GetObject("winmgmts:"&strComputer&"ootwmi")
SetcolMonitoredEvents=objWMIService.ExecNotificationQuery_
("Select*fromMSNdis_StatusMediaConnect")
DoWhileTrue
SetstrLatestEvent=colMonitoredEvents.NextEvent
Wscript.Echo"Anetworkconnectionhasbeenmade:"
WScript.EchostrLatestEvent.InstanceName,Now
Wscript.Echo
Loop
调用方法示例:cscript网络连接.vbs>>F:test微软脚本log.txt
三、获取所有域用户信息
复制代码 代码如下:
ConstADS_SCOPE_SUBTREE=2
SetobjConnection=CreateObject("ADODB.Connection")
SetobjCommand=CreateObject("ADODB.Command")
objConnection.Provider="ADsDSOObject"
objConnection.Open"ActiveDirectoryProvider"
SetobjCOmmand.ActiveConnection=objConnection
objCommand.CommandText=_
"SelectName,Locationfrom'LDAP://DC=DomainName,DC=com'"_
&"WhereobjectClass='computer'"
objCommand.Properties("PageSize")=1000
objCommand.Properties("Searchscope")=ADS_SCOPE_SUBTREE
SetobjRecordSet=objCommand.Execute
objRecordSet.MoveFirst
DoUntilobjRecordSet.EOF
Wscript.Echo"ComputerName:"&objRecordSet.Fields("Name").Value
Wscript.Echo"Location:"&objRecordSet.Fields("Location").Value
objRecordSet.MoveNext
Loop
调用方法示例:cscript域用户信息.vbs>>F:test微软脚本域用户信息.txt
四、修改文本文件内容
复制代码 代码如下:
ConstForReading=1
ConstForWriting=2
SetobjFSO=CreateObject("Scripting.FileSystemObject")
SetobjTextFile=objFSO.OpenTextFile("sample.ini",ForReading)
DoUntilobjTextFile.AtEndOfStream
strNextLine=objTextFile.Readline
intLineFinder=InStr(strNextLine,"UserName")
IfintLineFinder<>0Then
strNextLine="UserName=邀月工作室"
EndIf
strNewFile=strNewFile&strNextLine&vbCrLf
Loop
objTextFile.Close
SetobjTextFile=objFSO.OpenTextFile("sample.ini",ForWriting)
objTextFile.WriteLinestrNewFile
objTextFile.Close
调用方法示例:ModifyFile.vbs
附件:
Sample.ini:
复制代码 代码如下:
[OEMInstall]
ProgGroupName=
DefaultDestDir=
UserName=
UserCompanyName=
UserSerialNumber=
五、通过脚本发送电子邮件
从安装了SMTPService的计算机中发送电子邮件的脚本。
脚本代码
复制代码 代码如下:
SetobjEmail=CreateObject("CDO.Message")
objEmail.From="monitor1@fabrikam.com"
objEmail.To="admin1@fabrikam.com"
objEmail.Subject="Atl-dc-01down"
objEmail.Textbody="Atl-dc-01isnolongeraccessibleoverthenetwork."
objEmail.Send
调用方法示例:SendMail.vbs
六、在没有SMTPService的条件下发送电子邮件
脚本设计用来在Microsoft的公司网络上进行工作。
复制代码 代码如下:
SetobjEmail=CreateObject("CDO.Message")
objEmail.From="admin1@fabrikam.com"
objEmail.To="admin2@fabrikam.com"
objEmail.Subject="Serverdown"
objEmail.Textbody="Server1isnolongeraccessibleoverthenetwork."
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
七、将新的记录添加到数据库中
通过脚本检索计算机声卡的信息,然后将这些信息保存到带有DSNInventory的ADO数据库中。
复制代码 代码如下:
ConstadOpenStatic=3
ConstadLockOptimistic=3
ConstadUseClient=3
SetobjConnection=CreateObject("ADODB.Connection")
SetobjRecordset=CreateObject("ADODB.Recordset")
objConnection.Open"DSN=Inventory;"
objRecordset.CursorLocation=adUseClient
objRecordset.Open"SELECT*FROMHardware",objConnection,_
adOpenStatic,adLockOptimistic
SetcolSoundCards=GetObject("winmgmts:").ExecQuery_
("Select*fromWin32_SoundDevice")
ForEachobjSoundCardincolSoundCards
objRecordset.AddNew
objRecordset("ComputerName")=objSoundCard.SystemName
objRecordset("Manufacturer")=objSoundCard.Manufacturer
objRecordset("ProductName")=objSoundCard.ProductName
objRecordset.Update
Next
objRecordset.Close
objConnection.Close
调用方法示例:AddOneRecord.vbs