简体   繁体   English

订阅 powershell 在 c# 中的进度不起作用

[英]Subscribing to powershell progress in c# not working

I'm writing a C# GUI program that uses System.Management.Automation to execute powershell commands on an instance created at runtime, I need it to be a persistent powershell instance so I can send multiple commands and collect the output at runtime. I'm writing a C# GUI program that uses System.Management.Automation to execute powershell commands on an instance created at runtime, I need it to be a persistent powershell instance so I can send multiple commands and collect the output at runtime. This works as intended but i'm having some issues subscribing to the progress event.这按预期工作,但我在订阅进度事件时遇到了一些问题。 It only fires when I input faulty commands and writes that the progress is -1 percent complete.它仅在我输入错误命令并写入进度已完成 -1% 时触发。

When I input correct commands it doesn't fire at all.当我输入正确的命令时,它根本不会触发。 The error and output events work properly so i'm not sure what's going wrong.错误和 output 事件正常工作,所以我不确定出了什么问题。

I found two others having similar problems but their solutions didn't work for me, I linked them down below.我发现另外两个有类似的问题,但他们的解决方案对我不起作用,我将它们链接在下面。 Here's the code i'm using unsucessfully so far.到目前为止,这是我使用失败的代码。

 public class CodeHandler {
   public static PowerShell psInstance;

   public void init() {
     //Create powershell instance
     psInstance = PowerShell.Create();
   }

   public async Task < string > ExecutePowershellCommand(string script) {
     psInstance.AddScript(script);
     psInstance.AddCommand("Out-String");

     PSDataCollection < PSObject > outputCollection = new PSDataCollection < PSObject > ();
     outputCollection.DataAdded += outputCollection_DataAdded;

     //The Part I Can't Get To Work
     psInstance.Streams.Progress.DataAdded += (sender, eventargs) => {
       PSDataCollection < ProgressRecord > progressRecords = (PSDataCollection < ProgressRecord > ) sender;
       Console.WriteLine("!Progress is {0} percent complete", progressRecords[eventargs.Index].PercentComplete);
     };

     psInstance.Streams.Error.DataAdded += Error_DataAdded;

     IAsyncResult result = psInstance.BeginInvoke < PSObject, PSObject > (null, outputCollection);

     while (!result.IsCompleted) {
       Console.WriteLine("Waiting for pipeline to finish...");
       await Task.Delay(100);
     }

     PSInvocationState state = psInstance.InvocationStateInfo.State;
     Console.WriteLine("Execution has stopped. The pipeline state: " + state);
     if (state != PSInvocationState.Completed)
       return string.Empty;

     StringBuilder stringBuilder = new StringBuilder();
     foreach(PSObject outputItem in outputCollection) {
       stringBuilder.AppendLine(outputItem.BaseObject.ToString());
       Console.WriteLine(outputItem.BaseObject.ToString());
     }

     return stringBuilder.ToString();
   }

 }

References:参考:

Getting Write-Progress -PercentComplete status from within C# 从 C# 中获取 Write-Progress -PercentComplete 状态

Reading Powershell Progress Bar Output in C# 在 C# 中读取 Powershell 进度条 Output

Add psInstance.EndInvoke(result);添加psInstance.EndInvoke(result); as shown below:如下所示:

                       ...
 IAsyncResult result = psInstance.BeginInvoke < PSObject, PSObject > (null, outputCollection);

 while (!result.IsCompleted) {
   Console.WriteLine("Waiting for pipeline to finish...");
   await Task.Delay(100);
 }

 psInstance.EndInvoke(result);

                       ...

Your script needs to use Write-Progress您的脚本需要使用Write-Progress

Example script示例脚本

# Use Get-Command to get list of commands and store them in the $cmds variable.
$cmds = Get-Command

# Pipe the events to the ForEach-Object cmdlet.
$cmds  | ForEach-Object -Begin {
    # In the Begin block, use Clear-Host to clear the screen.
    Clear-Host

    # Set the $i counter variable to zero.
    $i = 0

    # Set the $out variable to a empty string.
    $out = ""
} -Process {
    # get name
     # Append to the out variable.
     # $out=$out + $_

     write-output ($_.Name + " " + $_.Version) 

    # Increment the $i counter variable which is used to create the progress bar.
    $i = $i+1

    # Use Write-Progress to output a progress bar.
    # The Activity and Status parameters create the first and second lines of the progress bar heading, respectively.
    Write-Progress -Activity "Searching Commands" -Status "Progress:" -PercentComplete ($i/$cmds.count*100)
} -End {
    # Display using the out variable.
    # $out
}

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

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