Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Microsoft VBScript resource
VBScript相關資源
Quick Start:
VBScript Operators
https://msdn.microsoft.com/en-us/library/9da4s2eh(v=vs.84).aspx
' default value of variable is "" or Empty or 0
Dim p1
If p1 = "" Then
Wscript.Echo "p1="""""
End If
If p1 = 0 Then
Wscript.Echo "p1=0"
End If
If isEmpty(p1) Then
Wscript.Echo "isEmpty(p1)"
End If
' default value is not null
If isNull(p1) Then
Wscript.Echo "isNull(p1)"
End If
Constant Value Description
----------------------------------------------------------------
vbCr Chr(13) Carriage return
vbCrLf Chr(13)& Chr(10) Carriage return–linefeed combination
vbLf Chr(10) Line feed
- vbCr : - return to line beginning
Represents a carriage-return character for print and display functions. - vbCrLf : - similar to pressing Enter
Represents a carriage-return character combined with a linefeed character for print and display functions. - vbLf : - go to next line
Represents a linefeed character for print and display functions.
' String
strMyString = "Line1"
strMyString = strMyString & vbNewLine
strMyString = strMyString & "Line2"
strMyString = strMyString & vbCr & vbLf
strMyString = strMyString & "Line3"
strMyString = strMyString & vbCrLf
strMyString = strMyString & "Line4"
' Date
Dim dteExpiredDate
dteExpiredDate = "2008/12/31"
dteExpiredDate = #2008/12/31#
'Time
Now()
Time()
Hour(Now())
Minute(Now())
Second(Now())
' Constants
Const COMP_NAME = "Microsoft"
Const LOCATION = "Taiwan"
' Intrinsic Constants (Build-in Constants)
FormatDateTime(Now(),vbShortDate)
FormatDateTime(Now(),vbLongDate)
FormatDateTime(Now(),vbShortTime)
FormatDateTime(Now(),vbLongTime)
' Buildin Datediff Function
DateDiff("d", Now, "2008/05/30")
' Buildin String Function
strMyString = "This is a book."
WScript.Echo "Upper Case : " & UCase(strMyString)
WScript.Echo "Lower Case : " & LCase(strMyString)
LEN(strMyString)
Left(strMyString,4)
Right(strMyString,5)
InStr(strMyString,"is")
InStr(4,strMyString,"is")
' Round
Randomize
vRnd = Rnd * 100
WScript.Echo "vRnd : " & vRnd
WScript.Echo "Round(vRnd) : " & Round(vRnd)
'Conversion Functions
'CInt
Dim MyDouble, MyInt, MyByte, MyString, MyVal1, MyVal2, MyLong1, MyLong2
MyDouble = 2345.5678 ' MyDouble is a Double.
MyInt = CInt(MyDouble) ' MyInt contains 2346.
'CBytes
MyDouble = 125.5678 ' MyDouble is a Double.
MyByte = CByte(MyDouble) ' MyByte contains 126.
'CStr
MyDouble = 437.324 ' MyDouble is a Double.
MyString = CStr(MyDouble) ' MyString contains "437.324".
'CLng
MyVal1 = 25427.45: MyVal2 = 25427.55 ' MyVal1, MyVal2 are Doubles.
MyLong1 = CLng(MyVal1) ' MyLong1 contains 25427.
MyLong2 = CLng(MyVal2) ' MyLong2 contains 25428.
Using Conditional Statements
https://msdn.microsoft.com/en-us/library/9t9x467f(v=vs.84).aspx
' If Then Else
' Dim myDate
myDate = #2/13/95#
If myDate < Now Then myDate = Now
If value = 0 Then
WScript.Echo "value=0"
ElseIf value = 1 Then
WScript.Echo "value=1"
Else
WScript.Echo "Value out of range!"
End If
MyVar = 2
Select Case MyVar
Case "0"
WScript.Echo "MyVar = 0"
Case "1"
WScript.Echo "MyVar = 1"
Case "2"
WScript.Echo "MyVar = 2"
Case Else
WScript.Echo "Sorry value is out of range"
End Select
Looping Through Code
https://msdn.microsoft.com/en-us/library/cbe735w2(v=vs.84).aspx
For j = 2 To 10 Step 2
total = total + j
Next
For myNum = 16 To 2 Step -2
total = total + myNum
Next
Do While myNum2 > 10
myNum2 = myNum2 - 1
counter = counter + 1
Loop
Do Until myNum3 = 1
myNum3 = myNum3 - 1
counter2 = counter2 + 1
If myNum3 < 3 Then Exit Do
Loop
Set d = CreateObject("Scripting.Dictionary")
d.Add "0", "Athens" 'Add some keys and items
d.Add "1", "Belgrade"
d.Add "2", "Cairo"
For Each I in d
WScript.Echo "D.Item(" & I & ") : " & d.Item(I)
Next
'Array & Dynamic Array
'== 1 Dimension Array ==
Dim Names(9) ' Declare an array with 10 elements.
'== 2 Dimension Array ==
Dim aryTwoDimension(2,2)
Dim aryTripleDimension
aryTripleDimension = Array(10,20,30)
B = aryTripleDimension(2) ' B is now 30.
'== Dynamic Array ==
Dim NumArray()
Dim DynamicArray() ' Declare a dynamic array.
ReDim DynamicArray(9) ' Allocate storage space.
ReDim Preserve DynamicArray(10) ' Allocate storage space.
'== Erase Array ==
Erase NumArray ' Each element is reinitialized.
Erase DynamicArray ' Free memory used by array.
'== Ubound Function ==
Dim A(100,3,4)
WScript.Echo "UBound(A, 1) : " & UBound(A, 1)
WScript.Echo "UBound(A, 2) : " & UBound(A, 2)
WScript.Echo "UBound(A, 3) : " & UBound(A, 3)
'== Split ==
Dim MyString, MyArray, Msg
MyString = "VBScriptXisXfun!"
MyArray = Split(MyString, "x", -1, 1)
' MyArray(0) contains "VBScript".
' MyArray(1) contains "is".
' MyArray(2) contains "fun!".
Msg = MyArray(0) & " " & MyArray(1)
Msg = Msg & " " & MyArray(2)
WScript.Echo Msg
'== Is Array ==
Dim MyVariable
Dim MyArray5(3)
MyArray5(0) = "Sunday"
MyArray5(1) = "Monday"
MyArray5(2) = "Tuesday"
MyVariable = IsArray(MyArray5) ' MyVariable contains "True".
WScript.Echo "MyVariable : " & MyVariable
' Sub Procedures
https://msdn.microsoft.com/en-us/library/bx9ceb2w(v=vs.84).aspx
Sub Procedures
Sub ConvertTemp
temp = InputBox("Please enter the temperature in degrees F.", 1)
MsgBox "The temperature is " & Celsius(temp) & " degrees C."
End Sub
Function Procedures
Sub ConvertTemp
temp = InputBox("Please enter the temperature in degrees F.", 1)
MsgBox "The temperature is " & Celsius(temp) & " degrees C."
End Sub
Function Celsius(fDegrees)
Celsius = (fDegrees - 32) * 5 / 9
End Function
'== Pass paremeter ==
WScript.Echo "Gross Value is: " & GrossValue(100,0.175)
Function GrossValue(NetValue, TaxRate)
GrossValue=NetValue + (NetValue * TaxRate)
End Function
'== Pass array parameter ==
Dim aryDemo(2)
aryDemo(0) = 1
aryDemo(1) = 2
aryDemo(2) = 3
Call PrintArray(aryDemo)
Sub PrintArray(aryParameter)
Dim i
For i=0 To UBound(aryParameter)
WScript.Echo "Array(" & i & ") Value Is : " & aryParameter(i)
Next
End Sub
'== Pass Object parameter ==
' List Items in the My Computer Folder
Const MY_COMPUTER = &H11&
Set objShell = CreateObject("Shell.Application")
Call ListItemInMyComputer(objShell)
Sub ListItemInMyComputer(objShellObject)
Set objFolder = objShellObject.Namespace(MY_COMPUTER)
Set objFolderItem = objFolder.Self
WScript.Echo objFolderItem.Path
Set colItems = objFolder.Items
For Each objItem in colItems
Wscript.Echo objItem.Name
Next
End Sub
Set objShell = Nothing
Sub TestSub(ByRef MyParam)
MyParam = 5
End Sub
Dim MyArg
MyArg = 123
TestSub MyArg
' MyArg in changed in TestSub to 5.
Sub TestSub(ByVal MyParam)
MyParam = 5
End Sub
Dim MyArg
MyArg = 123
TestSub MyArg
' MyArg is still 123.
更多ByRef,ByVal用法請參考
ByRef and ByVal Parameters
https://msdn.microsoft.com/en-us/library/ee478101(v=vs.84).aspx
Error handling
To Err Is VBScript – Part 1
https://technet.microsoft.com/en-us/library/ee692852.aspx
On Error Resume Next
strComputer = "fictional"
strPrinter = "TestPrinter"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
If Err = 0 Then
WScript.Echo "No error binding to " & strComputer
strPrinter = "TestPrinter"
Set objPrinter = objWMIService.Get _
("Win32_Printer.Name='" & strPrinter & "'")
If Err = 0 Then
WScript.Echo "No error connecting to " & strPrinter
Else
DisplayErrorInfo
End If
Else
DisplayErrorInfo
End If
'******************************************************************************
Sub DisplayErrorInfo
WScript.Echo "Error: : " & Err
WScript.Echo "Error (hex) : &H" & Hex(Err)
WScript.Echo "Source : " & Err.Source
WScript.Echo "Description : " & Err.Description
Err.Clear
End Sub
VBScript:
Script Center > Learn > Learn Beginning Scripting
- Hey, Scripting Guy! Blog - VBScript
- VBScript Language Reference
- WSH Language Reference
- WMI SDK
- ADSI SDK
- Script Center - Repository
Scripting Guide
Scripting Concepts and Technologies for System Administration
Scripting Solutions for System Administration
Download Documents or Tools
- Microsoft Windows Script 5.6 help Script 5.6 Help
- Windows Script 5.6 Documentation (scrdoc56en.exe)
- VBScript V5.5 Documentation (vbsdoc.exe)
- VBScript V5.5 程式語言參考-VBScript Reference
- Script Center All-in-One
- The Hey, Scripting Guy! Archive: Volume 2 (August 2004 - September 2007)
- Scripting Guy
- Sesame Script
- TechNet Script Center
- WMI Help
- WMI Explorer 2.0 , WMI Explorer 1.00
- The WMI Diagnosis Utility
- WMI Administrative Tools
- WMI Code Creator v1.0
VBScript Constants
VBScript Template
Template - Sample
Security:
vbe file
Script Encoder Overview (Script Encoder is not supported in Windows Vista or Windows 7)
SCRENC [/s] [/f] [/xl] [/l defLanguage ] [ /e defExtension] input file output file
Script Encoder , Script Encoder
HTML Applications (HTAs):
Creating Your Own HTAs - Try It Yourself: Add a Multi-Line Text Box to an HTA
Creating Your Own HTAs - Try It Yourself: Add Radio Buttons to an HTA
Creating Your Own HTAs - Try It Yourself: Add a Button to an HTA
Creating Your Own HTAs - Try It Yourself: Add a Check Box to an HTA
Creating Your Own HTAs - Try It Yourself: Add a Drop-down List Box to an HTA
Creating Your Own HTAs - Try It Yourself: Add a List Box to an HTA
Creating Your Own HTAs - Try It Yourself: Add a Multi-select List Box to an HTA
Creating Your Own HTAs - Try It Yourself: Add a Password Box to an HTA
Creating Your Own HTAs - Try It Yourself: Add a Subroutine to an HTA
Creating Your Own HTAs - Try It Yourself: Add a Text Box to an HTA
Creating Your Own HTAs - Try It Yourself: Display Process Names and IDs in an HTA
Creating Your Own HTAs - Try It Yourself: Write Data to a <span>
Extreme Makeover, Part 2: Wrap Your Scripts Up in a GUI Interface
Extreme Makeover: Wrap Your Scripts Up in a GUI Interface
The ABCs of HTAs: Scripting HTML Applications - Add Color Options to a List Box
The ABCs of HTAs: Scripting HTML Applications - Add a Gradient Background to Your HTAs
The ABCs of HTAs: Scripting HTML Applications - AutoRefresh an HTA Using a Timer
The ABCs of HTAs: Scripting HTML Applications - Create an HTA Without a Title Bar
The ABCs of HTAs: Scripting HTML Applications - Run a Script from a Text Link
- HTML Applications Reference
- HTML and DHTML Reference
- Internet Explorer Filters and Transition
- How Can I Encode Scripts Within an HTA?
https://blogs.technet.microsoft.com/heyscriptingguy/2006/03/09/how-can-i-encode-scripts-within-an-hta/Method 1
step1.add **Start Encode** tag to mark the beginning of the section to be encoded
<script language=”VBScript”>
'**Start Encode**step2.
Screnc /e htm test.hta encoded.hta
Or to do it in place (overwriting the original):
Screnc /f /e htm test.hta
Method 2
step1.rename test.hta to test.htm
step2.screnc test.htm encoded.hta
Development Tools for VBScript:
- Visual Studio
- Notepad
- Notepad++ (Third-Party)
- UltraEdit (Third-Party)
- VbsEdit (Third-Party)
- Admin Script Editor (Third-Party)
Code Auto Generator:
- Scriptomatic 2.0 - Scriptomatic2
- ADSI Scriptomatic - EZADScriptomatic
- Tweakomatic (WMI) - tweakomatic
- WMI Code Creator v1.0 - Date Published:9/12/2012 - WMICodeCreator
- HTA Helpomatic - HTA Helpomatic 1.01 hta_helpomatic , HTAHelpomatic Code Generator