Skip to content

Instantly share code, notes, and snippets.

@alx9r
Last active May 4, 2020 22:09
Show Gist options
  • Save alx9r/688730f7c8c01e610e361cfa44eb877b to your computer and use it in GitHub Desktop.
Save alx9r/688730f7c8c01e610e361cfa44eb877b to your computer and use it in GitHub Desktop.
PowerShell Progress Bar Host Shim (PowerShell/PowerShell#12541)
using System;
using System.Globalization;
using System.Management.Automation.Host;
namespace ProgressBarShim
{
public class Host : PSHost
{
private readonly PSHost PSHost;
private readonly HostUI HostUI;
public Host(PSHost host){
PSHost=host;
HostUI = new HostUI(PSHost.UI);
}
public override CultureInfo CurrentCulture => PSHost.CurrentCulture;
public override CultureInfo CurrentUICulture => PSHost.CurrentUICulture;
public override Guid InstanceId => PSHost.InstanceId;
public override string Name => PSHost.Name;
public override PSHostUserInterface UI => HostUI;
public override Version Version => PSHost.Version;
public override void EnterNestedPrompt()
{
PSHost.EnterNestedPrompt();
}
public override void ExitNestedPrompt()
{
PSHost.ExitNestedPrompt();
}
public override void NotifyBeginApplication()
{
PSHost.NotifyBeginApplication();
}
public override void NotifyEndApplication()
{
PSHost.NotifyEndApplication();
}
public override void SetShouldExit(int exitCode)
{
PSHost.SetShouldExit(exitCode);
}
}
}
Add-Type -Path .\host.cs,.\ui.cs
$hostShim = [ProgressBarShim.Host]::new($Host)
$rs = [runspacefactory]::CreateRunspace($hostShim)
$rs.Open()
$sb = {
1..10 | % {
Write-Progress 'Activity' 'Status' 1 -PercentComplete ($_*10) -SourceId 99
sleep -Milliseconds 100
}
}
$ps = [powershell]::Create($rs).AddScript($sb)
$ps.Invoke()
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Security;
namespace ProgressBarShim
{
internal class HostUI : PSHostUserInterface
{
private readonly PSHostUserInterface PSHostUI;
public HostUI(PSHostUserInterface psHostUI)=>PSHostUI=psHostUI;
public override PSHostRawUserInterface RawUI => PSHostUI.RawUI;
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
{
return PSHostUI.Prompt(caption, message, descriptions);
}
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
{
return PSHostUI.PromptForChoice(caption, message, choices, defaultChoice);
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
return PSHostUI.PromptForCredential(caption, message, userName, targetName, allowedCredentialTypes, options);
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
{
return PSHostUI.PromptForCredential(caption, message, userName, targetName);
}
public override string ReadLine()
{
return PSHostUI.ReadLine();
}
public override SecureString ReadLineAsSecureString()
{
return PSHostUI.ReadLineAsSecureString();
}
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
{
PSHostUI.Write(foregroundColor, backgroundColor, value);
}
public override void Write(string value)
{
PSHostUI.Write(value);
}
public override void WriteDebugLine(string message)
{
PSHostUI.WriteDebugLine(message);
}
public override void WriteErrorLine(string value)
{
PSHostUI.WriteErrorLine(value);
}
public override void WriteLine(string value)
{
PSHostUI.WriteLine(value);
}
public override void WriteProgress(long sourceId, ProgressRecord record)
{
ProgressRecord r = new ProgressRecord(
record.ActivityId,
$"{sourceId} {record.Activity}",
record.StatusDescription
) {
CurrentOperation = record.CurrentOperation,
ParentActivityId = record.ParentActivityId,
PercentComplete = record.PercentComplete,
RecordType = record.RecordType,
SecondsRemaining = record.SecondsRemaining
};
PSHostUI.WriteProgress(sourceId, r);
}
public override void WriteVerboseLine(string message)
{
PSHostUI.WriteVerboseLine(message);
}
public override void WriteWarningLine(string message)
{
PSHostUI.WriteWarningLine(message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment