To use this function add it to the class library file named OrchestratorService.ts
GitHub Repository: https://github.com/SimonSparksUK/Orchestrator
Filename: OrchestratorService.ts
Description: Orchestrator Function to Invoke a PowerShell Script
Function: invokeScript
public invokeScript(objPowerShellHost: PowerShellHost, strScriptContent: string, strSessionId?: string, strUsername?: string, strPassword?: string): PowerShellRemotePSObject {
let objPowerShellSession: PowerShellSession;
if (strSessionId) {
objPowerShellSession = objPowerShellHost.getSession(strSessionId);
} else if (strUsername && strPassword) {
objPowerShellSession = objPowerShellHost.openSessionAs(strUsername, strPassword);
} else {
objPowerShellSession = objPowerShellHost.openSession();
}
let objPowerShellInvocationResult: PowerShellInvocationResult = objPowerShellSession.invokeScript(strScriptContent);
this.objLogger.info(`Invocation State: ${objPowerShellInvocationResult.getInvocationState()}.`);
this.objLogger.info(`Host Output: \n${objPowerShellInvocationResult.getHostOutput()}.`);
if (objPowerShellInvocationResult.getInvocationState() === "Completed") {
let objPowerShellRemotePSObject: PowerShellRemotePSObject = objPowerShellInvocationResult.getResults();
let strJson: string = objPowerShellRemotePSObject.getAsJson();
let objJson: any = JSON.parse(strJson);
this.objLogger.info(JSON.stringify(objJson, null, 2));
return objPowerShellRemotePSObject;
} else if (objPowerShellInvocationResult.getInvocationState() === "Failed") {
let arrError: string[] = objPowerShellInvocationResult.getErrors();
this.objLogger.info("PowerShellInvocationError: Errors found while executing script \n" + arrError);
if (arrError.length) {
this.objLogger.info(`Script Errors: ${arrError.join('\n')}.`);
}
return null;
}
}Discover more from Cloud Build Tools
Subscribe to get the latest posts sent to your email.
