A few very useful scripts (from Microsoft website, carefully collected by Downmoon)
A. Send a warning when a network hardware failure or network failure is broken
Copy Code code as follows:
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
Call Method Example: Cscript network disconnected. vbs >> F:\test\ Microsoft Script \log.txt
Second, send a warning when a network hardware connection is successful or a network recovery connection is made
Copy Code code as follows:
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
Call Method Example: Cscript network connection. VBS >> F:\test\ Microsoft Script \log.txt
Third, obtain all domain user information
Copy Code code as follows:
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
Call Method Example: Cscript domain user information. vbs >> F:\test\ Microsoft script \ domain user information. txt
Iv. Modifying the contents of a text file
Copy Code code as follows:
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= invited to the Moon Studio"
End If
strNewFile = strNewFile & Strnextline & VbCrLf
Loop
Objtextfile.close
Set objTextFile = objFSO.OpenTextFile ("Sample.ini", ForWriting)
objTextFile.WriteLine strNewFile
Objtextfile.close
Invoke Method Example: Modifyfile.vbs
Attachment:
Sample.ini:
Copy Code code as follows:
[OEM Install]
Proggroupname=
Defaultdestdir=
Username=
Usercompanyname=
Userserialnumber=
V. Send e-mail via script
A script that sends e-mail messages from the computer where the SMTP Service is installed.
Scripting code
Copy Code code as follows:
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
Invoke Method Example: Sendmail.vbs
Vi. send e-mail without the SMTP Service
Scripting is designed to work on a Microsoft corporate network.
Copy Code code as follows:
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
Invoke Method Example: Sendmailnosmtp.vbs
Add new records to the database
Retrieve information about your computer's sound card through a script, and then save that information in an ADO database with DSN Inventory.
Copy Code code as follows:
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
Invoke Method Example: Addonerecord.vbs