PowerShell inline If (IIf)

后端 未结 7 2026
独厮守ぢ
独厮守ぢ 2020-12-07 16:24

How do I create an a statement with an inline If (IIf, see also: Immediate if or ternary If) in PowerShell?

If you also think that this should be a native Po

7条回答
  •  难免孤独
    2020-12-07 16:53

    Function Compare-InlineIf  
    {  
    [CmdletBinding()]  
        Param(  
            [Parameter(  
                position=0,  
                Mandatory=$false,  
                ValueFromPipeline=$false  
            )]  
            $Condition,  
            [Parameter(  
                position=1,  
                Mandatory=$false,  
                ValueFromPipeline=$false  
            )]  
            $IfTrue,  
            [Parameter(  
                position=2,  
                Mandatory=$false,  
                ValueFromPipeline=$false  
            )]  
            $IfFalse  
        )  
        Begin{  
            Function Usage  
            {  
                write-host @"  
    Syntax  
        Compare-InlineIF [[-Condition] ] [[-IfTrue]  or ]  
     [[-IfFalse]  or ]  
    Inputs  
        None  
        You cannot pipe objects to this cmdlet.  
    
    Outputs  
        Depending on the evaluation of the condition statement, will be either the IfTrue or IfFalse suplied parameter values  
    Examples  
       .Example 1: perform Compare-InlineIf :  
        PS C:\>Compare-InlineIf -Condition (6 -gt 5) -IfTrue "yes" -IfFalse "no"  
    
        yes
    
       .Example 2: perform IIF :  
        PS C:\>IIF (6 -gt 5) "yes" "no"  
    
        yes  
    
       .Example 3: perform IIF :  
        PS C:\>IIF `$object "`$true","`$false"  
    
        False  
    
       .Example 4: perform IIF :  
        `$object = Get-Item -ErrorAction SilentlyContinue "HKCU:\AppEvents\EventLabels\.Default\"  
        IIf `$object {`$_.GetValue("DispFilename")}  
    
        @mmres.dll,-5824  
    "@  
            }  
        }  
        Process{  
            IF($IfTrue.count -eq 2 -and -not($IfFalse)){  
                $IfFalse = $IfTrue[1]  
                $IfTrue = $IfTrue[0]  
            }elseif($iftrue.count -ge 3 -and -not($IfFalse)){  
                Usage  
                break  
            }  
            If ($Condition -IsNot "Boolean")  
            {  
                $_ = $Condition  
            } else {}  
            If ($Condition)  
            {  
                If ($IfTrue -is "ScriptBlock")  
                {  
                    &$IfTrue  
                }  
                Else  
                {  
                    $IfTrue  
                }  
            }  
            Else  
            {  
                If ($IfFalse -is "ScriptBlock")  
                {  
                    &$IfFalse  
                }  
                Else  
                {  
                    $IfFalse  
                }  
            }  
        }  
        End{}  
    }  
    Set-Alias -Name IIF -Value Compare-InlineIf  
    

提交回复
热议问题