14

System.Management.Automation.RunspacesPowerShell スクリプトの実行に使用しています。特定のスクリプトの終了コードを読み取ることができるオプションはありますか?

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

3 に答える 3

6

PowerShell コマンドには、整数の終了コードよりも豊富なエラー メカニズムがあります。終了しないエラーが表示されるエラー ストリームがあります。エラーを終了すると例外がスローされるため、それらを処理する必要があります。次のコードは、2 つのメカニズムの使用方法を示しています。

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 . . .
于 2012-10-18T04:45:15.170 に答える
0

プロセスは PowerShell ではなく、終了コードを返しますRunspaces

私の知る限り、a の状態をキャプチャする最良の方法は、イベントRunspaceにサブスクライブすることです。これにより、対処することができます。には、 と の 2 つの便利なプロパティがあります。Runspace.StateChangedRunspaceStateInfoRunspaceStateInfoReasonState

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

于 2012-10-15T13:38:14.163 に答える
0

作成した実行空間に値を尋ねるのと同じくらい簡単です。

s_ps.AddScript("$LASTEXITCODE");
results = s_ps.Invoke();
int.TryParse(results[0].ToString(),out valorSortida);
于 2016-10-14T15:19:34.430 に答える