已更新。我在Visual Studio 2010中使用C#/。Net 4.0创建了PowerShell 3.0 cmdlet
。它运行正常。但cmdlet
需要一段时间,我想添加一个进度条。
MSDN文档在WriteProgressCommand上含糊不清。这是链接:http://msdn.microsoft.com/en-us/library/microsoft.powershell.commands.writeprogresscommand.completed(v=vs.85).aspx
下面的代码显示了我想要做的事情。基本上在ProcessRecord()
下进行一些处理。然后每秒更新进度条。不确定如何显示进度条。帮助
[System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get, "StatusBar")]
public class GetStatusBarCommand : System.Management.Automation.PSCmdlet
{
/// <summary>
/// Provides a record-by-record processing functionality for the cmdlet.
/// </summary>
protected override void ProcessRecord()
{
WriteProgressCommand progress = new WriteProgressCommand();
for (int i = 0; i < 60; i++)
{
System.Threading.Thread.Sleep(1000);
progress.PercentComplete = i;
}
progress.Completed = true;
this.WriteObject("Done.");
return;
}
}
// Commented out thanks to Graimer's answer
// [System.Management.Automation.CmdletAttribute("Write", "Progress")]
// public sealed class WriteProgressCommand : System.Management.Automation.PSCmdlet { }
答案 0 :(得分:5)
我已经测试了cmdlet现在开发了10分钟并找出了进度条的工作原理。我甚至无法添加WriteProgressCommand类(但后来我又是编程菜鸟)。我做的工作虽然如下:
protected override void ProcessRecord()
{
ProgressRecord myprogress = new ProgressRecord(1, "Testing", "Progress:");
for (int i = 0; i < 100; i++)
{
myprogress.PercentComplete = i;
Thread.Sleep(100);
WriteProgress(myprogress);
}
WriteObject("Done.");
}
ProgressRecord存储进度定义,并调用WriteProgress命令以使用新更新的progressdata更新shell(powershell窗口)。构造函数中的“1”只是一个id。