VMware Cloud Community
gazza_d
Contributor
Contributor

Error when invoking a powershell script from a VRO workflow

I'm trying to get a couple of workflows going in vRO to prove it as a concept. and I'm struggling.

I have a post-deployment powershell script which applies tags in various categories to a new VM and a couple of other things. The script itself works if I run it on the powershell host I've configured, and I can run a very simple powershell script

The script I am using is below

 

var output;
var session;

try {

    session = host_1.openSession();
   var arguments = name + " " + WSUS + " \"" + Avamar + "\" \"" + Classification + "\" " ;
    var script1 =  '& "' + script + '" ' + arguments;

    output = System.getModule("com.vmware.library.powershell").invokeScript(host_1,script1,session.getSessionId()) ;

    var psOutput = getHostOutput();

    System.log("Output:"+psOutput);

}

finally {

    if (session){

        host_1.closeSession(session.getSessionId());

    }

}

 

And the error I get is below.

PowerShellInvocationError: Errors found while executing script System.Management.Automation.CommandNotFoundException: The term 'Instance' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception) at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0) at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args) at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.<InvokeWithPipe>b__0() at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action) at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args) at System.Management.Automation.ScriptBlock.InvokeUsingCmdlet(Cmdlet contextCmdlet, Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args) at Microsoft.PowerShell.Commands.InvokeExpressionCommand.ProcessRecord() at System.Management.Automation.CommandProcessor.ProcessRecord() (Dynamic Script Module name : invokeScript#15)

I'd be grateful for any pointers and help

Gary

0 Kudos
4 Replies
imtrinity94
Enthusiast
Enthusiast

Can you provide other inputs like what's in variables like script and name as well script in getHostOutput() please

Mayank Goyal
vRO Engineer
https://www.linkedin.com/in/mayankgoyal1994/
https://cloudblogger.co.in/
0 Kudos
imtrinity94
Enthusiast
Enthusiast

 

var output;
var session;
try {
    session = host_1.openSession();
    var arguments = "dir";
    var script1 = arguments;
    output = invokeScript(host_1, script1, session.getSessionId());
    var psOutput = output.getHostOutput();
    System.log(psOutput);
    return ("Output:" + psOutput);
} finally {
    if (session) {
        host_1.closeSession(session.getSessionId());
    }
}

function invokeScript(host, script, sessionId) {
    var oSession = host.getSession(sessionId);
    var result = oSession.invokeScript(script);
    if (result.invocationState == 'Failed') {
        throw "PowerShellInvocationError: Errors found while executing script \n" + result.getErrors();
    }
    return result;
}

 

And output is

Directory: C:\Users\svc_acc

Mode LastWriteTime Length Name
---- ------------- ------ ----
d-r--- 7/16/2016 9:23 AM Desktop
d-r--- 10/12/2021 4:26 AM Documents
d-r--- 7/16/2016 9:23 AM Downloads
d-r--- 7/16/2016 9:23 AM Favorites
d-r--- 7/16/2016 9:23 AM Links
d-r--- 7/16/2016 9:23 AM Music
d-r--- 7/16/2016 9:23 AM Pictures
d----- 7/16/2016 9:23 AM Saved Games
d-r--- 7/16/2016 9:23 AM Videos

 

 

 

 


Mayank Goyal
vRO Engineer
https://www.linkedin.com/in/mayankgoyal1994/
https://cloudblogger.co.in/
0 Kudos
gmrv
Contributor
Contributor

Try defining/declaring your parameters (output, session) as input parameters to the workflow.   You may want to consider putting your script in an Action then create a Workflow with an action element.    Be mindful of the parameters and what naming you elect to use.

i.e.,  If you create an Action with an Input of "session", there is no need for "output". The Action return actionResult.   You can define the Return Type (string, VC:HostSystem....etc) within the Action

Using the Action within a workflow:  Add an action element to your workflow.  Specify the Action containing your PowerShell.  You may see Update and Promote prompts.  The Promote will expose the "session" and "actionResult" to the workflow.   Select the variable/input/output inside of the workflow that aligns with your Action parameters.

0 Kudos
gmrv
Contributor
Contributor

I neglected to point out the following:

Due to the handler function when using PowerShell (or Node, Python) your parameter name must include $inputs.

e.g.,  Input parameter for Workflow  declared as string with name myString.   Inside the code you would use

$inputs.myString   

or, in your example

$inputs.session

Of course, you could set any $inputs.<parameter name>  to another variable within the code and then use it.

e.g.,  $newvar = $inputs.session

$newvar could then be used within the script

0 Kudos