VMware Cloud Community
platforminc
Enthusiast
Enthusiast

Getting output from a PowerShell Script on VRO task

Hi All.

 

First post, what a wonderful job everyone gets up to here. Learning from each other,

 

I have hit a problem with some VRO automation that i am doing.

 

I can get my task to execute a powershell script, I believe i am using the official plugin. My powershell script creates a file on a remote host, however I need to get the path that the script creates. At the moment, within my script I just do a stdout of the filename, so now I would like VRO to pick up the output filename.

 

The script task which builds the PS arguments is as follows.

powershellArguments = "-Servername " + hostname + " -config_dir " + confniguration_directory + " -instance_no " + instance_no + " -type " + environment_type + " -admin_login " + admin_login + " -collation " + db_collation + " -sql_account " + sql_account + " -install_dir " + install_directory + " -sql_components " + components+ " -sql_dir " + data_directory+ " -log_dir " + log_directory+ " -backup_dir " + y+ " -tempdb_dir " + x; 
System.log ("Using PowerShell Aruments '" + powershellArguments + "'.");

Now, if I want to receive the output of the file, how do I do this please?

0 Kudos
7 Replies
daphnissov
Immortal
Immortal

You may really want to look at the Guest Script Manager community package out there as it should get this for you already. Keep in mind this is unsupported, however.

0 Kudos
platforminc
Enthusiast
Enthusiast

Please see screenshot below.
0 Kudos
platforminc
Enthusiast
Enthusiast

Attached is a screenshot of the script task.
0 Kudos
platforminc
Enthusiast
Enthusiast

Any ideas anyone ?
0 Kudos
iiliev
VMware Employee
VMware Employee

'Invoke an external script' workflow, which you seem to be using, has an output parameter named output of type PowerShellRemotePSObject. If you check the documentation of this object in vRO API explorer, you'll find its method getRootObject(), which should return the result of PowerShell script invocation converted to the corresponding vRO type (could be simple type, array, Properties, or PowerShellPSObject).

0 Kudos
platforminc
Enthusiast
Enthusiast

Thanks for the explanation, however this is all new to me and I would need your help.

I entered changed the script task to enter the below and it was just failing.

 

var outputname = output.getRootObject();
System.log ("Is the output '" + outputname + "'.");

0 Kudos
platforminc
Enthusiast
Enthusiast

I found an example which works, however the way they have used the powershell script is differ

invResult in the example below is a variable holding the execution of the code. In my case, the Ps script file is an attribute of the workflow and the arguments are passed as parameters to the PS script file. So I dont know how to capture the result in my case.

 


var sess;
try {
sess = host.openSession()
var script = ' $objects = get-process | Select-Object -first 3 \r\n' +
' $objects \r\n';
// ' $objects | format-list -property name, basepriority, priorityclass \r\n';
//Set input parameters
var invResult = sess.invokeScript(script)
//Show result
System.log( "------- HOST OUTPUT ---------------------------" );
System.log( invResult.getHostOutput() );
System.log( "-------- HOST OUTPUT END --------------------------" );
//Check for errors
if (invResult.invocationState  == 'Failed'){
System.error(invResult.getErrors());
} else {
// Get PowerShellRemotePSObject
var psObject = invResult.getResults();
//Show row XML 
        var psList = psObject.getRootObject();
// No need to parse the result yoursekf. This is done by PoeerShell plugin itself
// Using PowerShellPSObject.getProperty methods you can access properties of returned object 
if ( psList instanceof Array ){
  for (idx in psList){
var item = psList[idx];
System.log( item.getProperty('Name') );//extract value from result
System.log( item.getProperty('Path') );//extract value from result
// you can also get the real type of PowerShell object as retturned by PowerShell engine
    // System.log( item.getTypes() );
      }
}
// Uncoment below lines to see row JSON/XML
//System.log( "------- JSON ---------------------------" );
//System.log( invResult.getResults().getAsJson() );
//System.log( "-------- JSON END --------------------------" );
//System.log( "------- ROW XML ---------------------------" );
//System.log( invResult.getResults().getXml() );
//System.log( "-------- ROW XML END --------------------------" );
}
} catch ( ex ) {
System.log (ex);
} finally {
if (sess) {
host.closeSession( sess.getSessionId());
}
}
 

0 Kudos