Wrapper function in PowerShell: Pass remaining parameters

后端 未结 3 1503
旧巷少年郎
旧巷少年郎 2021-01-02 03:37

I’m trying to write a wrapper function in PowerShell that basically evaluates the first parameter and based on that runs a program on the computer. All the remaining paramet

3条回答
  •  半阙折子戏
    2021-01-02 03:56

    This sort of does what you ask. You may run into trouble if you need to pass dash-prefixed options to the executable that conflict or cause ambiguity with the PowerShell common parameters. But this may get you started.

    function Invoke-MyProgram
    {
        [CmdletBinding()]
        Param
        (
            [parameter(mandatory=$true, position=0)][string]$Option,
            [parameter(mandatory=$false, position=1, ValueFromRemainingArguments=$true)]$Remaining
        )
    
        if ($Option -eq 'A')
        {
            Write-Host $Remaining
        }
        elseif ($Option -eq 'B')
        {
            & 'C:\Program Files\some\program.exe' @Remaining # NOTE: @ not $ (splatting)
        }
    }
    

提交回复
热议问题