在c#中订阅powershell progress无法工作
我正在编写一个C#GUI程序,该程序使用System.Management.Automation在运行时创建的实例上执行powershell命令,我需要它是一个持久的powershell实例,以便在运行时发送多个命令并收集输出。这是预期的,但我在订阅进度事件时遇到了一些问题。它只在我输入错误的命令并写入进度已完成-1%时触发 当我输入正确的命令时,它根本不会启动。 错误和输出事件工作正常,所以我不确定出了什么问题 我发现另外两个人也有类似的问题,但他们的解决方案对我不起作用,我在下面把他们联系起来。这是我到目前为止未成功使用的代码在c#中订阅powershell progress无法工作,c#,powershell,automation,C#,Powershell,Automation,我正在编写一个C#GUI程序,该程序使用System.Management.Automation在运行时创建的实例上执行powershell命令,我需要它是一个持久的powershell实例,以便在运行时发送多个命令并收集输出。这是预期的,但我在订阅进度事件时遇到了一些问题。它只在我输入错误的命令并写入进度已完成-1%时触发 当我输入正确的命令时,它根本不会启动。 错误和输出事件工作正常,所以我不确定出了什么问题 我发现另外两个人也有类似的问题,但他们的解决方案对我不起作用,我在下面把他们联系起
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();
}
}
公共类代码处理程序{
公共静态PowerShell psInstance;
公共void init(){
//创建powershell实例
psInstance=PowerShell.Create();
}
公共异步任务ExecutePowershellCommand(字符串脚本){
psInstance.AddScript(脚本);
psInstance.AddCommand(“输出字符串”);
PSDataCollectionoutputCollection=新PSDataCollection();
outputCollection.DataAdded+=outputCollection\u DataAdded;
//我不能工作的部分
psInstance.Streams.Progress.DataAdded+=(发送方,事件参数)=>{
PSDataCollectionprogressRecords=(PSDataCollection)发送方;
Console.WriteLine(“!进度为{0}完成百分比”,progressRecords[eventargs.Index].PercentComplete);
};
psInstance.Streams.Error.DataAdded+=Error\u DataAdded;
IAsyncResult result=psInstance.BeginInvoke(null,outputCollection);
而(!result.IsCompleted){
WriteLine(“等待管道完成…”);
等待任务。延迟(100);
}
PSInvocationState=psInstance.InvocationStateInfo.state;
WriteLine(“执行已停止。管道状态:“+状态”);
如果(状态!=PSInvocationState.Completed)
返回字符串。空;
StringBuilder StringBuilder=新的StringBuilder();
foreach(outputCollection中的PSObject outputItem){
AppendLine(outputItem.BaseObject.ToString());
Console.WriteLine(outputItem.BaseObject.ToString());
}
返回stringBuilder.ToString();
}
}
参考资料:
添加
psInstance.EndInvoke(结果)代码>如下所示:
...
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);
...
剧本在哪里?什么?问题中有一条评论说这是我无法解决的问题“只有当我输入错误的命令并写入进度为-1%时才会触发。”-你有“错误的命令”和“正确的命令”的例子吗?如果您试图调用一个不发送到进度
流的命令,那么您描述的行为是预期的
# 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
}