4

更新。cmdlet我在 Visual Studio 2010 中使用 C#/.Net 4.0创建了一个 PowerShell 3.0 。它工作正常。但这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 { }
4

1 回答 1

6

我已经测试了开发 10 分钟的 cmdlet,并弄清楚了进度条是如何工作的。我什至不能添加那个 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 命令以使用新更新的进度数据更新 shell(powershell 窗口)。构造函数中的“1”只是一个 id。

于 2012-12-14T19:25:48.757 回答