常用脚本实例:(以下都使用VBScript)
(主要讲解脚本宿主和脚本运行时,脚本创建com和脚本引擎的扩展先不讲,以后再讲)
1
Creating and Controlling Files with FSO (Scripting.FileSystemObject)创建folder且collect 错误
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' NewFolderEC.vbs
' Free example VBScript to create a folder with error-correcting Code.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.6 - May 2005
' ---------------------------------------------------------------'
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Option Explicit
Dim objFSO, objFolder, objShell, strDirectory
strDirectory = "c:\logs"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' Note If..Exists. Then, Else
End If construction
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
WScript.Echo strDirectory & " already created "
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" &" " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
WScript.Quit
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' End of Sample VBScript to create a folder with error-correcting Code
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
创建文件且检查文件是否已经存在
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' NewFileEC.vbs
' Sample VBScript to create a file with error-correcting Code
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.6 - June 2005
' ---------------------------------------------------------------'
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Option Explicit
Dim objFSO, objFolder, objShell, objFile
Dim strDirectory, strFile
strDirectory = "e:\logs"
strFile = "\Summer.txt"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
set objFolder = nothing
set objFile = nothing
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" & " " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
WScript.Quit
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' End of VBScript to create a file with error-correcting Code
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
文件的末尾增加内容
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' NewTextEC.vbs
' Sample VBScript to write to a file. With added error-correcting
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.5 - August 2005
' ---------------------------------------------------------------'
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile
Dim strDirectory, strFile, strText
strDirectory = "e:\logs3"
strFile = "\Summer.txt"
strText = "Book Another Holiday"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' Check that the strDirectory folder exists
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
Wscript.Echo "Just created " & strDirectory & strFile
End If
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
set objFile = nothing
set objFolder = nothing
' OpenTextFile Method needs a Const value
' ForAppending = 8 ForReading = 1, ForWriting = 2
Const ForAppending = 8
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' Writes strText every time you run this VBScript
objTextFile.WriteLine(strText)
objTextFile.Close
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' Bonus or cosmetic section to launch explorer to check file
If err.number = vbEmpty then
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" &" " & strDirectory & "\" )
Else WScript.echo "VBScript Error: " & err.number
End If
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
WScript.Quit
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' End of VBScript to write to a file with error-correcting Code
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
写log data到文件
' EventLogFSOvbs
' Example VBScript to interogate the Event Log and create a file
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.8 - June 2005
' ---------------------------------------------------------'
Option Explicit
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Dim objFso, objFolder, objWMI, objItem, objShell, strEventLog
Dim strFile, strComputer, strFolder, strFileName, strPath
Dim intEvent, intNumberID, intRecordNum, colLoggedEvents
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' --------------------------------------------------------
' Set the folder and file name
' Set numbers
intNumberID = 17 ' Event ID Number
intRecordNum = 0
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
strComputer = "."
strFileName = "\Event" & intNumberID & ".txt"
strFolder = "e:\logs\eventlog"
strPath = strFolder & strFileName
strEventLog = "'System' "
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' -----------------------------------------------------
' Section to create folder and hold file.
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolder) Then
Set objFolder = objFSO.GetFolder(strFolder)
Else
Set objFolder = objFSO.CreateFolder(strFolder)
Wscript.Echo "Folder created " & strFolder
End If
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Wscript.Echo " Press OK and Wait 30 seconds (ish)"
Set strFile = objFso.CreateTextFile(strPath, True)
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colLoggedEvents = objWMI.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = " & strEventLog)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' -----------------------------------------
' Next section loops through ID properties
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
For Each objItem in colLoggedEvents
If objItem.EventCode = intNumberID Then
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' Second Loop to filter only if they tried Administrator
strFile.WriteLine("Category: " & objItem.Category _
& " string " & objItem.CategoryString)
strFile.WriteLine("ComputerName: " & objItem.ComputerName)
strFile.WriteLine("Logfile: " & objItem.Logfile _
& " source " & objItem.SourceName)
strFile.WriteLine("EventCode: " & objItem.EventCode)
strFile.WriteLine("EventType: " & objItem.EventType)
strFile.WriteLine("Type: " & objItem.Type)
strFile.WriteLine("User: " & objItem.User)
strFile.WriteLine("Message: " & objItem.Message)
strFile.WriteLine (" ")
intRecordNum = intRecordNum +1
End If
Next
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' Confirms the script has completed and opens the file
Set objShell = CreateObject("WScript.Shell")
objShell.run ("Explorer" &" " & strPath & "\" )
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
WScript.Quit
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' End of Guy's Sample FSO VBScript
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
拷贝文件
' VBScript.
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFile "c:\COMPlusLog.txt", "c:\x\"
拷贝文件夹
' VBScript.
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFolder "c:\x", "c:\y"
2 读写注册表
' VBScript.
Set Sh = CreateObject("WScript.Shell")
key = "HKEY_CURRENT_USER\"
Sh.RegWrite key & "WSHTest\", "testkeydefault"
Sh.RegWrite key & "WSHTest\string1", "testkeystring1"
Sh.RegWrite key & "WSHTest\string2", "testkeystring2", "REG_SZ"
Sh.RegWrite key & "WSHTest\string3", "testkeystring3", "REG_EXPAND_SZ"
Sh.RegWrite key & "WSHTest\int", 123, "REG_DWORD"
WScript.Echo Sh.RegRead(key & "WSHTest\")
WScript.Echo Sh.RegRead(key & "WSHTest\string1")
WScript.Echo Sh.RegRead(key & "WSHTest\string2")
WScript.Echo Sh.RegRead(key & "WSHTest\string3")
WScript.Echo Sh.RegRead(key & "WSHTest\int")
Sh.RegDelete key & "WSHTest\"
3 创建快捷方式
VBScript.
Dim Shell, DesktopPath, URL
Set Shell = CreateObject("WScript.Shell")
DesktopPath = Shell.SpecialFolders("Desktop")
Set URL = Shell.CreateShortcut(DesktopPath & "\MSDN Scripting.URL")
URL.TargetPath = "HTTP://MSDN.Microsoft.com/scripting/"
URL.Save
' VBScript.
Set Shell = CreateObject("WScript.Shell")
DesktopPath = Shell.SpecialFolders("Desktop")
Set link = Shell.CreateShortcut(DesktopPath & "\test.lnk")
link.Arguments = "1 2 3"
link.Description = "test shortcut"
link.HotKey = "CTRL+ALT+SHIFT+X"
link.IconLocation = "app.exe,1"
link.TargetPath = "c:\blah\app.exe"
link.WindowStyle = 3
link.WorkingDirectory = "c:\blah"
link.Save
以下主要使用WMI
4 列举系统进程
' Process.vbs
' Free Sample VBScript to discover which processes are running
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.4 - December 2005
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strList
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
strComputer = "."
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
For Each objProcess in colProcess
strList = strList & vbCr & _
objProcess.Name
Next
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
WSCript.Echo strList
WScript.Quit
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' End of List Process Example VBScript
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
看看它的结果吗:
![r_listprocess.jpg](http://www.cppblog.com/images/cppblog_com/mzty/2021/r_listprocess.jpg)
start stop 进程
' StartProcessLocal.vbs
' Free example VBScript to start a process (not interactive)
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.8 - December 2005
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objProcess, objCalc
Dim strShell, objProgram, strComputer, strExe
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
strComputer = "."
strExe = "Calc.exe"
' Connect to WMI
set objWMIService = getobject("winmgmts://"_
& strComputer & "/root/cimv2")
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' Obtain the Win32_Process class of object.
Set objProcess = objWMIService.Get("Win32_Process")
Set objProgram = objProcess.Methods_( _
"Create").InParameters.SpawnInstance_
objProgram.CommandLine = strExe
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
'Execute the program now at the command line.
Set strShell = objWMIService.ExecMethod( _
"Win32_Process", "Create", objProgram)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
WScript.echo "Created: " & strExe & " on " & strComputer
WSCript.Quit
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' End of free example of a Process VBScript
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' ProcessKillLocal.vbs
' Sample VBScript to kill a program
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.7 - December 2005
' ------------------------ -------------------------------'
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill
strComputer = "."
strProcessKill = "'calc.exe'"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next
WSCript.Echo "Just killed process " & strProcessKill _
& " on " & strComputer
WScript.Quit
' End of WMI Example of a Kill Process
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
物理和逻辑disks
' LogicalDisk.vbs
' Sample VBScript to interrogate a Logical disk with WMI
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.8 - November 2005
' -------------------------------------------------------------'
Option Explicit
Dim objWMIService, objItem, colItems, strComputer
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
On Error Resume Next
strComputer = "."
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
For Each objItem in colItems
Wscript.Echo "Computer: " & objItem.SystemName & VbCr & _
" ==================================" & VbCr & _
"Drive Letter: " & objItem.Name & vbCr & _
"Description: " & objItem.Description & vbCr & _
"Volume Name: " & objItem.VolumeName & vbCr & _
"Drive Type: " & objItem.DriveType & vbCr & _
"Media Type: " & objItem.MediaType & vbCr & _
"VolumeSerialNumber: " & objItem.VolumeSerialNumber & vbCr & _
"Size: " & Int(objItem.Size /1073741824) & " GB" & vbCr & _
"Free Space: " & Int(objItem.FreeSpace /1073741824) & _
" GB" & vbCr & _
"Quotas Disabled: " & objItem.QuotasDisabled & vbCr & _
"Supports DiskQuotas: " & objItem.SupportsDiskQuotas & vbCr & _
"Supports FileBasedCompression: " & _
objItem.SupportsFileBasedCompression & vbCr & _
"Compressed: " & objItem.Compressed & vbCr & _
""
Next
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
WSCript.Quit
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' End of Sample DiskDrive VBScript
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' DiskDriveLogical.vbs
' Sample VBScript to interrogate a physical disk with WMI
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.3 - November 2005
' --------------------------------------------------------------'
Option Explicit
Dim objWMIService, objItem, colItems, strComputer
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' On Error Resume Next
strComputer = "."
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(_
"Select * from Win32_DiskDrive")
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
For Each objItem in colItems
Wscript.Echo "Computer: " & objItem.SystemName & VbCr & _
"Status: " & objItem.Status & VbCr & _
" ==================================" & VbCr & _
"Name: " & objItem.Name & VbCr & _
"Description: " & objItem.Description & VbCr & _
"Signature: " & objItem.Signature & VbCr & _
"Manufacturer: " & objItem.Manufacturer & VbCr & _
"Model: " & objItem.Model & VbCr & _
"Size: " & Int(objItem.Size /(1073741824)) & " GB" & VbCr & _
"Number of Partitions: " & objItem.Partitions & VbCr & _
"Total Cylinders: " & objItem.TotalCylinders & VbCr & _
"Tracks PerCylinder: " & objItem.TracksPerCylinder & VbCr & _
"Total Heads: " & objItem.TotalHeads & VbCr & _
"Total Sectors: " & objItem.TotalSectors & VbCr & _
"Bytes PerSector: " & objItem.BytesPerSector & VbCr & _
"Sectors PerTrack: " & objItem.SectorsPerTrack & VbCr & _
"Total Tracks: " & objItem.TotalTracks & VbCr & _
" -------- SCSI Info ---------- "& VbCr & _
"SCSI TargetId: " & objItem.SCSITargetId & VbCr & _
"SCSI Bus: " & objItem.SCSIBus & VbCr & _
"SCSI Logical Unit: " & objItem.SCSILogicalUnit & VbCr & _
"SCSI Port: " & objItem.SCSIPort
Next
WSCript.Quit
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' End of Sample DiskDrive VBScript
获得memory
' Memory.vbs
' Sample VBScript to discover how much RAM in computer
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - August 2005
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objComputer, colComputer
Dim strLogonUser, strComputer
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
strComputer = "."
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery _
("Select * from Win32_ComputerSystem")
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
For Each objComputer in colComputer
Wscript.Echo "System Name: " & objComputer.Name _
& vbCr & "Total RAM " & objComputer.TotalPhysicalMemory
Next
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
WScript.Quit
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
' End of free example of Memory WMI / VBScript
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
打开百度:
Dim objIE
Set objIE = WScript.CreateObject ("InternetExplorer.Application")
objIE.AddressBar = true
objIE.Visible = true
objIE.Navigate("www.baidu.com")
删除你制定的services:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name = 'DbService'")
For Each objService in colListOfServices
objService.StopService()
objService.Delete()
Next
internet 和server active directory 还有创建com等没有讲哦!
主要参考:
MS的document
http://computerperformance.co.uk/