通过代码调用时获取Powershell命令的输出

Muh*_*der 9 c# powershell azure

我编写了一段代码(在C#中)来执行Powershell脚本(特别是Azure PowerShell)System.Management.Automation.powershell脚本基本上在Azure上的容器中上传vhd,当通过azure Powershell手动输入命令时,该容器显示上载进度和经过的时间等.通过代码一切正常但我想获得命令的结果/输出(即上传进度,时间已过去),在命令执行期间(即pipeline.invoke();)这里是代码:

 RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
 Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
 runspace.Open();
 RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
 Pipeline pipeline = runspace.CreatePipeline();

 Command myCommand = new Command(scriptPath);
 foreach (var argument in arguments)
 {
     myCommand.Parameters.Add(new CommandParameter(argument.Key, argument.Value));
 }
 pipeline.Commands.Add(myCommand);

 var results = pipeline.Invoke(); // i want to get results here (i.e. during command execution) 
 foreach (var psObject in results)
 {
     System.Diagnostics.Debug.Write(psObject.BaseObject.ToString());
 }
Run Code Online (Sandbox Code Playgroud)

请指导是否可以从Powershell检索实时输出.

Mat*_*sen 17

除非您的目标是PowerShell 1.0,否则无需手动设置您的运行空间和管道,而是创建PowerShell该类的实例:

PowerShell psinstance = PowerShell.Create();
psinstance.AddScript(scriptPath);
var results = psinstance.Invoke();
Run Code Online (Sandbox Code Playgroud)

方式更简单.


现在,PowerShell类公开的各种非标准输出流(详细,调试,错误等) -包括进展流-通过Streams属性,所以你可以订阅它,就像这样:

psinstance.Streams.Progress.DataAdded += myProgressEventHandler;
Run Code Online (Sandbox Code Playgroud)

然后在你的事件处理程序中:

static void myProgressEventHandler(object sender, DataAddedEventArgs e)
{
    ProgressRecord newRecord = ((PSDataCollection<ProgressRecord>)sender)[e.Index];
    if (newRecord.PercentComplete != -1)
    {
        Console.Clear();
        Console.WriteLine("Progress updated: {0}", newRecord.PercentComplete);
    }
}
Run Code Online (Sandbox Code Playgroud)

作为示例,这里是上面显示的事件处理程序,同时运行一个示例脚本,该脚本将进度信息(下面发布的示例脚本)写入一个简单的控制台应用程序中:

readProgress

测试Progress.ps1

function Test-Progress
{
    param()

    Write-Progress -Activity 'Testing progress' -Status 'Starting' -PercentComplete 0
    Start-Sleep -Milliseconds 600
    1..10 |ForEach-Object{
        Write-Progress -Activity "Testing progress" -Status 'Progressing' -PercentComplete $(5 + 6.87 * $_)
        Start-Sleep -Milliseconds 400
    }
    Write-Progress -Activity 'Testing progress' -Status 'Ending' -PercentComplete 99
    Start-Sleep -Seconds 2
    Write-Progress -Activity 'Testing progress' -Status 'Done' -Completed
}

Test-Progress
Run Code Online (Sandbox Code Playgroud)

  • 嗨,我正在使用 PowerShell 类,但从未为我触发 DataAdded 事件。我的问题是为什么?我的代码如下所示: using (PowerShell powerShell = PowerShell.Create()) { powerShell.AddScript(scriptText); powerShell.Streams.Progress.DataAdded += ProgressDataAdded; Collection&lt;PSObject&gt; PSOutput = powerShell.Invoke(); } (2认同)

小智 6

可能有点晚了,但仍然如此。

@Mathias R. Jessen 答案效果很好,但对于输出普通字符串的情况(没有 Write-Progress 等):

首先初始化集合

PSDataCollection<PSObject> output = new PSDataCollection<PSObject>();
output.DataAdded += Output_DataAdded;
Run Code Online (Sandbox Code Playgroud)

履行

var res = psinstance.BeginInvoke<PSObject, PSObject>(null, output);
res.AsyncWaitHandle.WaitOne();
Run Code Online (Sandbox Code Playgroud)

实时获取结果

private static void Output_DataAdded(object sender, DataAddedEventArgs e)
{
    PSObject newRecord = ((PSDataCollection<PSObject>)sender)[e.Index];
    Console.WriteLine(newRecord);
}
Run Code Online (Sandbox Code Playgroud)

Get-Process在PS 命令上测试。