Windows Administration How to automate Windows MSDN Windows

  • Slides: 30
Download presentation
Windows Administration How to automate Windows MSDN: “Windows Script Host (WSH) is a Windows

Windows Administration How to automate Windows MSDN: “Windows Script Host (WSH) is a Windows Administration Tool. ”

WSH – What it is? n n n a powerful tool for programming at

WSH – What it is? n n n a powerful tool for programming at the operating system level. Is the environment for hosting scripts - it makes objects and services visible to the scripts It is language independent for WSH compliant scripting engines

WSH – What is good for? n n n Back up or restore files

WSH – What is good for? n n n Back up or restore files on your system. Shut down or restart Windows with a mouse click. You can also use a script to add special shutdown or startup tasks (i. e. : backing up certain files after closing applications or logging a user's name after booting the system) Integrate applications and their data. (i. e. , a script can launch an Office application, load and process a document, print it, and close the application)

WSH – What is good for? n n n Manage system administration tasks (i.

WSH – What is good for? n n n Manage system administration tasks (i. e. : adding, updating, and removing user accounts in Windows NT and Windows 2000 Directly access the Windows shell through suitable objects (to create shortcuts or map network devices such as drives or printers) Read environment variables or retrieve information about Windows.

WSH – What is good for? n n n Launch programs and control Automation

WSH – What is good for? n n n Launch programs and control Automation objects. Display dialog boxes that inform the user about the program status or retrieve user input. Access the Windows shell and Windows application programming interface (API) to control windows and other applications.

WSH – Which Language? n n VBScript uses the same syntax as Visual Basic;

WSH – Which Language? n n VBScript uses the same syntax as Visual Basic; it is actually a subset of Visual Basic. JScript is Microsoft's implementation of ECMAScript, the vendor-independent programming language based on Java. Script.

WSH – Which language? n WSH offers an open interface so that third-party vendors

WSH – Which language? n WSH offers an open interface so that third-party vendors can integrate their own language engines to support other languages such as Perl, Tool Control Language (Tcl), and Rexx.

WSH – Language comparison n For users that used Visual Basic or VBA and

WSH – Language comparison n For users that used Visual Basic or VBA and JScript for HTML pages writing WSH scripts shouldn‘t be any problem Visual Basic vs. WSH and JScript vs. Java. Script n Visual Basic programs can be compiled into EXE files, this functionality isn't available with WSH. n In WSH, simple text files with extensions such as . vbs, . js, or. wsf. Are used. You can prepare your scripts using a simple text editor (such as Notepad).

Visual Basic vs. WSH & JScript vs. Java. Script n n n VBScript and

Visual Basic vs. WSH & JScript vs. Java. Script n n n VBScript and JScript doesn‘t include the Declare statement in Visual Basic and VBA, that allow access to external functions and procedures VBScript doesn't include routines for extended runtime error handling (such as On Error Go. To label) Neither VBScript nor JScript supports explicit data type declarations; instead, they treat all variables as Variants.

Visual Basic vs. WSH & JScript vs. Java. Script n n n The WSH

Visual Basic vs. WSH & JScript vs. Java. Script n n n The WSH environment doesn't provide an extended user interface (as Internet Explorer does) the WSH object model doesn't expose any user-interface events (such as onclick in HTML scripts) However, WSH does support event handling

WSH – Hello World VBScript WScript. Echo "Hello, world" JScript WScript. Echo ("Hello, world“);

WSH – Hello World VBScript WScript. Echo "Hello, world" JScript WScript. Echo ("Hello, world“);

WSH – Running scripts n Double click the script file n For better control

WSH – Running scripts n Double click the script file n For better control run them using n n WScript. exe (Windows-based host for scripts) CScript. exe (Console application-based host for scripts)

WSH - WScript & CScript n n n //I - //B – Enable/disable interactive

WSH - WScript & CScript n n n //I - //B – Enable/disable interactive mode //T: nn – the script is not allowed to run more than „nn“ seconds //S – saves the command line options //D – enable debugging //E: engine //Job: xxx

WSH - WScript & CScript n n n //X – Runs the script in

WSH - WScript & CScript n n n //X – Runs the script in the debugger //logo - //Nologo //H: CScript - //H: WScript

WSH – WSF files n Text file as the normal. js or. vbs files

WSH – WSF files n Text file as the normal. js or. vbs files which contains Extensible Markup Language (XML) code. A. wsf file has the following structure: <job id="T 1"> <script language="VBScript"> WScript. Echo "Hello, world“ </script> </job>

WSH – WSF files n . wsf files makes possible to combine scripts from

WSH – WSF files n . wsf files makes possible to combine scripts from different languages. <job id="PERLand. VBS"> <script language="Perl. Script"> sub Perl. Hello { my $str = @_[0]; $WScript->Echo($str); } </script> <script language="VBScript"> WScript. Echo "Hello from VBScript" Perl. Hello "Hello from PERLScript" </script> </job>

WSH – WSF files - includes n Allow inclusion of files <? xml version="1.

WSH – WSF files - includes n Allow inclusion of files <? xml version="1. 0" encoding="ISO-8859 -1"? > <job id="Task 1"> <script language="JScript" src="Hello. js"/> <script language="VBScript"> WScript. Echo "Hello, world again"> </script> </job> Include debugging features by using: <? job debug="true"? > n

WSH – WSF files – type libraries n The <reference> element in a. wsf

WSH – WSF files – type libraries n The <reference> element in a. wsf file enables you to use constants defined in a type library in scripts <job id="Type. Lib. Example"> <reference guid='{420 B 2830 -E 718 -11 CF-893 D-00 A 0 C 9054228}'/> ‘the GUID of the File. System. Object <script language="VBScript"> WScript. Echo "CDRom = " & CDRom & vb. Cr. Lf & _ "Fixed = " & Fixed & vb. Cr. Lf & _ "Ram. Disk = " & Ram. Disk & vb. Cr. Lf & _ "Remote = " & Remote & vb. Cr. Lf & _ "Unknown = " & Unknown & vb. Cr. Lf & _ "Removable = " & Removable </script> </job>

WSH – Register a custom Active. X n If the need to use other

WSH – Register a custom Active. X n If the need to use other functionalities than those exposed by Excel, Word, WSH or Windows objects, custom objects can be created. Before it can be used by WSH the object has to be registered: regsvr 32. exe c: WorkMy. Control. ocx

WSH – VBScript – Basic features n n n n Statements: v = v

WSH – VBScript – Basic features n n n n Statements: v = v + 1 Continued lines : „bla bla“ & _ Comments: ‘comment Variables, constants, intrinsic constants (variants, Option Explicit) Public Private Control statements Procedure calls - pitfalls

WSH – VBScript – Advanced features n n Error handling Regular expresions Classes With

WSH – VBScript – Advanced features n n Error handling Regular expresions Classes With statement

WSH – UI - Output n Echo, Msg. Box for output. Msg. Box prompt,

WSH – UI - Output n Echo, Msg. Box for output. Msg. Box prompt, buttons, title n Popup method var Wsh. Shell = WScript. Create. Object("WScript. Shell"); var result; result = Wsh. Shell. Popup( Message, 0, Title, vb. OKCancel + vb. Information);

WSH – UI - Input n Getting user input using Input Box result =

WSH – UI - Input n Getting user input using Input Box result = Input. Box(“Please Input the path”, ”Our Test”, “c: Winodws”, 100)

WSH – UI – Input - Forms n For displaying more complicated forms Internet

WSH – UI – Input - Forms n For displaying more complicated forms Internet Explorer is used as front end (Internet Explorer Objects are used) Set o. IE = Create. Object("Internet. Explorer. Application", "IE_") ' window position and other properties o. IE. Left = 50. . o. IE. navigate path & "Form 1. htm" ' Form o. IE. Visible = 1 ' Show document window.

WSH – UI – Input - Forms n For getting the result the connected

WSH – UI – Input - Forms n For getting the result the connected event handler should be used. Sub IE_On. Quit() ' Event handler is called if IE terminates. ' This happens if the user clicks the OK button. ' Retrieve the values. name = "Name: " & o. IE. Document. Valid. Form. f. Name. Value age = "Age: " & o. IE. Document. Valid. Form. f. Age. Value ready = True ' Indicate form is closed. End Sub

WSH – WScript Object n Application - Returns the IDispatch interface of the WScript

WSH – WScript Object n Application - Returns the IDispatch interface of the WScript n Arguments - Returns a collection object containing the script n Full. Name - Contains the full path to the host executable n n n object arguments (CScript. exe or WScript. exe) Name - The friendly name of WScript (This is the default property. ) Path - The name of the directory in which the host (WScript. exe or CScript. exe) resides Script. Full. Name - The full path to the script that is currently running in WSH Script. Name - The filename of the script that is currently running in WSH Version - A string containing the WSH version (not the language engine version)

WSH – Environment variables n Access to the environment variables Set Wsh. Shell =

WSH – Environment variables n Access to the environment variables Set Wsh. Shell = Create. Object("WScript. Shell") Set obj. Env = Wsh. Shell. Environment("Process") Echo obj. Env(„PATH“) n Creating and releasing objects Set Object_variable = WScript. Create. Object("Prog. ID") or ‘Set obj. Adr = WScript. Get. Object(str. Path[, [str. Prog. ID][, str. Prefix]]) WScript. Disconnect. Object_name Object_Name = Nothing

WSH – Advanced WSH n Creating Shortcuts n Retrieving User name, Domain. . .

WSH – Advanced WSH n Creating Shortcuts n Retrieving User name, Domain. . . n Mapping Network drives n Accessing Registry Set Wsh. Shell = WScript. Create. Object("WScript. Shell") Set object = Wsh. Shell. Create. Shortcut("shortcut_file. lnk") Set Wsh. Network = WScript. Create. Object("WScript. Network") User = Wsh. Network. User. Name ' Read property… Set Wsh. Network = WScript. Create. Object("WScript. Network") Wsh. Network. Map. Network. Drive "X: ", \DEC Wsh. Shell. Reg. Write "HKCRFHTest", "Hello, world", "REG_SZ"

WSH – Advanced WSH n Using File System & IO File. System. Object n

WSH – Advanced WSH n Using File System & IO File. System. Object n n Manipulating Applications (VBA style) Use Send. Keys to simulate keystrokes var Wsh. Shell = WScript. Create. Object("WScript. Shell"); Wsh. Shell. Run("Calc. exe"); WScript. Sleep(200); Wsh. Shell. Send. Keys("10"); Wsh. Shell. Send. Keys("{+}"); Wsh. Shell. Send. Keys("2"); Wsh. Shell. Send. Keys("=");

WSH The End

WSH The End