Last active
June 15, 2019 03:13
-
-
Save vexx32/f130a15e2b695e8a9940b377084f6ed9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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