|
Batch Scripts for WindowsThis page summarizes use of Microsoft Windows Script Technologies to perform activites without operation intervention by coding scripts run on an interpreter or “host” such as Active Server Pages (ASP), Internet Explorer, or Windows Script Host. | Topics this page:
|
Related Topics:
|
Scripting Languages
Command Prompt .bat filesThe Microsoft Windows DOS operating system always had .bat files containing traditional DOS commands File with a .Bat or .cmd extension are still used to invoke scripting hosts..wsh Host Control File.wsh files are text documents the controls the run-time execution of scripts. Their function is analogous to the PIF file used to run older 16-bit applications. The .wsh file is created automatically when properties are set for a supported script file (below).Logon files (introduced with WSH 2) are read by Windows 2000 PDC's and BDC's integrated with the Windows 2000 OS. Commands within .wsh files may access system services through requests for COM components to be implemented (what interface handlers to use). Sample .wsh logon script .js JScript (Javascript/ECMAScript)JScript files (with a .js file suffix and under “JSFile” in the Registry) contain the Microsoft implementation of the ECMA 262 language specification (ECMAScript Edition 3) — also called Javascipt. Unlike VBScript, Javascript has no built-in support for reading or writing files. To remedy this, the Microsoft FSO (File System Object) model to process (detect, add, move, change, create, or delete) individual of a collection of files, folders, and drives. For security, the Internet Explorer default security settings do not allow client-side use of FSO objects. The Scripting type library (Scrrun.dll) supports text file creation and manipulation through the TextStream object. JScript Sample code:
var fso = new ActiveXObject("Scripting.FileSystemObject"); // Invoke the method: var a = fso.CreateTextFile("c:\\testfile.txt", true); // Do something with it: a.WriteLine("This is a test."); // Close the connection: a.Close(); The MS.NET (5.6) version of JScript developed in conjunction with ECMAScript Edition 4 includes access to the .NET Frameworks as well as code compilation, typed and typeless variables, classes (with inheritance, function overloading, property accessors, etc.), packages, and cross-language support. JScript Language Reference (from the Windows 2000 Professional CD. folder VALUADD/ MSFT/ XTRADOCS) .vbs VBScriptVBScript files (with a .vbs file suffix and under “VBSFile” in the Registry) is officially called “Microsoft® Visual Basic® Scripting Edition” language. It can use the same Script Components to reference COM components used to control Microsoft's IIS web servers.But unlike VBA, VBScript v5 supports run-time Regular Expression (RegExp), Eval of assignment expressions and and Evaluate on comparison expressions that tests whethre variables have the same value. VBScript Language Reference (from the Windows 2000 Professional CD folder VALUADD/ MSFT/ XTRADOCS) Run sample VBScript file viewall.vbs (a compilation of .vbs files from the Windows 2000 Resource Kit CD) .wsf Windows Script properties FilesProvide flexibility. Only from within .wsf files can ...
Other Script Languages |
Windows Scripting Technologies Product Version History
Windows Vista introduces the PowerShell [subscribers only]
|
Script Execution SecurityClick here to have your Internet Explorer (IE browser) Open (and Run) the wsh_info.vbs script file which displays information about your computer in a pop-up box. These are called intrinsic objects. One way to disable the execution of ALL such files is from within Windows Explorer. Right-click on a .vbs file (of Type “VBScript Script File”) and select Open With. Instead of being executed by the “Microsoft Windows Based Script Host” program, Choose Program... and select a program that cannot execute files, such as “Notepad” or “Wordpad”. This will cause the script file to be opened for editing. This action changes the Open registry key under HKEY_CLASSES_ROOT\ VBSFile\ Shell to
%SystemRoot%\System32\CScript.exe "%1" %* With this change, if you need to execute a VBScript, in MS Windows Explorer, right click on the file it and select Execute VBScript. Repeat the above actions for two other extensions:
.wsh (in Reg. key HKCR\WSHFile) HTML Applications (HTAs) introduced with IE5 are "not subject to the same security constraints as Web pages" Hackers exploited this back door with the Winmain.exe exploit By default, Windows automatically enables HTA on bootup via registry entry HKLM / SOFTWARE / classes / htafile / Shell / Open / Command value C:\WINNT\System32\mshta.exe "% 1" %* Since this runs in the trusted Local zone, parses and runs scripts despite firewall and/or IE restrictions. Free HTAStop2003 blocks and permits HTA apps. $60 IEClean from Privacy Software prevents additional exploits. |
Script Encoder
Microsoft provides a Script Encoder command-line tool (screnc.exe) to encode (not really compile) scripts to make them more difficult to view or modify. Encoded scripts are also quicker to download. An example encoded script to be inserted between <HEAD> tags in a HTML file: <SCRIPT language="JScript.Encode"> <!--// //**Start Encode**mDkWP7nDb0zZKD.n1YAMGhk+Dv ZKD.n1Y AMGhk+Dv //--> </SCRIPT> The function defined by this script would simply not execute if even one character of the encoding is changed during transmission. So you may want to add code to detect whether script code was actually executed.
|
Script Debuggers
By default, the Internet Explorer Script Debugger is invoked. "<SCRIPT>" must be placed at the beginning and "</SCRIPT>" at the end of the file. Visual Studio's Visual InterDev product is better because it handles This command enables error messages and launching of the script debugger when a syntax or run-time error is encountered in a Windows Script file:
|
Brent Lane's "TNQ300-12 How To Administer Windows 2000 Using Windows Script Host 2.0 (Part A and B)" on the TechNet Training CD 12. Code samples are from The Microsoft Script Technologies Home Page (previously at URL msdn.microsoft.com/ scripting) is now redirected to under MSDN > Web Development > Scripting. msdn.microsoft.com/voices/scripting monthly columns and news:// msnews. microsoft.com/ public. scripting. wsh. The windowsscript communities at msn.com communities.msn.com/windowsscript (originally wsh.glazier.co.nz) is focused on using WSH for network logon scripts. NT administrators who are not programmers have the opportunity to learn some new technology and experienced programmers can share their sample code. There is also a lot of information on COM objects that can be used with WSH.
|
Message Dialog BoxVBScript provides for a more sophisticated dialog box:
Syntax: Example: To show a question icon with OK and Cancel buttons in the foreground:
|
Input Dialog Box VBScriptVBScript Syntax:result = InputBox( prompt[, [title], [default], [xpos], [ypos]]) Example: To prompt user for a text string: Use ADSI to add Users to a Group from an Excel file
set objGroup= GetObject("@Namespace!Company\Group1") for each objUser in objDS objGroup.GeneralInfo.Members.Add (objUser) next objUser |
Objects
Built-In ObjectsXML Elements |
ADSI/(Active Directory Service Interfaces) Directory COM Objects
|
Logon Scripts
Make Windows wait for scripts to finishTo make Windows NT/2000/XP wait for logon scripts to complete before it starts the user shell (rather than letting the scripts should run asynchronously), find registry key
(HKEY_LOCAL_MACHINE is application for all users. So to limit this restriction per user, add the value to HKEY_CURRENT_USER) Edit or create the DWORD value 'RunLogonScriptSync' 0 = disabled, 1 = enabled http://www.win2000mag.com/Articles/Index.cfm?Articleid=4760 Sams Teach Yourself Windows Script Host in 21 Days by Fredell, Thomas L.; Morrison, Michael ISBN: 067231374X Indianapolis, Ind. Sams, 1999.
|
More Resources
Microsoft Scripting MVPs
|
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page
Thank you! |