Skip to content

JustinGrote/ScriptFeedbackProvider

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PowerShell Script Based Feedback Providers

This module allows you to write feedback providers in PowerShell rather than C# and register them.

By default, feedback providers are registered for errors only. To register for another type, use the -Trigger parameter.

Also errors and potential issues are silently suppressed by default. Use the -ShowDebugInfo parameter of Register-ScriptFeedbackProvider to get useful information for troubleshooting feedback providers.

Usage

A [FeedbackContext] is provided as an argument and you must return a single [FeedbackItem] object.

You can access the feedback context either via $args[0] or as param($context)

You can return a [FeedbackItem] result multiple ways:

  1. Return a FeedbackItem object. This is the safest method. [FeedbackItem]::new('header',@('action1','action2'))
  2. Return a single string, that will fill in for the header.
  3. Return multiple strings, the first will be the header and the others will be the defined actions
  4. Return a hashtable with the same properties as the FeedbackItem class.

Examples

Echo the last command

Register-ScriptFeedbackProvider -Name EchoCommand -Trigger All -ScriptBlock {
  param($context)
  [FeedbackItem]::new("Command was", $context.CommandLine)
}

Alt text

Report on a recommended action

Register-ScriptFeedbackProvider -Name 'Error Recommended Action' {
  param($context)
  if ($context.LastError.ErrorDetails.RecommendedAction) {
    [FeedbackItem]::new(
      'The last error has a recommended action:',
      $context.LastError.ErrorDetails.RecommendedAction
    )
  }
}

Alt text

Using simplified string syntax

Register-ScriptFeedbackProvider -Name 'Error Recommended Action' {
  param($context)
  if ($context.LastError.ErrorDetails.RecommendedAction) {
    'The last error has a recommended action:'
    $context.LastError.ErrorDetails.RecommendedAction
  }
}

Alt text

Authoring Intellisense

To get intellisense for $context, add the namespace at the top of your file.

using namespace System.Management.Automation.Subsystem.Feedback
{
  param([FeedbackContext]$context)
  $context.<tab>
}

But do not include the namespace in your final command

About

Write PowerShell Feedback Providers using PowerShell!

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published