Подтвердить что ты не робот

Как читать код выхода PowerShell через С#

Я использую System.Management.Automation.Runspaces для выполнения сценариев PowerShell. есть ли опция, я могу прочитать код выхода данного script?

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;
        }
    }
}
4b9b3361

Ответ 1

Команды PowerShell имеют более богатый механизм ошибок, чем целые коды выхода. Существует поток ошибок, в котором появляются ошибки без завершения. Завершение ошибок приводит к появлению исключений, поэтому вам необходимо их обработать. Следующий код показывает, как использовать два механизма:

using System;
using System.Collections.ObjectModel;
using System.Management.Automation;

namespace PowerShellRunspaceErrors
{
    class Program
    {
        private static PowerShell s_ps;

        static void Main(string[] args)
        {
            s_ps = PowerShell.Create();
            ExecuteScript(@"Get-ChildItem c:\xyzzy");
            ExecuteScript("throw 'Oops, I did it again.'");
        }

        static void ExecuteScript(string script)
        {
            try
            {
                s_ps.AddScript(script);
                Collection<PSObject> results = s_ps.Invoke();
                Console.WriteLine("Output:");
                foreach (var psObject in results)
                {
                    Console.WriteLine(psObject);
                }
                Console.WriteLine("Non-terminating errors:");
                foreach (ErrorRecord err in s_ps.Streams.Error)
                {
                    Console.WriteLine(err.ToString());
                }
            }
            catch (RuntimeException ex)
            {
                Console.WriteLine("Terminating error:");
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Если вы запустите эту программу, она выведет:

Output:
Non-terminating errors:
Cannot find path 'C:\xyzzy' because it does not exist.
Terminating error:
Oops, I did it again.
Press any key to continue . . .

Ответ 2

Процессы возвращают коды выхода, а не PowerShell Runspaces.

Насколько я знаю, лучшим способом захватить состояние Runspace было бы подписаться на событие Runspace.StateChanged, которое даст вам RunspaceStateInfo для решения. На RunspaceStateInfo есть два полезных свойства: Reason и State.

http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.runspacestateinfo_members(v=vs.85).aspx

Ответ 3

Это так же просто, как задать значение только что созданной области:

s_ps.AddScript("$LASTEXITCODE");
results = s_ps.Invoke();
int.TryParse(results[0].ToString(),out valorSortida);