Reputation: 227
Updated. I created a PowerShell 3.0 cmdlet
using C#/.Net 4.0 in Visual Studio 2010. It works fine. But the cmdlet
takes a while and I want to add a progress bar.
The MSDN documentation is vague on WriteProgressCommand. Here is the link: http://msdn.microsoft.com/en-us/library/microsoft.powershell.commands.writeprogresscommand.completed(v=vs.85).aspx
The code below shows what I want to do. Basically do some processing under ProcessRecord()
. Then every second update the progress bar. Not sure how to display the progress bar. Help?
[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 { }
Upvotes: 4
Views: 3051
Reputation: 54981
I've tested cmdlet developing for 10min now and figured out how the progressbar works. I couldn't even add that WriteProgressCommand class(but then again I'm a programming noob). What I did get to work though was the following:
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 stores the progress-definition and you call a WriteProgress command to update the shell(powershell window) with the newly updated progressdata. "1" in the constructor is just an id.
Upvotes: 6