1. Overview
In addition to providing an object model, Windows script Host provides a scripting framework, which is the wsf script. By WSF the agreed markup elements, you can combine code blocks written in a variety of scripting languages to accomplish the task. In addition, you can implement some DOS command function, through the Cscript xxx.wsf/? To view the Help document, the Help document is declared by the WSF and the interpreter dynamically generate the complete document. Combined with the Wshenvironment object, it is possible to implement the DOS command, which provides the increment function freely.
2.WSF Marking
Elements |
Grammar |
Property |
Note |
<?job?> |
<? job error= "Flag" debug= "flag"?> |
error=> boolean value, default value false, and when error= "true", allows syntax errors or run-time errors in Windows Script (. wsf) files. debug=> boolean value, default value false, and when debug= "true", debugging is enabled and the script debugger is started when an error occurs. |
Parent element <job> |
<? Xml?> |
<? XML version= "Version" [standalone= "Dtdflag"]?> |
The VERSION=>N.N form that specifies the XML level of the file. The default value is 1.0. Dtdflag=> Optional, Boolean value, indicating whether references to external document type definitions (DTDs) are included in the XML file. The Script Component XML file does not include such a reference, so the property value is always "yes". |
Optional, must be placed on the first line. By no means, otherwise, the greater-than and less-than numbers in the script will be handled incorrectly |
<description> |
<description>text</description> |
text=> text "Multiline" to describe the purpose of the script, that is, the first line of the usage document |
Parent element <runtime> |
<example> |
<example>text</example> |
Text=> text to provide a simple script invocation example |
Parent element <runtime> |
<job> |
<job [id=jobid]>code</job> |
Jobid=> Job ID, which must be a legal identifier, and is unique in the current program |
There is only one job of the wsf script, without the parent element <package> |
<NAMED> |
<named name = Namedname helpstring = helpstring type = "String|boolean|simple" required = Boolean/> |
name=> string; parameter name, passed in Wscript.Arguments.Named helpstring=> text; The description document for this parameter. The WSH runtime uses Showusage () or/? To get the parameter document defined here. The type=> is optional. The type of the parameter that defines how the parameter is parsed from the command line. The default value is Simple;string, which is a string, and when Boolean is represented with +/-true/false;simple, there is no value, such as "dir/b", the "B" named parameter, which is an optional Required=>, a Boolean value ; Indicates whether a parameter is required. |
parent element <runtime>, can be a lot of |
<object> |
<object id= "ObjID" [classid= "Clsid:guid" | progid= "ProgID"]/> |
Objid=> a valid and unique identifier. Used to refer to the name of the object in the script. Guid=> is optional. The Class ID (GUID) of the object. Progid=> is optional. The program ID of the object, which can be specified to replace the class ID. |
The parent element <job>, such as <obect id= "FSO" progid= "Scripting.FileSystemObject"/> The FSO can be used directly in the script without having to declare the object; it can be a lot of |
<package> |
<package>jobs</package> |
Jobs=> one or more <job> elements |
When there are multiple jobs, you must use the |
<reference> |
<reference [progid= "ProgID" |guid= "Typelibguid"] [version= "version"]/> |
Progid=> You can derive a program ID for a type library. It can contain a version number (for example, ADO. recordset.2.0), which is the explicit program ID of the type library, can also contain executables that are merged with the type library, such as. DLL) for the program ID. If you use Object properties, you do not need to specify a version property because the version can be inferred from the program ID. If you specify an object property, you cannot specify the GUID property at the same time. Typelibguid=> the GUID of the type library to reference. If you specify a GUID property, you cannot specify an object property. Version=> is optional. The version number of the type library to use. It must be in the form of < major version >[.< minor version;]. If no version is specified, the default version is 1.0. If the object property is used to specify a type library and no version is specified, the version is derived from the registry key that specifies the program ID. If no information is found, the default version is 1.0. |
parent element <job> can have many, one job references that are not visible in another job's code |
<resource> |
<resource id= "ResourceID" >textornumber</resource> |
A unique identifier for a resource in a resourceid=> script, with text or numbers |
Can have many, parent element <job> A job resource that is not visible in another job's code |
<runtime> |
<runtime> <description>text<description> <named attributes etc./> <unnamed attributes etc./> <example>text</example> </runtime> |
|
Showusage () uses the information enclosed by the <runtime> element to display the script's run-time parameters. Parent element <job> |
<script> |
<script language= "Language" [src= "strfile"]>code</script> |
The language=> declaration is used for scripting language names in script blocks, such as VBScript or JavaScript. strfile=> the script file name to include in the script block, such as C:\\a.js |
parent element <job>, there can be many, the language does not have to be the same |
<UNNAMED> |
<unnamed name = Unnamedname helpstring = helpstring many = Boolean required = Boolean/integer/> |
name = = The string that is used to represent the parameter in the usage. This value is not used elsewhere. The helpstring=> the description document for this parameter. The WSH runtime uses Showusage () or/? Gets the parameter document Many=> optional, boolean value. If true, the parameter can be repeated more times than specified by the required property, otherwise the required property represents the number of times the desired parameter is to be used. The required=> is optional. An integer value that indicates the number of occurrences of this parameter on the command line. |
|
<usage> |
<usage>text</usage> |
Text=> used to replace WSH dynamically generated documents based on other elements of the <runtime> element |
Parent element <runtime> |
3. Example of a custom command-line tool
1<package>2<job id= "JS" >3<resource id= "DBPath" >D:\Data\Persons.xml</resource>4<runtime>5<description>write the base information of a person to the database</description>6<named name= "name" helpstring= "name of person." Type= "string" required= "true"/>7<named name= "Sex" helpstring= "sex of person." Type= "string" required= "true"/>8<named name= "Age" helpstring= "age of the person." Type= "string" required= "true"/>9<unnamed name= "Note" helpstring= "note of person" many= "false" required= "false"/>Ten<example>test.wsf/name:lilei/sex:male/age:34 "Got some trouble!" </example> One</runtime> A<script language= "JScript" > - varArgsnamed =WScript.Arguments.Named; - varArgsunnamed =WScript.Arguments.Unnamed; theWScript.Echo ("There is" + Argsnamed.length + "named arguments:"); - for(varE=NewEnumerator (argsnamed);!e.atend (); E.movenext ()) { - varName=E.item (); -WScript.Echo ("['" "+name+" ']= "+Argsnamed.item (name)); + } -WScript.Echo ("There is" + argsunnamed.length + "Unnamed arguments:"); + for(vari=0;i<argsunnamed.length;i++){ AWScript.Echo (' [' +i.tostring () + ']: ' +Argsunnamed.item (i)); at } -WScript.Echo ("There is" + WScript.Arguments.length + "Arguments in total:"); - for(varE=NewEnumerator (wscript.arguments);!e.atend (); E.movenext ()) { - WScript.Echo (E.item ()); - } -WScript.Echo (GetResource (' DBPath '))); in</script> -</job> to<job id= "vbs" > +<script language= "VBScript" > -WScript.Echo GetResource ("DBPath") the</script> *</job> $</package>
Execute "test.wsf/?" The results are as follows:
1 write The base information of a person to the Database 2 usage: Test.wsf/name:value /sex:value /age:value [note] 3 4 options: 5 6 name:name of Person. 7 Sex:sex of person. age:age of person. note:note of person 10 test.wsf/name:lilei /sex:male /age:34 "Got some trouble!"
Line 1th, the first line shows the contents of <description> directly
Line 2nd, dynamically generated invocation by WScript.exe or CScript.exe, based on the information provided by the various child elements of the <runtime> element
第6-9, which is the information provided by WScript.exe or CScript.exe according to the various <named>/<unnamed> elements of the <runtime> element, generates a parameter description, that is, the < parameter name > :<description>
Line 10th, the last line directly shows the contents of the <example> element.
Because the note parameter required= "false", it is not required, so the above document shows "[note]" instead of "note"
In the command window, execute the following command:
cscript//nologo test.wsf/name: lilei/sex: male/age:34 "Got some trouble!"
Output:
1There is 3named arguments:2[' Name ']=Lilei3[' Sex ']=male4[' Age ']=345There is 1Unnamed arguments:6[0]: Gotsome trouble!7There is 4arguments in total:8/name: Lilei9/sex: MaleTen/age:34 One Got some trouble! AD:\Data\Persons.xml
It can be seen that if a WSF script contains more than one <job>, the code for all the <script> blocks of the first job is executed by default when no calling job is specified at the time of invocation. If you want to invoke the second job, you can execute cscript//nologo//job:vbs test.wsf, with the following results:
1 C:\Users\nutix\Desktop\test.wsf (+) Microsoft VBScript run-time error: Invalid procedure call or parameter: ' GetResource '
Obviously, there's no access to resource in//job:js.
4. Combining code in different languages
"1" refers to external code via SRC <script> block
1<package>2<job id="Includeexample">3<script language="JScript"Src="Getfreespace.js"/>4<script language="VBScript">5 'get the free space for drive C. 6s = Getfreespace ("C:")7 WScript.Echo S8</script>9</job>Ten</package>
Getfreespace.js's Code:
1 functionGetfreespace (drvpath) {2 varFS, D, S;3FS =NewActiveXObject ("Scripting.FileSystemObject");4D =FS. Getdrive (fs. GetDriveName (Drvpath));5s = "Drive" + Drvpath + "-" ;6s + =D.volumename;7s + = "free Space:" + d.freespace/1024 + "Kbytes";8 returns;9}
"2" directly writes different language code into the same WSF script file, which only needs to use the <script> block in multiple languages.
5.WSH file, which is a simple configuration file that functions like the contents of the Script tab of the right-click Properties script file, the configurable item is rarely very simple, here is an example:
1 [ScriptFile] 2 Path=c:\users\nutix\desktop\a.js3[Options]4 timeout=05 Displaylogo=16 batchmode=0
Note: The script file specified by path must exist and can be executed by WScript or cscript interpretation. Other settings: timeout: Set timeout, automatically terminate execution when timed out, Displaylogo: whether to display logo, Batchmode: whether to execute in batch mode
WSF script Explanation: Combining JS and VBS code to complete the job