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

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

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

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

Windows Administration

How to automate Windows

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

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

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

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

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)

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

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.

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

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.

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

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.

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

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.

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

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).

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

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.

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

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

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

WSH – Hello World

VBScriptWScript.Echo "Hello, world"

JScriptWScript.Echo ("Hello, world“);

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

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)

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

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

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

WSH - WScript & CScript

//X – Runs the script in the debugger

//logo - //Nologo //H:CScript - //H:WScript

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

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:

<job id="T1">     <script language="VBScript">         

WScript.Echo "Hello, world“     </script> </job>

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

WSH – WSF files .wsf files makes possible to combine

scripts from different languages.<job id="PERLandVBS"> <script language="PerlScript"> sub PerlHello { my $str = @_[0];

$WScript->Echo($str); }

</script> <script language="VBScript">

WScript.Echo "Hello from VBScript" PerlHello "Hello from PERLScript"

</script> </job>

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

WSH – WSF files - includes Allow inclusion of files

<?xml version="1.0" encoding="ISO-8859-1"?> <job id="Task1">     <script language="JScript" src="Hello.js"/>     <script language="VBScript">  

WScript.Echo "Hello, world again">     </script> </job>

Include debugging features by using:<?job debug="true"?>

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

WSH – WSF files – type libraries

The <reference> element in a .wsf file enables you to use constants defined in a type library in scripts

<job id="TypeLibExample"><reference guid='{420B2830-E718-11CF-893D-00A0C9054228}'/>‘the GUID of the FileSystemObject    <script language="VBScript">         WScript.Echo 

"CDRom = " & CDRom & vbCrLf & _"Fixed = " & Fixed & vbCrLf & _

"RamDisk = " & RamDisk & vbCrLf & _"Remote = " & Remote & vbCrLf & _"Unknown = " & Unknown & vbCrLf & _"Removable = " & Removable

</script> </job>

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

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

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

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

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

WSH – VBScript – Advanced features

Error handling Regular expresions Classes With statement

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

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);

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

WSH – UI - Input Getting user input using Input Boxresult = InputBox(“Please Input the path”,”Our Test”,

“c:\Winodws”, 100,100)

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

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 propertiesoIE.Left = 50....oIE.navigate path & "Form1.htm"  ' FormoIE.Visible = 1         ' Show document window.

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

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

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

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)

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

WSH – Environment variables Access to the environment variables

Set WshShell = CreateObject("WScript.Shell")Set objEnv = WshShell.Environment("Process") Echo objEnv(„PATH“)

Creating and releasing objectsSet Object_variable = WScript.CreateObject("ProgID") or‘Set objAdr = WScript.GetObject(strPath[, [strProgID]

[, strPrefix]])

WScript.DisconnectObject Object_name Object_Name = Nothing

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

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 drivesSet WshNetwork = WScript.CreateObject("WScript.Network") WshNetwork.MapNetworkDrive "X:", \\DE\C

Accessing RegistryWshShell.RegWrite "HKCR\FH\Test", "Hello, world", "REG_SZ"

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

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("=");

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

WSH

The End