Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Windows Administration How to automate Windows MSDN: “Windows Script Host (WSH) is a Windows Administration Tool.”"— Presentation transcript:

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

2 WSH – What it is? 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

3 WSH – What is good for? 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)

4 WSH – What is good for? 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.

5 WSH – What is good for? 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.

6 WSH – Which Language? 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 JavaScript.

7 WSH – Which language? 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.

8 WSH – Language comparison 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. JavaScript Visual Basic programs can be compiled into EXE files, this functionality isn't available with WSH. 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).

9 Visual Basic vs. WSH & JScript vs. JavaScript 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 run- time error handling (such as On Error GoTo label) Neither VBScript nor JScript supports explicit data type declarations; instead, they treat all variables as Variants.

10 Visual Basic vs. WSH & JScript vs. JavaScript 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

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

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

13 WSH - WScript & CScript //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

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

15 WSH – WSF files Text file as the normal.js or.vbs files which contains Extensible Markup Language (XML) code. A.wsf file has the following structure: WScript.Echo "Hello, world“

16 WSH – WSF files.wsf files makes possible to combine scripts from different languages. sub PerlHello { my $str = @_[0]; $WScript->Echo($str); } WScript.Echo "Hello from VBScript" PerlHello "Hello from PERLScript"

17 WSH – WSF files - includes Allow inclusion of files WScript.Echo "Hello, world again"> Include debugging features by using:

18 WSH – WSF files – type libraries The element in a.wsf file enables you to use constants defined in a type library in scripts ‘the GUID of the FileSystemObject WScript.Echo "CDRom = " & CDRom & vbCrLf & _ "Fixed = " & Fixed & vbCrLf & _ "RamDisk = " & RamDisk & vbCrLf & _ "Remote = " & Remote & vbCrLf & _ "Unknown = " & Unknown & vbCrLf & _ "Removable = " & Removable

19 WSH – Register a custom ActiveX 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: regsvr32.exe c:\Work\MyControl.ocx

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

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

22 WSH – UI - Output Echo, MsgBox for output. MsgBox prompt, buttons, title Popup method var WshShell = WScript.CreateObject("WScript.Shell"); var result; result = WshShell.Popup( Message, 0, Title, vbOKCancel + vbInformation);

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

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

25 WSH – UI – Input - Forms For getting the result the connected event handler should be used. Sub IE_OnQuit() ' Event handler is called if IE terminates. ' This happens if the user clicks the OK button. ' Retrieve the values. name = "Name: " & oIE.Document.ValidForm.fName.Value age = "Age: " & oIE.Document.ValidForm.fAge.Value ready = True ' Indicate form is closed. End Sub

26 WSH – WScript Object Application - Returns the IDispatch interface of the WScript object Arguments - Returns a collection object containing the script arguments FullName - Contains the full path to the host executable (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 ScriptFullName - The full path to the script that is currently running in WSH ScriptName - The filename of the script that is currently running in WSH Version - A string containing the WSH version (not the language engine version)

27 WSH – Environment variables Access to the environment variables Set WshShell = CreateObject("WScript.Shell") Set objEnv = WshShell.Environment("Process") Echo objEnv(„PATH“) Creating and releasing objects Set Object_variable = WScript.CreateObject("ProgID") or ‘Set objAdr = WScript.GetObject(strPath[, [strProgID][, strPrefix]]) WScript.DisconnectObject Object_name Object_Name = Nothing

28 WSH – Advanced WSH Creating Shortcuts Set WshShell = WScript.CreateObject("WScript.Shell") Set object = WshShell.CreateShortcut("shortcut_file.lnk") Retrieving User name, Domain... Set WshNetwork = WScript.CreateObject("WScript.Network") User = WshNetwork.UserName ' Read property… Mapping Network drives Set WshNetwork = WScript.CreateObject("WScript.Network") WshNetwork.MapNetworkDrive "X:", \\DE\C Accessing Registry WshShell.RegWrite "HKCR\FH\Test", "Hello, world", "REG_SZ"

29 WSH – Advanced WSH Using File System & IO FileSystemObject Manipulating Applications (VBA style) Use SendKeys to simulate keystrokes var WshShell = WScript.CreateObject("WScript.Shell"); WshShell.Run("Calc.exe"); WScript.Sleep(200); WshShell.SendKeys("10"); WshShell.SendKeys("{+}"); WshShell.SendKeys("2"); WshShell.SendKeys("=");

30 WSH The End


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

Similar presentations


Ads by Google