Sto usando System.Management.Automation.Runspaces
per eseguire gli script di PowerShell. c'è un'opzione in grado di leggere il codice di uscita di un determinato script?Come leggere il codice di uscita di PowerShell tramite C#
using System.IO;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.Management.Automation;
namespace PowerShell
{
public class PowerShellExecuter
{
public Collection<PSObject> RunPsScript(string psScriptFile)
{
string psScript;
if (File.Exists(psScriptFile))
{
psScript = File.ReadAllText(psScriptFile);
}
else
{
throw new FileNotFoundException("Wrong path for the script file");
}
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runSpace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeLine = runSpace.CreatePipeline();
pipeLine.Commands.AddScript(psScript);
pipeLine.Commands.Add("Out-String");
Collection<PSObject> returnObjects = pipeLine.Invoke();
runSpace.Close();
return returnObjects;
}
}
}