Skip to content

Instantly share code, notes, and snippets.

@vexx32
Last active June 15, 2019 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vexx32/f130a15e2b695e8a9940b377084f6ed9 to your computer and use it in GitHub Desktop.
Save vexx32/f130a15e2b695e8a9940b377084f6ed9 to your computer and use it in GitHub Desktop.
namespace PSFSharp
open System.Management.Automation
open System.Management.Automation.Internal
[<Cmdlet(VerbsLifecycle.Invoke, "ConditionalAction",
ConfirmImpact = ConfirmImpact.Medium, SupportsShouldProcess = true)>]
[<Alias("?!")>]
type InvokeConditionalActionCommand() =
inherit PSCmdlet()
let mutable inputCount = 0
let mutable firstObject = AutomationNull.Value
[<Parameter(Mandatory = true, ValueFromPipeline = true)>]
[<Alias("InputObject")>]
member val If : PSObject = null
with get, set
[<Parameter(Mandatory = true, Position = 0)>]
member val Then : ScriptBlock = null
with get, set
[<Parameter(Position = 1)>]
member val Else : ScriptBlock = null
with get, set
override self.ProcessRecord() =
if firstObject <> AutomationNull.Value then
firstObject <- self.If
inputCount <- inputCount + 1
override self.EndProcessing() =
let isTrue = inputCount > 1 || (inputCount = 1 && LanguagePrimitives.ConvertTo<bool>(firstObject))
if isTrue then
self.WriteObject(self.Then.Invoke(), true)
else
self.WriteObject(self.Else.Invoke(), true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment