分享VBS技术Sharing VBScripting T…
来源:互联网 发布:淘宝网 等级 编辑:程序博客网 时间:2024/06/05 18:42
具体用法:新建一个txt文档,将脚本保存在其中,然后将文件类型保存为.VBS,点击运行。
Adding Elements to aDictionary
Demonstration scrīpt that adds threekey-item pairs to a scrīpt Runtime Dictionary. scrīpt must be runon the local computer.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline"objDictionary.Add "Printer 3", "Printing"
Creating an Instance of InternetExplorer
Demonstration scrīpt that creates aninstance of Internet Explorer, opened to a blank page.
Set ōbjExplorer = Wscrīpt.CreateObject("InternetExplorer.Application")objExplorer.Navigate "about:blank" objExplorer.ToolBar = 0objExplorer.StatusBar = 0objExplorer.Width=300objExplorer.Height = 150 objExplorer.Left = 0objExplorer.Top = 0objExplorer.Visible = 1
Creating scrīpt DocumentationUsing scrīpt Comments
Demonstrates the use of the FileSystemObjectas a way to copy comments from a scrīpt to a separate text file.Requires comments to have been marked using '*.
Const ForReading = 1Const ForWriting = 2Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")Set ōbjscrīptFile = objFSO.OpenTextFile("c:\scrīpts\Service_Monitor.vbs", _ ForReading)Set ōbjCommentFile = objFSO.OpenTextFile("c:\scrīpts\Comments.txt", _ ForWriting, TRUE)Do While objscrīptFile.AtEndOfStream <> TRUE strCurrentLine = objscrīptFile.ReadLine intIsComment = Instr(1,strCurrentLine,"'*") If intIsComment > 0 Then objCommentFile.Write strCurrentLine & VbCrLf End IfLoopobjscrīptFile.CloseobjCommentFile.Close
Determining the Number of Items in aDictionary
Demonstration scrīpt that counts the numberof key-item pairs in a scrīpt Runtime Dictionary. scrīpt must berun on the local computer.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline"objDictionary.Add "Printer 3", "Printing"Wscrīpt.Echo objDictionary.Count
Displaying Real Time Events in aCommand Window
Creates a temporary event consumer thatmonitors the event log for error events. When an error eventoccurs, the scrīpt displays the event information in the commandwindow.
strComputer = "."Set ōbjWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate,(Security)}!\\" & _ strComputer & "\root\cimv2")Set colMonitoredEvents = objWMIService.ExecNotificationQuery _ ("Select * from __InstanceCreationEvent within 5 where TargetInstance isa " _ & "'Win32_NTLogEvent' and TargetInstance.EventType = '1'")Do Set ōbjLatestEvent = colMonitoredEvents.NextEvent Wscrīpt.Echo "Record No.: " & _ objLatestEvent.TargetInstance.RecordNumber Wscrīpt.Echo "Event ID: " & objLatestEvent.TargetInstance.EventCode Wscrīpt.Echo "Time: " & objLatestEvent.TargetInstance.TimeWritten Wscrīpt.Echo "Source: " & objLatestEvent.TargetInstance.SourceName Wscrīpt.Echo "Category: " & _ objLatestEvent.TargetInstance.CategoryString Wscrīpt.Echo "Event Type: " & objLatestEvent.TargetInstance.Type Wscrīpt.Echo "Computer: " & _ objLatestEvent.TargetInstance.ComputerName Wscrīpt.Echo "User: " & objLatestEvent.TargetInstance.User Wscrīpt.echo "Text: " & objLatestEvent.TargetInstance.MessageLoop
Displaying Tabular Output in a CommandWindow
Retrieves service data from a computer, andthen outputs that data in tabular format in a commandwindow.
Set colServices = GetObject("winmgmts:"). _ ExecQuery("Select * from Win32_Service")For Each objService in colServices intPadding = 50 - Len(objService.DisplayName) intPadding2 = 17 - Len(objService.StartMode) strDisplayName = objService.DisplayName & Space(intPadding) strStartMode = objService.StartMode & Space(intPadding2) Wscrīpt.Echo strDisplayName & strStartMode & objService.State Next
Masking Command Line Passwords
Demonstration scrīpt that uses scrīptPW.dllto mask passwords entered at the command line.
Set ōbjPassword = CreateObject("scrīptPW.Password") Wscrīpt.StdOut.Write "Please enter your password:" strPassword = objPassword.GetPassword() Wscrīpt.EchoWscrīpt.Echo "Your password is: " & strPassword
Masking Passwords Using InternetExplorer
Demonstration scrīpt that creates aninstance of Internet Explorer, and retrieves a password typed intoa password-style text box. Requires a Web page named password.htmwith the appropriate text box.
Set ōbjExplorer = Wscrīpt.CreateObject _ ("InternetExplorer.Application", "IE_")objExplorer.Navigate "file:///c:\scrīpts\password.htm" objExplorer.Visible = 1 objExplorer.ToolBar = 0objExplorer.StatusBar = 0objExplorer.Width=400objExplorer.Height = 250 objExplorer.Left = 0objExplorer.Top = 0Do While (objExplorer.Document.Body.All.OKClicked.Value = "") Wscrīpt.Sleep 250 Loop strPassword = objExplorer.Document.Body.All.PasswordBox.ValueobjExplorer.QuitWscrīpt.Sleep 250Wscrīpt.Echo strPassword
Removing All Elements from aDictionary
Demonstration scrīpt that deletes all thekey-item pairs from a scrīpt Runtime Dictionary. scrīpt must berun on the local computer.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline"objDictionary.Add "Printer 3", "Printing"colKeys = objDictionary.KeysWscrīpt.Echo "First run: "For Each strKey in colKeys Wscrīpt.Echo strKeyNextobjDictionary.RemoveAllcolKeys = objDictionary.KeysWscrīpt.Echo VbCrLf & "Second run: "For Each strKey in colKeys Wscrīpt.Echo strKeyNext
Removing Debugging Comments
Demonstrates the use of the FileSystemObjectas a way to remove debugging comments from a scrīpt. Requirescomments to have been marked as '* BUG.
Const ForReading = 1Const ForWriting = 2Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")Set ōbjTextFile = objFSO.OpenTextFile("C:\scrīpts\CreateUser.vbs", ForReading) Do While objTextFile.AtEndOfStream <> true strNextLine = objTextFile.Readline intCheckForBugComment = Instr(strNextLine, "'* BUG") If intCheckForBugComment = 0 Then strSavedLines = strSavedLines & strNextLine & VbCrLf End IfLoop Set ōbjTextFile = objFSO.OpenTextFile _ ("c:\scrīpts\CreateUser.vbs ", ForWriting)objTextFile.Write strSavedLines objTextFile.Close
Removing One Element from aDictionary
Demonstration scrīpt that deletes a specifickey-item pair from a scrīpt Runtime Dictionary. scrīpt must berun on the local computer.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline"objDictionary.Add "Printer 3", "Printing"colKeys = objDictionary.KeysWscrīpt.Echo "First run: " For Each strKey in colKeys Wscrīpt.Echo strKeyNext objDictionary.Remove("Printer 2")colKeys = objDictionary.KeysWscrīpt.Echo VbCrLf & "Second run: " For Each strKey in colKeys Wscrīpt.Echo strKeyNext
Retrieving Command Line Argumentsfrom an Active Directory Container
Demonstration scrīpt that retrieves thenames of all the computers in an Active Directory container, andthen returns service information from each of thosecomputers.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")i = 0Set ōbjOU = GetObject("LDAP://CN=Computers, DC=fabrikam, DC=com")objOU.Filter = Array("Computer")For Each objComputer in objOU objDictionary.Add i, objComputer.CN i = i + 1NextFor Each objItem in objDictionary Set colServices = GetObject("winmgmts://" & _ objDictionary.Item(objItem) _ & "").ExecQuery("Select * from Win32_Service") Wscrīpt.Echo colServices.CountNext
Retrieving Command Line Argumentsfrom a Text File
Demonstration scrīpt that opens ahypothetical text file consisting of server names, then retrievesservice information from each on the servers in thefile.
Const ForReading = 1Set ōbjDictionary = CreateObject("scrīpting.Dictionary")Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")Set ōbjTextFile = objFSO.OpenTextFile("c:\scrīpts\servers.txt", ForReading)i = 0Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline objDictionary.Add i, strNextLine i = i + 1LoopFor Each objItem in objDictionary Set colServices = GetObject("winmgmts://" & _ objDictionary.Item(objItem) _ & "").ExecQuery("Select * from Win32_Service") Wscrīpt.Echo colServices.CountNext
Retrieving a Web Page
Retrieves the HTML source for the Web pagehttp://www.microsoft.com. This scrīpt contributed by Maxim Stepinof Microsoft.
url="http://www.microsoft.com"Set ōbjHTTP = CreateObject("MSXML2.XMLHTTP")Call objHTTP.Open("GET", url, FALSE)objHTTP.SendWscrīpt.Echo(objHTTP.ResponseText)
Saving Data inXML Format
Demonstration scrīpt that retrieves serviceinformation for a computer, and then saves that data as an XMLfile.
Const ForAppending = 2Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")Set ōbjTextFile = objFSO.OpenTextFile _ ("c:\scrīpts\service_status.xml", ForAppending, True)objTextFile.WriteLine ""objTextFile.Write ""objTextFile.WriteLine ""strComputer = "."Set colServices = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"). _ ExecQuery("Select * from Win32_Service")For Each objService in colServices objTextFile.WriteLine "" objTextFile.WriteLine "" objTextFile.WriteLine objService.DisplayName objTextFile.WriteLine "" objTextFile.WriteLine "" objTextFile.WriteLine objService.State objTextFile.WriteLine "" objTextFile.WriteLine ""NextobjTextFile.WriteLine ""objTextFile.Close
Sorting WMIData
Demonstration scrīpt showing how WMI datacan be sorted using a disconnected recordset (by itself, WMI doesnot allow you to specify a sort order for returned data). In thisscrīpt, service information is retrieved using WMI and is storedin a disconnected recordset, a recordset that is not tied to aphysical data source. The Sort method is then used to sort theservice data by service state rather than by servicename.
Const adVarChar = 200Const MaxCharacters = 255 Set DataList = CreateObject("ADOR.Recordset")DataList.Fields.Append "ServiceName", adVarChar, MaxCharactersDataList.Fields.Append "ServiceState", adVarChar, MaxCharactersDataList.Open strComputer = "."Set ōbjWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")Set ServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service") For Each Service in ServiceList DataList.AddNew DataList("ServiceName") = Service.Name DataList("ServiceState") = Service.State DataList.UpdateNext DataList.Sort = "ServiceState"DataList.MoveFirst Do Until DataList.EOF Wscrīpt.Echo DataList.Fields.Item("ServiceName") _ & vbTab & DataList.Fields.Item("ServiceState") DataList.MoveNextLoop
SuppressingMultiple Event Notifications
Issues an alert if available space on a diskdrive falls below 100 megabytes. Will wait one hour before issuingthe next alert.
dtmStartTime = NowstrComputer = "."Set ōbjWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set ōbjDiskDrives = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk")For Each objDrive in objDiskDrives If objDrive.FreeSpace < 10000000 Then Wscrīpt.Echo "Drive is low on disk space." End IfNextDoSet ōbjDiskDrives = objWMIService.ExecQuery _ ("Select * from Win32_LogicalDisk")For Each objDrive in objDiskDrives If objDrive.FreeSpace < 10000000 Then intElapsedHours = DateDiff("h", dtmStartTime, Now) If intElapsedHours >= 1 Then Wscrīpt.Echo "Drive is low on disk space." dtmStartTime = Now End If End IfNextWscrīpt.Sleep 1000Loop
Trackingscrīpt Progress in a Command Window
Demonstrates the use of StdOut as a methodfor indicating the progress being made by a scrīpt.
Wscrīpt.Echo "Processing information. This might take several minutes."strComputer = "."Set colServices = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"). _ ExecQuery("Select * from Win32_Service")For Each objService in colServices Wscrīpt.StdOut.Write(".")NextWscrīpt.StdOut.WriteLineWscrīpt.Echo "Service information processed."
Tracking scrīptProgress Using Internet Explorer
Demonstrates how to use Internet Explorer asa method for indicating the progress being made by ascrīpt.
Set ōbjExplorer = Wscrīpt.CreateObject("InternetExplorer.Application")objExplorer.Navigate "about:blank" objExplorer.ToolBar = 0objExplorer.StatusBar = 0objExplorer.Width=400objExplorer.Height = 200 objExplorer.Left = 0objExplorer.Top = 0Do While (objExplorer.Busy) Wscrīpt.Sleep 200Loop objExplorer.Visible = 1 objExplorer.Document.Body.InnerHTML = "Retrieving service information. " _ & "This might take several minutes to complete."strComputer = "."Set colServices = GetObject("winmgmts: \\" & strComputer & "\root\cimv2"). _ ExecQuery("Select * from Win32_Service")For Each objService in colServices Wscrīpt.Sleep 200NextobjExplorer.Document.Body.InnerHTML = "Service information retrieved."Wscrīpt.Sleep 3000Wscrīpt.Quit
Using a Text File as aCommand Line Argument
Demonstration scrīpt that allows you to draga text file (consisting of server names) onto the scrīpt icon inWindows Explorer. Thescrīpt then opens the text file, then retrieves serviceinformation from each on the servers in the file.
Set ōbjArgs = Wscrīpt.ArgumentsConst ForReading = 1Set ōbjDictionary = CreateObject("scrīpting.Dictionary")Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")Set ōbjTextFile = objFSO.OpenTextFile(objArgs(0), ForReading)i = 0 Do While objTextFile.AtEndOfStream <> True strNextLine = objTextFile.Readline objDictionary.Add i, strNextLine i = i + 1Loop For Each objItem in objDictionary Set colServices = GetObject("winmgmts://" & objDictionary.Item(objItem) _ & "").ExecQuery("Select * from Win32_Service") Wscrīpt.Echo colServices.CountNext
Verifying theExistence of a Dictionary Key
Demonstration scrīpt that verifies theexistence of a particular key within a scrīpt Runtime Dictionary.scrīpt must be run on the local computer.
Set ōbjDictionary = CreateObject("scrīpting.Dictionary")objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline"objDictionary.Add "Printer 3", "Printing"If objDictionary.Exists("Printer 4") Then Wscrīpt.Echo "Printer 4 is in the Dictionary."Else Wscrīpt.Echo "Printer 4 is not in the Dictionary."End If
- 分享VBS技术Sharing VBScripting T…
- 10055 - Hashmat t…
- resource is out of sync with t…
- Weibo Sharing Icon
- ADB server didn't ACK * failed t…
- Locally vs. Dictionary Managed T…
- An interesting website related t…
- can't call a non static method f…
- DB2 Basics: Fun with Dates and T…
- Integrated Security =T…
- 【原创】mysql update t…
- VBS VBScript简介
- Lesson 85 Never t…
- Lesson 62 After t…
- nothing to say, t…
- Failed to write t…
- Error (10028): Can't resol…
- Ansoft HFSS - http://t.cn/zQclhN…
- 二叉树11个简单算法
- 二叉树的定义和性质(C语言)
- QTP 取DataTable 列名 的脚本
- QTP打开网址和文件的语句
- 描述编程中的Description对象
- 分享VBS技术Sharing VBScripting T…
- VS2008 无法在web服务器上启动调试 基础连接已经关闭 发送时发生错误
- linux下查看最消耗CPU、内存的进程
- [转]Scripts to manage Text Files
- 如何解决返回数据库查询纪录条数的…
- QTP在EXCEL里面添加超联接的方法
- QTP Action级别的参数调用
- [转]QTP处理定时运行程序的办法
- qtp 使用Oracle数据库做测试数据驱…