繁体   English   中英

从C#代码调用PS脚本(Visual Studio 2017)

[英]Invoking PS script from C# code (Visual studio 2017)

我正在尝试从C#代码调用PS脚本。

**PS code. -- Try.ps1**
Write-Host "HelloHost"
Write-Debug "HelloDebug"
Write-Output "HelloOutput"
echo "tofile" > $PSScriptRoot/a.txt

**C# code**

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Management.Automation.Runspaces;

namespace TryitOut
{
    class Program
    {
        static void Main(string[] args)
        {
            using (PowerShell pshell = PowerShell.Create())
            {
                string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                pshell.AddScript(path + "\\Try.ps1");
                IAsyncResult result = pshell.BeginInvoke();
                while (result.IsCompleted == false)
                {
                    Console.WriteLine("Waiting for PS script to finish...");
                    Thread.Sleep(1000);
                }
                Console.WriteLine("Finished!");
                Console.ReadKey();
            }
        }
    }
}

当我独立运行“ Try.ps1”时,它运行正常并创建a.txt,并且按预期显示控制台输出。 但是,它不会通过此C#代码被调用/执行。 问题1 您能帮我个忙吗? Q 2.通过C#代码调用Try.ps1时,如何查看PS控制台输出

感谢您的时间 :)。

处理PowerShell对象的输出有两种方法。 一个是Invoke调用的直接返回值,另一个是通过各种 在这两种情况下,您都有责任处理数据。 当您在标准PowerShell窗口中运行脚本时,控制台不会自动输出它们。 例如,要从Write-Object获取结果,可以在while循环之后将以下内容添加到C#代码中:

foreach(PSObject pso in pshell.EndInvoke(result))
{
   Console.WriteLine(pso);
}

这对于示例中的简单字符串而言效果很好,但是对于具有多个属性的复杂对象,则需要按名称提取单个属性。 例如:

string propValue = pso.Members["PropertyName"].Value.ToString();

看看我以前的答案,看看将复杂对象转换为自己的类型的一种方法: 使用PowerShell Inside C#处理CimObjects

像这样处理调试,信息,详细信息等:

foreach(DebugRecord dbr in pshell.Streams.Debug)
{
   Console.WriteLine(dbr);
}

您还需要将其添加到脚本的开头:

$DebugPreference = 'continue'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM