如何通过C#阅读PowerShell退出代码 [英] How to read PowerShell exit code via c#

查看:53
本文介绍了如何通过C#阅读PowerShell退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 System.Management.Automation.Runspaces 执行PowerShell脚本.我可以读取给定脚本的退出代码吗?

I am using System.Management.Automation.Runspaces to execute PowerShell scripts. is there an option i can read the exit code of a given 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;
        }
    }
}

推荐答案

PowerShell命令具有比整数退出代码更丰富的错误机制.存在一个错误流,其中出现了非终止错误.终止错误会导致引发异常,因此您需要处理这些异常.以下代码显示了如何使用这两种机制:

PowerShell commands have a richer error mechanism than integer exit codes. There is an error stream that non-terminating errors appear on. Terminating errors result in thrown exceptions so you need to handle those. The following code shows how to use the two mechanisms:

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

如果运行此程序,它将输出:

If you run this program it outputs:

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 . . .

这篇关于如何通过C#阅读PowerShell退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆