Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Summarize a few very useful scripts

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "summing up a few very useful scripts". Interested friends might as well take a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "sum up a few very useful scripts"!

First, send a warning when a network hardware failure or network failure disconnects

The copy code is 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

Example of calling method: cscript network disconnected. VBS > > F:\ test\ Microsoft script\ log.txt

Send a warning when the network hardware is connected successfully or when the connection is restored by a network failure

The copy code is 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

Example of calling method: cscript network connection .vbs > > F:\ test\ Microsoft script\ log.txt

3. Get all domain user information

The copy code is 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

Example of calling method: cscript domain user information .vbs > > F:\ test\ Microsoft script\ domain user information .txt

IV. Modify the contents of the text file

The copy code is 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= Moon Studio"

End If

StrNewFile = strNewFile & strNextLine & vbCrLf

Loop

ObjTextFile.Close

Set objTextFile = objFSO.OpenTextFile ("sample.ini", ForWriting)

ObjTextFile.WriteLine strNewFile

ObjTextFile.Close

Example of calling method: ModifyFile.vbs

Attachment:

Sample.ini:

The copy code is as follows:

[OEM Install]

ProgGroupName=

DefaultDestDir=

UserName=

UserCompanyName=

UserSerialNumber=

Send e-mail through script

A script that sends e-mail messages from a computer where SMTP Service is installed.

The copy code is 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

Example of calling method: SendMail.vbs

6. Send email without SMTP Service

The script is designed to work on Microsoft's corporate network.

The copy code is 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

Example of calling method: SendMailNoSMTP.vbs

Add new records to the database

Retrieve the information from your computer's sound card through a script, and then save the information to an ADO database with DSN Inventory.

The copy code is 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

Example of calling method: AddOneRecord.vbs

At this point, I believe you have a deeper understanding of "summing up a few very useful scripts". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report