I want to develop a PowerShell application that is able to invoke certain commands depending on the previous command. Also, the next command must be able to take the result from the previous one and do some stuff with it. Therefore, I use the PowerShell.SDK from Microsoft.
Currently, I have the following approach with which the commands are executed one after another and waiting for each other. But do not care about the results.
public Task Execute(IJobExecutionContext context)
{
var details = (IList<(string modulepath,string arguments)>)context.JobDetail.JobDataMap["Commands"];
PowerShell ps = PowerShell.Create();
foreach (var detail in details)
{
ps.Commands.AddCommand("Start-Process").AddParameter("FilePath", detail.modulepath).AddParameter("ArgumentList", detail.arguments).AddParameter("Wait");
}
ps.Invoke();
return Task.FromResult(true);
}
Is there any easy why to get the Errors or ExitCode from the single commands? Or am I following the wrong way? Thanks for any suggest. What would be the correct and best way to do that?