5.5.2

Table Of Contents
n
If the returned object is of type complex, the object is represented as PSObject.
Examples of Scripts for Common PowerShell Tasks
You can cut, paste, and edit the JavaScript examples to write scripts for common PowerShell tasks.
For more information about scripting, see the vCenter Orchestrator Developer's Guide.
Example: Run a PowerShell Script Through the API
You can use JavaScript to run a PowerShell script through the API of the plug-in.
This example script performs the following actions.
n
Opens a session to a PowerShell host.
n
Provides a script to run.
n
Checks invocation results.
n
Closes the session.
var sess;
try {
//Open session to PowerShell host
var sess = host.openSession()
//Set executed script
var result = sess.invokeScript('dir')
//Check for errors
if (result.invocationState == 'Failed'){
throw "PowerShellInvocationError: Errors found while executing script \n" +
result.getErrors();
}
//Show result
System.log( result.getHostOutput() );
} catch (ex){
System.error (ex)
} finally {
if (sess) {
//Close session
host.closeSession( sess.getSessionId() );
}
}
Example: Work with Result
You can use JavaScript to work with the result of a PowerShell script run.
This example script performs the following actions.
n
Checks the invocation state.
n
Extracts a value from the result.
n
Checks the RemotePSObject type.
var sess = host.openSession()
sess.addCommandFromString("dir " + directory)
var invResult = sess.invokePipeline();
//Show result
System.log( invResult.getHostOutput() );
Chapter 20 Using the PowerShell Plug-In
VMware, Inc. 125