-
Do While...Loop
2010-03-23 14:14:43
Do While...Loop
The Do While...Loop command enables you to run a script. as long as a certain condition is in effect
Example:MonitorForChangedDiskSpace.vbs
'Header information section,define the variabl and on error resume Next
Option Explicit
On Error Resume Next
Dim colMonitoredDisks
Dim objWMIService
Dim objDiskChange
Dim strComputer
Dim startTime 'snapTime used for timer Function'reference information secton
Const LOCAL_HARD_DISK = 3 'the driveType value from SDK
Const RUN_TIME = 1000 'time to allow the script. to run in seconds
strComputer = "."
startTime = Timer'Worker and Output Information section
Set bjWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colMonitoredDisks = objWMIService.ExecNotificationQuery _
("Select * from __instancemodificationevent within 10 where " _
& "TargetInstance ISA 'Win32_LogicalDisk'")
Do While True
snapTime = Timer
Set bjDiskChange = colMonitoredDisks.NextEvent
If objDiskChange.TargetInstance.DriveType = LOCAL_HARD_DISK Then
WScript.echo "diskSpace on " &_
objDiskChange.TargetInstance.deviceID &_
" has changed. It now has " &_objDiskChange.TargetInstance.freespace &_
" Bytes free."
End If
If (snapTime - startTime) > RUN_TIME Then
Exit Do
End If
Loop
WScript.Echo FormatNumber(snapTime-startTime) & " seconds elapsed. Exiting now"
WScript.quitFrom
Microsoft® VBScript. Step by Step
By Ed Wilson -
For...Next
2010-03-23 11:06:32
For...Next VS For Each...Next
An important difference between For Each...Next and For...Next is that with For Each...Next, you don't have to know how many times you want to do something. With the For...Next construct, you must know exactly how many times you want to do something.
例子DisplayProcessInformation.vbs
'Header information section,define the variabl and on error resume Next
Option Explicit
On Error Resume Next
Dim objWMIService
Dim objItem
Dim i'reference information sectuin,define two constants
Const MAX_LOOPS = 8, ONE_HOUR = 3600000'Worker and Output Information, connects to WMI and then executes a query
'that lists all Win32 processes running on the machine
'query chooses everything from the Win32 process, but only if the process ID is not equal to 0
'(the system idle process)
For i = 1 To MAX_LOOPS
Set bjWMIService = GetObject("winmgmts:").ExecQuery _
("SELECT * FROM Win32_Process where processID <> 0")
WScript.Echo "There are " & objWMIService.count &_
" processes running " & Now
For Each objItem In objWMIService
WScript.Echo "Process: " & objItem.Name
WScript.Echo Space(9) & objItem.commandline
WScript.Echo "Process ID: " & objItem.ProcessID
WScript.Echo "Thread Count: " & objItem.ThreadCount
WScript.Echo "Page File Size: " & objItem.PageFileUsage
WScript.Echo "Page Faults: " & objItem.PageFaults
WScript.Echo "Working Set Size: " & objItem.WorkingSetSize
WScript.Echo vbNewLine ' add a blank line
Next
WScript.Echo "******PASS COMPLETE**********"
WScript.Sleep ONE_HOUR 'wscript. sleep method, the time expression with milliseconds
NextFrom
Microsoft® VBScript. Step by Step
By Ed Wilson -
Constants vs. Variables
2010-03-22 20:33:33
Compareing to Variable,there are several advantages by using a constant
1 The script. is easier to read
2 The script. is easier to revise
3 The script. is easier to reuse
Wrong example for variable
Option Explicit
On Error Resume Next
Dim total
Dim FirstValue
Dim SecondValue
FirstValue = 1
SecondValue = 3
Total = FirstValue + SecondValue
WScript.Echo " the total of " & FirstValue & " and " & _
SecondValue & " Is " & (total)
FirstValue = Total
WScript.Echo " the total of " & FirstValue & " and " & _
SecondValue & " Is " & (Total)Executing the scripting The result is wrong like below:
C:\script.vbs H:CScript
Microsogt <R> Windows Script. Host Version 5.7
Copyright <C> Microsoft Corporation. All right reserved.
the total of 1 and 3 Is 4
the total of 4 and 3 Is 4
From
Microsoft® VBScript. Step by Step
By Ed Wilson -
For Each...Next
2010-03-22 20:17:05
例子CollectionOfDrives.vbs
Option Explicit
On Error Resume Next
Dim colDrives 'the collection that comes from WMI
Dim drive 'an individual drive in the collection
Const DriveType = 3 'Local drives. From the SDK
set colDrives =_
GetObject("winmgmts:").ExecQuery("select size,freespace " &_
"from Win32_LogicalDisk where DriveType =" & DriveType)
For Each drive in colDrives 'walks through the collection
WScript.Echo "Drive: " & drive.DeviceID
WScript.Echo "Size: " & drive.size
WScript.Echo "Freespace: " & drive.freespace
NextFor Each...Next,描述的是个体和集合的概念,只要存在集合的概念,应第一考虑For Each...Next的用法,例如
files, and folders are returned as a collection from the fileSystemObject
From
Microsoft® VBScript. Step by Step
By Ed Wilson -
VBScript Step By Step Chapter 1
2010-03-22 19:22:02
例1 DisplayAdminTools.vbs
Set bjshell = CreateObject("Shell.Application")
Set bjNS = objshell.namespace(&h2f)
Set colitems = objNS.items
For Each objitem In colitems
WScript.Echo objitem.name
NextTable 1-1. Useful registry keys for script. writers
Location
Service information
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
User name used to log on to the domain
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Logon User Name
Microsoft Exchange 2000 domain information
HKEY_CURRENT_USER\Software\Microsoft\Exchange\LogonDomain
Exchange 2000 domain user information
HKEY_CURRENT_USER\Software\Microsoft\Exchange\UserName
Group Policy server
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy\History\DCName
User's home directory
HKEY_CURRENT_USER\Volatile Environment\HomeShare
The server that authenticated the currently logged-on user
HKEY_CURRENT_USER\Volatile Environment\LOGONSERVER
The DNS domain name of the currently logged-on user
HKEY_CURRENT_USER\Volatile Environment\USERDNSDOMAIN
例2 DisplayComputerNamesWithComments.vbs
'This script. displays various Computer Names by reading the registry
Option Explicit 'forces the scripter to declare variables
On Error Resume Next 'tells VBScript. to go to the next line
'instead of exiting when an error occurs
'Dim is used to declare variable names that are used in the script
Dim objShell
Dim regActiveComputerName, regComputerName, regHostname
Dim ActiveComputerName, ComputerName, Hostname
'When you use a variable name and then an equal sign (=)
'you're saying the variable contains the information on the right.
'The registry keys are quite long, so make them easier to read on
'a single screen by splitting the line in two.regActiveComputerName = "HKLM\SYSTEM\CurrentControlSet" & _
"\Control\ComputerName\ActiveComputerName\ComputerName"
regComputerName = "HKLM\SYSTEM\CurrentControlSet\Control" & _
"\ComputerName\ComputerName\ComputerName"
regHostname = "HKLM\SYSTEM\CurrentControlSet\Services" & _
"\Tcpip\Parameters\Hostname"
Set bjShell = CreateObject("WScript.Shell")
ActiveComputerName = objShell.RegRead(regActiveComputerName)
ComputerName = objShell.RegRead(regComputerName)
Hostname = objShell.RegRead(regHostname)'To make dialog boxes you can use WScript.Echo
'and then tell it what you want it to say.WScript.Echo activecomputername & " is active computer name"
WScript.Echo ComputerName & " is computer name"
WScript.Echo Hostname & " is host name"例3 registry keys Information.VBS
Option Explicit
On Error Resume Next
Dim objShell
Dim regLogonUserName, regServiceInformation, regGPServer
Dim regLogonServer, regDNSdomain
Dim LogonUserName, ServiceInformation, GPServer
Dim LogonServer, DNSdomain
regLogonUserName = "HKEY_CURRENT_USER\Software\Microsoft\" & _
"Windows\CurrentVersion\Explorer\Logon User Name"
regServiceInformation = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ServiceCurrent"
regGPServer = "HKEY_CURRENT_USER\Software\Microsoft\Windows\" & _
"CurrentVersion\Group Policy\History\DCName"
regLogonServer = "HKEY_CURRENT_USER\Volatile Environment\" & _
"LOGONSERVER"
regDNSdomain = "HKEY_CURRENT_USER\Volatile Environment\" & _
"USERDNSDOMAIN"
Set bjShell = CreateObject("WScript.Shell")
LogonUserName = objShell.RegRead(regLogonUserName)
ServiceInformation = objShell.RegRead(regServiceInformation)
GPServer = objShell.RegRead(regGPServer)
LogonServer = objShell.RegRead(regLogonServer)
DNSdomain = objShell.RegRead(regDNSdomain)
WScript.Echo LogonUserName & " is currently Logged on"
WScript.Echo ServiceInformation & " is the current service current"
WScript.Echo GPServer & " is the current Group Policy Server"
WScript.Echo LogonServer & " is the current logon server"
WScript.Echo DNSdomain & " is the current DNS domain"例4 Crash Recovery Information.VBS
Option Explicit
On Error Resume Next
Dim objShell
Dim regLogonUserName, regServiceInformation, regGPServer
Dim regLogonServer, regDNSdomain
Dim LogonUserName, ServiceInformation, GPServer
Dim LogonServer, DNSdomain
regLogonUserName = "HKEY_CURRENT_USER\Software\Microsoft\" & _
"Windows\CurrentVersion\Explorer\Logon User Name"
regServiceInformation = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ServiceCurrent"
regGPServer = "HKEY_CURRENT_USER\Software\Microsoft\Windows\" & _
"CurrentVersion\Group Policy\History\DCName"
regLogonServer = "HKEY_CURRENT_USER\Volatile Environment\" & _
"LOGONSERVER"
regDNSdomain = "HKEY_CURRENT_USER\Volatile Environment\" & _
"USERDNSDOMAIN"
Set bjShell = CreateObject("WScript.Shell")
LogonUserName = objShell.RegRead(regLogonUserName)
ServiceInformation = objShell.RegRead(regServiceInformation)
GPServer = objShell.RegRead(regGPServer)
LogonServer = objShell.RegRead(regLogonServer)
DNSdomain = objShell.RegRead(regDNSdomain)
WScript.Echo LogonUserName & " is currently Logged on"
WScript.Echo ServiceInformation & " is the current service current"
WScript.Echo GPServer & " is the current Group Policy Server"
WScript.Echo LogonServer & " is the current logon server"
WScript.Echo DNSdomain & " is the current DNS domain"From
Microsoft® VBScript. Step by Step
By Ed Wilson -
VBS例子
2010-03-22 00:20:22
例1
定义一个数组, 包含5个元素, 都是随机整数(随便输入), 要求把他们按照从大到小的顺序排列起来
Option Explicit
Dim a(4)
Dim i
Dim j
Dim tempFor i =0 to 4
a(i) = inputbox ("请输入第" &(i+1) &"个元素 ")
NextFor i = 0 to 4
For j = (i+1) to 4
If a(i) < a(j) Then
temp = a(i)
a(i) = a(j)
a(j) = temp
End If
Next
Next
msgbox (a(0)&" "&a(1)&" "&a(2)&" "&a(3)&" "&a(4)) -
VBScript
2010-03-21 23:25:44
VBScript是Visual Basic Script的简称,有时也被缩写为VBS。VBScript是微软开发的一种脚本语言,可以看作是VB语言的简化版,与VBA的关系也非常密切。它具有原语言容易学习的特性。目前这种语言广泛应用于网页和ASP程序制作,同时还可以直接作为一个可执行程序。用于调试简单的VB语句非常方便。
目录
使用范围 由于VBScript可以通过Windows脚本宿主调用COM,因而可以使用Windows操作系统中可以被使用的程序库,比如它可以使用Microsoft Office的库,尤其是使用Microsoft Access和Microsoft SQL Server的程序库,当然它也可以使用其它程序和操作系统本身的库。在实践中VBScript一般被用在以下三个方面:
Windows操作系统
VBScript可以被用来自动地完成重复性的Windows操作系统任务。在Windows操作系统中,VBScript可以在Windows Script. Host的范围内运行。Windows操作系统可以自动辨认和执行*.VBS和*.WSF两种文档格式,此外Internet Explorer可以执行HTA和CHM文档格式。VBS和WSF文档完全是文字式的,它们只能通过少数几种对话窗口与用户通讯。HTA和CHM文档使用HTML格式,它们的程序码可以象HTML一样被编辑和检查。在WSF、HTA和CHM文档中VBScript和JavaScript的程序码可以任意混合。HTA文档实际上是加有VBS、JavaScript成分的HTML文档。CHM文档是一种在线帮助,用户可以使用专门的编辑程序将HTML程序编辑为CHM。
网页浏览器(客户端的VBS)
网页中的VBS可以用来控制客户端的网页浏览器(以浏览器执行VBS程序)。VBS与JavaScript在这一方面是竞争者,它们可以用来实现动态HTML,甚至可以将整个程序结合到网页中来。
至今为止VBS在客户方面未能占优势,因为它只获得Microsoft Internet Explorer的支持(Mozilla Suite可以透过安装一个包来支持VBS)。而JavaScript则受到所有网页浏览器的支持。在Internet Explorer中VBS和JavaScript使用同样的权限,它们只能有限地使用Windows操作系统中的对象。
网页服务器(服务器方面的VBS)
在网页服务器方面VBS是微软的Active Server Pages的一部分,它与JavaServer Pages和PHP是竞争对手。在这里VBS的程序码直接嵌入到HTML页内,这样的网页以ASP结尾。网页服务器Internet信息服务执行ASP页内的程序部分并将其结果转化为HTML传递给网页浏览器供用户使用。这样服务器可以进行数据库闻讯并将其结果放到HTML网页中。
语言
VBScript主要的优点有:
- 由于VBScript由操作系统,而不是由网页浏览器解释,它的文档比较小。
- 易学。
- 在所有2000 / 98SE以后的Windows版本都可直接使用。
- 可以使用其它程序和可使用的对象(尤其Microsoft Office)。
缺点有:
- 现在VBS无法作为电子邮件的附件了。Microsoft Outlook拒绝接受VBS为附件,收信人无法直接使用VBS附件。
- VBS的各种编辑器不受欢迎。
- 操作系统没有任何特别的保护设施。VBS程序与其它JS、EXE、BAT或CMD程序一样对待。操作系统没有监察恶意功能的能力。
和VB的对比
不能为变量定义类型
在VB中,为变量定义类型使用Dim变量名As类型
但是在VBScript中这样写是错误的。只能使用Dim变量名,解释器会自动根据赋值的类型定义变量类型。
不能使用条件编译
在VB中,可以使用#If…Then、#ElseIf…Then、#Else、#End If、#Const… = …等语句定义编译时使用的语句
而由于VBScript不需要编译即可直接执行,所以并不需要条件编译语句。
安全性
微软决定Outlook和Outlook Express中的HTML邮件可以使用VBScript后出现了许多利用Windows Script. Host和ActiveX的功能的电脑病毒。这些病毒之所以能够传播开来也是因为一开始这些系统功能完全未受保护。虽然VBScript和JavaScript使用同样的使用操作系统的功能的安全措施,今天呼唤这些功能被看作不符合标准。
一般很难保护VBScript的程序码不被用户看到。
-
COM component(COM组件)
2010-03-21 23:15:14
COM是微软公司为了计算机工业的软件生产更加符合人类的行为方式开发的一种新的软件开发技术。在COM构架下,人们可以开发出各种各样的功能专一的组件,然后将它们按照需要组合起来,构成复杂的应用系统。由此带来的好处是多方面的:可以将系统中的组件用新的替换掉,以便随时进行系统的升级和定制;可以在多个应用系统中重复利用同一个组件;可以方便的将应用系统扩展到网络环境下;COM与语言,平台无关的特性使所有的程序员均可充分发挥自己的才智与专长编写组件模块;等等。
COM是开发软件组件的一种方法。组件实际上是一些小的二进制可执行程序,它们可以给应用程序,操作系统以及其他组件提供服务。开发自定义的COM组件就如同开发动态的,面向对象的API。多个COM对象可以连接起来形成应用程序或组件系统。并且组件可以在运行时刻,在不被重新链接或编译应用程序的情况下被卸下或替换掉。Microsoft的许多技术,如ActiveX, DirectX以及OLE等都是基于COM而建立起来的。并且Microsoft的开发人员也大量使用COM组件来定制他们的应用程序及操作系统。
COM所含的概念并不止是在Microsoft Windows操作系统下才有效。COM并不是一个大的API,它实际上象结构化编程及面向对象编程方法那样,也是一种编程方法。在任何一种操作系统中,开发人员均可以遵循“COM方法”。
一个应用程序通常是由单个的二进制文件组成的。当编译器生成应用程序之后,在对下一个版本重新编译并发行新生成的版本之前,应用程序一般不会发生任何变化。操作系统,硬件及客户需求的改变都必须等到整个应用程序被重新生成。
目前这种状况已经发生变化。开发人员开始将单个的应用程序分隔成单独多个独立的部分,也即组件。这种做法的好处是可以随着技术的不断发展而用新的组件取代已有的组件。此时的应用程序可以随新组件不断取代旧的组件而渐趋完善。而且利用已有的组件,用户还可以快速的建立全新的应用。
传统的做法是将应用程序分割成文件,模块或类,然后将它们编译并链接成一个单模应用程序。它与组件建立应用程序的过程(称为组件构架)有很大的不同。一个组件同一个微型应用程序类似,即都是已经编译链接好并可以使用的二进制代码,应用程序就是由多个这样的组件打包而得到的。单模应用程序只有一个二进制代码模块。自定义组件可以在运行时刻同其他的组件连接起来以构成某个应用程序。在需要对应用程序进行修改或改进时,只需要将构成此应用程序的组件中的某个用新的版本替换掉即可。
COM,即组件对象模型,是关于如何建立组件以及如何通过组件建立应用程序的一个规范,说明了如何可动态交替更新组件。[使用组件的优点]
使用组件的种种优点直接来源于可以将它们动态的插入或卸出应用。为了实现这种功能,所有的组件必须满足两个条件:第一,组件必须动态链接;第二,它们必须隐藏(或封装)其内部实现细节。动态链接对于组件而言是一个至关重要的要求,而消息隐藏则是动态链接的一个必要条件。
补充:COM组件由以Win 32动态连接库(DLL)或可执行文件(EXE)形式发布的可执行代码所组成。遵循COM规范编写出来的组件将能够满足对组件架构的所有要求。COM组件可以给应用程序、操作系统以及其他组件提供服务;自定义的COM组件可以在运行时刻同其他组件连接起来构成某个应用程序;COM组件可以动态的插入或卸出应用。
恶意网站可以利用含有漏洞的com组件接口,下载木马,并且执行;
禁用com组件一般是指设置了Kill位,即IE浏览器不能使用这个组件,通俗讲:通过设置Kill位,可以使InternetExplorer在使用默认设置时永不调用被禁用的com组件,从而禁止该控件在Internet Explorer中运行。禁用含有漏洞的com组件后,IE就不能调用含有漏洞的COM组件;黑客利用有漏洞的COM组,写成的网页代码就不能在IE中被执行,木马等将不会被下载。[禁用com组件可能导致的问题]
在线杀毒功能的组件被禁用,会导致在线杀毒不能使用;
在线游戏功能的组件被禁用,会导致在线游戏无法玩,
com组件禁用后的具体情况,需要根据具体的com组件功能作判断。[手动启动COM组件操作方法]
我的栏目
标题搜索
我的存档
数据统计
- 访问量: 78109
- 日志数: 111
- 建立时间: 2007-09-07
- 更新时间: 2012-06-05