public invoke()

in src/app/runtime/wac/services/powershell-service.ts [100:148]


  public invoke(pwCmdString: string, psParameters: any, requireCredSSP: boolean = false): Observable<any[]>{
    psParameters.sessionId = this.sessionId;
    var flags: string[] = IsProduction ? [] : [ 'verbose' ];
    var compiled: string = PowerShell.createScript(pwCmdString, psParameters, flags);
    var scriptName: string = pwCmdString.split("\n")[0]
    var session = requireCredSSP ? this.createCredSSPSession() : this.session;
    return session.pipe(
      mergeMap(ps => ps.powerShell.run(compiled)),
      instrument(this.logger, `${scriptName} => ${JSON.stringify(psParameters)}`),
      logError(this.logger, LogLevel.WARN, `Script ${scriptName} failed`),
      map((response: PowerShellResult) => {
        if (!response) {
          throw `Powershell command ${scriptName} returns no response`;
        }
        if (response.warning) {
          this.logger.log(LogLevel.WARN, `Powershell command ${scriptName} returns the following warnings`)
          for (const line of response.warning) {
            this.logger.log(LogLevel.WARN, line);
          }
        }
        if (response.errors) {
          this.logger.log(LogLevel.ERROR, `Powershell command ${scriptName} returns the following errors`)
          for (const line of response.errors) {
            this.logger.log(LogLevel.ERROR, line);
          }
        }
        if (!response.results) {
          throw `Powershell command ${scriptName} returns null response`;
        }
        if (response.results.length <= 0) {
          throw `Powershell command ${scriptName} returns empty response`;
        }
        return response.results;
      }),
      catchError((e, _) => {
        let rethrow = e;
        // WAC wrap the powershell error (or exception) message around this AjaxError object. We would unwrap it for easier readability
        if (e.name == "AjaxError" && e.status == 400) {
          if (e.response.error && !e.response.exception) {
            rethrow = e.response.error;
          } 
          if (!e.response.error && e.response.exception) {
            rethrow = e.response.exception;
          }
        }
        throw rethrow;
      }),
    );
  }