简体   繁体   中英

How to understand the JavaScript I am using to call Powershell code?

I am trying to understand this JavaScript code. I don't know anything about JavaScript. As I understand this script is calling PowerShell script. But I am unable to understand what is that indexof code doing.

I am using this JavaScript on VMWare VRealize orchestrator.

var output;
var session;

try {

    session = host.openSession();
    var script =  '& "' + Powershellscript + '" ' + scriptparameter;
    output = session.invokeScript(script);
    var psOutput = output.getHostOutput();
    System.log(psOutput);

    if(psOutput.indexOf("Error In Operation") == -1)
    {
        isPsExecuted = true;
    }
    else
    {
        isPsExecuted = false;
    }

} 
finally 
{
    if (session)
    {
        host.closeSession(session.getSessionId());
    }
}

indexOf checks if the text 'Error In Operation' is present anywhere in the output of the script. It uses this to detect whether an error happened. indexOf returns the index of a given string in another string

psOutput.indexOf("Error In Operation") == -1 means that Error In Operation was not present anywhere in psOutput (-1 is returned when the string is not found)

psOutput.indexOf("Error In Operation") == -1 means psOutput doesn't contain the text "Error In Operation" (case sensitive)

Generally, indexOf returns the place a text/element is found in a string/array. It returns -1 if it was not found. More info at MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM