Bil*_*ble 4 c# powershell .net-4.0
更新.我cmdlet在Visual Studio 2010中使用C#/ .Net 4.0 创建了一个PowerShell 3.0 .它工作正常.但cmdlet需要一段时间,我想添加一个进度条.
关于WriteProgressCommand的MSDN文档含糊不清.这是链接: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 { }
Run Code Online (Sandbox Code Playgroud)
我已经测试了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.");
}
Run Code Online (Sandbox Code Playgroud)
ProgressRecord存储进度定义,并调用WriteProgress命令以使用新更新的progressdata更新shell(powershell窗口).构造函数中的"1"只是一个id.
| 归档时间: |
|
| 查看次数: |
2388 次 |
| 最近记录: |