Windows Script Host Programming

Source: Internet
Author: User

From: http://dev.yesky.com/117/2482617.shtml

Windows Script Host programming Author: Zheng jianbiao Source: Tianji development responsibility Editor: Fangzhou

   1. Introduction


Wsh stands for Windows Script Host and is an independent scripting language that supports ActiveX (COM. The wsh script program was originally generated to replace the batch processing files in the DOS era. Later, due to the wide application of wsh, it is not just a substitute for batch processing. Since Windows 98, wsh is already included in the operating system. Therefore, wsh can be directly used without installing any software in Windows 98 or later operating systems. Microsoft has the latest wsh version on its homepage for download. The current version is 5.6, and there are two versions for Windows9x and Windows2000/xp. The installation files of each version cannot exceed 1 MB.

  2. Basic concepts of wsh script files

2.1 wsh script file type

Wsh can use VBScript and JScript, that is, the script language is the same as the web script. The wsh file extension is vbs and JS, and VBScript and JScript are used as programming script languages respectively. The extension of a script file is WSF, which is a file containing XML. WSF files can contain both VBScript and JScript. Multiple scripts can be used in WSF files. Some XML labels are required for declaration. The most basic method is to use the <SCRIPT> label to describe the script type, in addition, the outermost layer should use the <job> label to declare the task. The following is an example of the simplest WSF script framework:

<Package> <job>
<Script language = "VBScript">
Wscript. Echo "this is VBScript" 'vb script
</SCRIPT>
<Script language = "jscript">
Wscript. Echo ("this is JScript"); // Java Script
</SCRIPT>
</Job> </package>

Vbs and JS files can directly write VBScript and JScript statements without any additional content.

2.2 run the wsh script file:

All the three types of wshfiles mentioned in the preceding statement can be directly run in Windows. The corresponding execution file is the wscript.exe file. Another external program is cscript.exe, which can run under DOS. The listener is output in the standard Console mode, and DoS redirection can be performed.

In addition, wsh scripts support drag-and-drop operations on files. The specific operation steps are as follows: select several files and drag them to the files containing the following script. Then, the following vbs script will be executed, get the dragged file names, and display them:

Set objargs = wscript. Arguments
For I = 0 to objargs. Count-1
Wscript. Echo objargs (I)
Next

2.3 Main wsh objects

Wsh scripts do not support API functions. In addition to some scripting languages, wsh also provides some programming objects that can be used, you can easily perform functions that are not provided by the script language, such as system and network operations.

2.3.1 wscript object

A wscript object is a wsh object. It can be directly used without being created in the script. The most common method of Createobject is to create a COM Object and call other COM programs. The echo method, quit method, sleep method, path attribute, and scriptfullname attribute of wsh can be used in the script to facilitate script writing.

2.3.2 wshshell object

Wshshell objects are the most useful objects provided by wsh. Many system functions can only be implemented through APIS. wshshell can be implemented simply. It can be created using the wscript. Createobject ("wscript. Shell") method. Its common methods and attributes include: The createshortcut method is used to create a shortcut or URL Shortcut; the run method is used to execute the program, which is more powerful than the shell command function of VB, you can directly open associated files, folders, webpages, etc. The regdelete/regread/regwrite method is very useful for deleting, reading, and writing registry entries; the sendkeys method sends the specified key sequence to the activity window. The specialfolders attribute returns special folder names, such as desktops and my documents.

2.3.3 wshnetwork object

The wshnetwork object mainly involves network and printer functions. Its main methods and attributes include: creating or deleting Network Printer connections, ing and deleting network drives, enumeration of network drives and network printers.

2.3.4 wshshortcut/wshurlshortcut object:

Wshshortcut/wshurlshortcut objects cannot be generated by the Createobject method, but are created by the createshortcut method of wshshell. Set the attributes of the shortcut and use the Save method to create the shortcut.

2.3.5 FSO object:

FSO is short for file system object. It provides operations related to disks, folders, and files, which are generated by wscript. Createobject ("scripting. FileSystemObject. FSO objects are no stranger to those who have used dynamic web pages, and background programs basically use FSO for file operations. Because VBScript is a simple version of VB, some functions of VB are removed, and all file functions are removed, therefore, the script can only use the com method to call the FSO object to solve folder and file operations. Strictly speaking, FSO objects are not carried by wsh, but by VBScript. However, wsh itself carries VBScript, and FSO plays a very important role in wsh, therefore, wsh has FSO, which is not a problem. However, there are a lot of articles about FSO, so I will not elaborate on it here.

2.3.6 call other objects:

Wsh can call other COM objects, so as long as the software installed on the machine and the COM server mode is provided, it can be called in wsh. Including ODBC database operation objects and objects in various EXE methods. For example, you can directly operate the word "word. Application" object of word. That is, the objects that wsh can use are infinite.

2.4 edit the wsh File

Since the wsh file is a plain text file, it can be edited theoretically in any text editor. However, some web page creation software now provides smart prompts for attributes and methods for some script programs, so it is much easier to select some of these tools. Among them, Microsoft's Visual Interdev is a good choice. After all, it is a Microsoft product with wsh, and it is better to cooperate with each other. Of course, if the input is based on the webpage, some HTML tags may be removed.
2.5 script example

The following is an example of a software installation script that completes file copying, creating shortcuts on the desktop, and modifying registry settings to enable automatic startup. The program has detailed annotations, and you can understand the basics of VB. To install a software, you only need to save the following program as a vbs file, which is in the same directory as the file to be installed, double-click the machine you want to install.

'Create two objects:
Set owsh = wscript. Createobject ("wscript. Shell ")
Set ofso = wscript. Createobject ("scripting. FileSystemObject ")
'Prompt target folder
Cdest = inputbox ("target folder to be installed:", "Install", "C:/XXX ")
'The script folder is the source folder:
 = ofso. GetFile (wscript. scriptfullname). parentfolder
'Copy the file to the target Folder:
Ofso. copyfile  & "*. *", cdest
'Create a shortcut on the desktop:
Cdesktop = owsh. specialfolders ("desktop") 'desktop folder
Set olink = owsh. createshortcut _
(Cdesktop & "/xx management system. lnk") 'create a shortcut object
Olink.tar getpath = cdest & "xx.exe" 'target file
Olink. windowstyle = 1' running mode: regular window
Olink. hotkey = "Ctrl + Shift + F" 'hotkey
Olink. workingdirectory = cdesktop 'working directory
Olink. save' to generate a shortcut file
'Write the program to the auto-run entry of the Registry to enable the program to run automatically when it is started.
CREG = "HKEY_CURRENT_USER/software/Microsoft/Windows/CurrentVersion/run /"
Owsh. regwrite CREG, cdest & "xx.exe" 'file write automatically run registry key
Msgbox "program installation completed"

2.6 encryption of script files

Because the script file is a plain text file, anyone can directly view the code without any tools. Like Web file encryption, wsh script files can also be encrypted using the script encoder provided by Microsoft. Screnc can be directly downloaded and installed for free. It is a command line software. The vbs and JS files are respectively encoded into VBE and je files. You can also double-click these files to run them, although the Code after encoding is still a text file, the content in it has been disrupted. The encoded file cannot be modified. You only need to modify it. Even if only one byte is modified, the entire script cannot be used. Although the encoder can disrupt the code and make people unable to read or modify the code, it can only be regarded as a simple tool to prevent the gentleman from defending against the villain, it is useless to deal with real decryption experts or hackers.

  3. wsh Application

3.1 Replace the batch processing file

Wsh was launched to replace the batch processing commands in the DOS era. In the preceding example, you can double-click the corresponding file and run the prepared program to implement the installation function. This method is mainly used for some simple environment system settings, temporary compilation of small tools and other programs with certain logic functions. Since the functions are much more powerful than batch processing, writing and running are easier than using a specific programming language, and it is easy to modify without the development environment or compilation process. Therefore, you can master wsh scripts, it still plays a role for programmers.

3.2 serve as a COM interface

Another purpose of wsh is to serve as a COM interface program to be called by other programming languages. It can provide functions not provided by this language itself. Generally, functions that are not provided in programming languages must be implemented through APIS, but it is troublesome to use APIs after all. The object functions provided by wsh are useful and can replace APIs.

As a standard COM interface program, the wsh objects described above, except the wscript objects, other objects can be used directly in other programming languages. For example, in VB, you can directly use the Createobject function to create an object, and then use its methods and attributes to operate it.

  4. wsh security issues

Wsh script programs are simple, but most of the tasks that can be done in advanced languages are basically available, so many viruses also use this and wsh is used to compile virus programs. Some viruses even send a virus-infected vbs attachment by email. Once you double-click the attachment file, it will infect the virus. So for a while, wsh was quite chilling, and many people regarded it as the culprit of the virus. In many cases, we have introduced how to delete wsh to prevent viruses. In fact, we don't need to be so scared. Although wsh can be deleted to prevent some viruses from being infected by wsh, it cannot fundamentally solve the problem. If you receive the problem of direct access to the vbs file, you can open an EXE file. Is it allowed for the machine to disable the EXE file? In fact, as long as you know that files such as vbs, JS, wsh, and WSF can be directly run, there is danger, and it is doubtful to receive such files from others. In the same way, wsh does bad things for viruses in the form of components and helps others to abuse them. Disabling wsh can only solve some problems. What about other components? Setting the security level in the IE option is very important. It is not safe if the security level is too low. In addition, upgrading wsh to version 5.6 can also improve the security factor, and many versions of vulnerability 5.6 have been blocked. Of course, if you do not need wsh, it is also a way to disable it. The Windows2000 method is to remove the association of vbs/JS and other files, and rename or delete the wscript.exeand cscript.exe files.

  5. Conclusion

Wsh uses a scripting language. Although the function is not as powerful as a formal programming language, wsh can write source programs and run them directly because the system is built in and requires simplicity, in addition, the scripting language is very similar to common VB and Java, so it is widely used. Such as some simple software installation, maintenance of system machines, and service functions for other software. However, its disadvantage is that some machines have deleted wsh to prevent viruses, so they do not have to install software, and the advantages that all machines can use may not exist, install the system if you want to use it on some machines.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.