Figure 1 WScript Object Properties
Property | Description | Arguments | Returns the collection of arguments that the script has received on the command line. The collection is rendered through a specialized WshArguments object. | FullName | Returns the fully qualified path of the host executable. | Interactive | Gets and sets the script working mode, be it interactive or batch. It is the programmatic counterpart of the //I command line switch. | Name | Returns the name of the WScript object, namely the string "Windows Script Host." | Path | Returns the name of the directory containing the host executable. | ScriptFullName | Returns the full path of the currently running script. | ScriptName | Returns the file name of the currently running script. | StdErr | Returns a TextStream object that renders the write-only error output stream for the current script (only for CScript.exe). | StdIn | Returns a TextStream object that renders the read-only input stream for the current script (only for CScript.exe). | StdOut | Returns a TextStream object that renders the write-only output stream for the current script (only for CScript.exe). | Version | Returns the version of WSH installed, for example, 5.6. | Figure 2 WScript Object Methods
Method | Description | CreateObject | Creates a new instance of a COM object given its ProgID. | | It can take an optional string argument indicating the function prefix in case you want to sink events exposed by the object. | ConnectObject | Associates the specified function prefix string with the given object instance.It allows you to sink events exposed by an object not created through WScript.CreateObject. | DisconnectObject | Cancels the object/prefix association set through a previous call to ConnectObject. After this method is called, the script stops handling the events fired by that object instance. | Echo | Outputs text to either a message box or the command console window depending on the running WSH host. The method can take any number of parameters that will be displayed one after the next, separated by a space. If no arguments are provided, a blank line is output. | GetObject | Retrieves an existing object with the specified ProgID, or creates a new one from the given file. The argument strFile is required and is the fully qualified path to the file where the object to load has been persisted. It can be the empty string, but in this case strProgID is mandatory. The optional argument strPrefix indicates the function prefix in case you want to sink events exposed by the object. | Quit | Terminates the execution of the script. No line after it is ever reached. It's not strictly needed if it is the final instruction of your script. The method may optionally return an error code. | Sleep | Suspends script execution for the specified number of milliseconds, then resumes. The thread running the script is suspended and so is its CPU utilization. A script suspended with the Sleep method can only be awakened by the corresponding timeout. | Figure 3 WshShell Methods
Method | Description | AppActivate | Activates an application window. The window to activate is retrieved from the title on the caption bar. The method changes the focus to the given window, but it does not affect whether it is maximized or minimized. | CreateShortcut | Opens the specified shortcut file. If such a file doesn't exist, a new shortcut is created. The method returns either a WshShortcut object or a WshURLShortcut object. Simply calling this method does not result in the creation of a shortcut. | Exec | Runs an application in a child MS-DOS shell and provides access to its StdIn, StdOut, and StdErr streams. The Exec method returns a WshScriptExec object and cannot be used to run remote scripts. | ExpandEnvironmentStrings | Returns an environment variable's expanded value. Environment variable names must be enclosed between % characters, for example, %WINDIR%. | LogEvent | Adds an event entry to a log file specifying the type of message and the target machine. In Windows NT/2000, events are logged in the Windows NT Event Log. | Popup | Displays text in a pop-up message box. It's similar to MsgBox, and allows for a timeout. | RegDelete | Deletes a key or one of its values from the registry. It indicates a value name except when the string ends with a backslash. If so, it deletes the matching key, if any. | RegRead | Returns the value of a key or a value from the registry. It indicates a value name except when the string ends with a backslash. If so, it reads the default value of the matching key, if any. | RegWrite | Used to create a new key, add a new value to an existing key, or change an existing value. Strings and numbers are the only values that can be written. | Run | Executes the specified program creating a new process. This method has the ability to wait for the specified program to terminate. | SendKeys | Sends one or more keystrokes to the active window. Executing this method for the target window has the same effect as typing on the keyboard. You use special strings to indicate the keystroke. | Figure 4 Policy Editor .gif) Figure 5 Access Restricted .gif) Figure 7 Certificate Information .gif) Figure 8 TrustPolicy Values
Value | Description | 0 | Run all scripts from any source. | 1 | Prompt the user if WSH is asked to run an untrusted script. | 2 | Run only trusted scripts and discard all others. | Figure 9 Scripting.Signer Methods
Method | Description | Sign | Signs a script stored in a string. The Extension argument designates the script extension type (.vbs, .js, .wsf). You also provide the certificate name and, optionally, the certificate store. | SignFile | Signs a script using a certificate. You provide the name of the script, the certificate and, optionally, the certificate store. | Verify | Verifies a digital signed script stored in a string. The Extension argument designates the script extension type (.vbs, .js, .wsf). The ShowUI argument controls whether a dialog box should be created in case of insufficient information. | VerifyFile | Verifies the digital signature in a script file. You provide the script file name and a boolean flag that indicates whether a dialog box should be used to prompt for more trust information. | Figure 10 WshRemote Object
Property | Description | Error | Exposes the WshRemoteError object, which holds information about the error that caused the remote script to terminate prematurely. | Status | Indicates the current status of the remote script. | Method | Description | Execute | Starts execution of a remote script object. | Terminate | Raises a request for the remote script process to terminate. However, it is always a good programming practice to let your remotely running scripts terminate on their own. | Event | Description | Start | Fires when the remote script begins executing. | End | Fires when the remote script terminates execution. | Error | Fires when an error occurs in the remote script. | Figure 11 WSH Remote Script Status
Value | Description | NoTask | Indicates that the WshRemote object is created but not yet in execution. Corresponds to an integer value of 0. | Running | Indicates that the WshRemote script is currently running. Corresponds to an integer value of 1. | Finished | Indicates the WshRemote script is finished running. Corresponds to an integer value of 2. | Figure 12 Event-driven Status Update Set controller = CreateObject("WScript.WshController")
Set remscript = controller.CreateScript("script.vbs", "server")
WScript.ConnectObject(remscript, "RemoteScript_")
remscript.Execute();
bDone = False
While Not bDone
WScript.Sleep(100)
Wend
WScript.Quit
' =========== Subroutines =====================
Function RemoteScript_End()
WScript.Echo("The process has ended") ' Runs on the caller!
bDone = True
End Function
Function RemoteScript_Error()
WScript.Echo("Error occurred: " & RemoteScript.Error.Description)
bDone = True
End Function
Function RemoteScript_Start()
WScript.Echo("The process has started")
End Function
Figure 13 Runtime Element Options
Element | Description | <Named> | Describes an expected named argument for the job. It includes attributes for the argument's name, type, usage, and help string. | <Unnamed> | Describes an unnamed argument for the job. It includes attributes for the argument's name, cardinality, usage, and help string. | <Description> | Represents the official description of the job, namely the descriptive text that is displayed when the job is run with the /? command line switch. | <Example> | Provides a string that shows how to use the job. | <Usage> | Contains an explicit usage text that overrides both the <description> and the <example> elements. | Figure 14 Generate Message Box <job>
<runtime>
<named name="Server"
helpstring="Indicates the machine to access"
type="string"
required="true"
/>
<named name="Drive"
helpstring="If specified, displays information for the drive."
type="simple"
required="false"
/>
<named name="Verbose"
helpstring="If specified, output will be verbose."
type="boolean"
required="false"
/>
</runtime>
<script language="VBScript">
WScript.Arguments.ShowUsage
</script>
</job>
Figure A CompileInMemory Function void CompileInMemory(String csfile)
{
ICodeCompiler icc = (new CSharpCodeProvider()).CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("system.dll");
cp.GenerateExecutable = true;
cp.GenerateInMemory = true;
StreamReader sr = new StreamReader(csfile);
String source = sr.ReadToEnd();
sr.Close();
CompilerResults cr;
cr = icc.CompileAssemblyFromSource(cp, source);
Assembly a = cr.CompiledAssembly;
}
|