Invoke-Ternary - something bizarre going on

Topics: User Forum
Feb 16, 2011 at 12:44 PM
Edited Feb 16, 2011 at 12:46 PM

I've filed an issue about this, but on further investigation something very odd is going on.

>get-content function:invoke-ternary

    param([scriptblock]$Condition  = $(throw "Parameter '-condition' (position 1) is required"),
          [scriptblock]$TrueBlock  = $(throw "Parameter '-trueBlock' (position 2) is required"),
          [scriptblock]$FalseBlock = $(throw "Parameter '-falseBlock' (position 3) is required"))
if (&$Condition) {
        &$TrueBlock
    }
    else {
        &$FalseBlock
    }

>dir function:invoke-ternary

CommandType     Name                                                Definition
-----------     ----                                                ----------
Filter          Invoke-Ternary                                      ...

Ok, that's invoke-ternary from PSCX. However,

PS>get-help invoke-ternary -examples

NAME
    Invoke-Ternary

SYNOPSIS
    Similar to the C# ? : operator e.g. name = (value != null) ? String.Empty : value

    -------------------------- EXAMPLE 1 --------------------------

    C:\PS>1..10 | ?: {$_ -gt 5} {"Greater than 5";$_} {"Less than or equal to 5";$_}


    Each input number is evaluated to see if it is > 5.  If it is then "Greater than 5" is
    displayed otherwise "Less than or equal to 5" is displayed.


PS>1..10 | invoke-ternary {$_ -gt 5} {"Greater than 5";$_} {"Less than or equal to 5";$_}
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5

So... it looks like the example in the documentation doesn't work. Even though, looking at the code, it seems fine to me.

Worse still:

PS>filter foo ([scriptblock]$a,[scriptblock]$b,[scriptblock]$c) { if (&$a) {&$b} else {&$c}  }
PS>1..10 | foo {$_ -gt 5} {"Greater than 5";$_} {"Less than or equal to 5";$_}
Less than or equal to 5
1
Less than or equal to 5
2
Less than or equal to 5
3
Less than or equal to 5
4
Less than or equal to 5
5
Greater than 5
6
Greater than 5
7
Greater than 5
8
Greater than 5
9
Greater than 5
10
>get-help invoke-ternary -examples

NAME
    Invoke-Ternary

SYNOPSIS
    Similar to the C# ? : operator e.g. name = (value != null) ? String.Empty : value

    -------------------------- EXAMPLE 1 --------------------------

    C:\PS>1..10 | ?: {$_ -gt 5} {"Greater than 5";$_} {"Less than or equal to 5";$_}


    Each input number is evaluated to see if it is > 5.  If it is then "Greater than 5" is
    displayed otherwise "Less than or equal to 5" is displayed.







PS 12:33 D:\Data
>1..10 | ?: {$_ -gt 5} {"Greater than 5";$_} {"Less than or equal to 5";$_}
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5
Less than or equal to 5

So, it looks like a function defined exactly the same as invoke-ternary works fine, even though invoke-ternary doesn't! Does anyone know what I'm missing here? This is on XP SP3, running Powershell v2. Here's my host info:

PS>$host

Name             : ConsoleHost
Version          : 2.0
InstanceId       : 46d53ea1-f23c-4798-9de4-0b1b15846576
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-GB
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

PS>get-wmiobject win32_operatingsystem | select Version,BuildNumber

Version                                                     BuildNumber
-------                                                     -----------
5.1.2600                                                    2600
Coordinator
Feb 17, 2011 at 5:41 AM

Hmm, not sure why it's not working either but thanks for reporting it.  I'll take a look at it.

Coordinator
Feb 17, 2011 at 5:43 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.