Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell PS传递参数设置为开关_Powershell - Fatal编程技术网

Powershell PS传递参数设置为开关

Powershell PS传递参数设置为开关,powershell,Powershell,Hi在此函数中,如何将'Install'参数传递给$action开关,而不是使用默认的'Report' Function MyFunc() { [CmdletBinding(DefaultParameterSetName = 'Report', PositionalBinding = $true)] param ( [Parameter(ParameterSetName = 'Report',

Hi在此函数中,如何将'Install'参数传递给$action开关,而不是使用默认的'Report'

Function MyFunc()
{
    [CmdletBinding(DefaultParameterSetName = 'Report',
                   PositionalBinding = $true)]
    param
    (
        [Parameter(ParameterSetName = 'Report',
                   Position = 0)]
        [Parameter(ParameterSetName = 'Install',
                   Position = 0)]
        [switch]$Action
    )

    switch ($PsCmdlet.ParameterSetName)
    {
        'Report' {
            Write-Output "Report chosen!" 
            break
        }
        'Install' {
            Write-Output "Install chosen!"
            break
        }
    }
}
当我这样做的时候

MyFunc-操作“安装”

抛出错误:

MyFunc:找不到接受参数“Install”的位置参数


谢谢。

如果您只想支持一个参数:

Function MyFunc()
{
    [CmdletBinding(DefaultParameterSetName = 'Report',
                   PositionalBinding = $true)]
    param
    (
        [Parameter(ParameterSetName = 'Action',
                   Position = 0)]
        $Action        
    )

    switch ($Action)
    {
        'Report' {
            Write-Output "Report chosen!" 
            break
        }
        'Install' {
            Write-Output "Install chosen!"
            break
        }
    }
}

Get-Help MyFunc
MyFunc -Action 'Install' 
执行它:

C:\code> powershell.exe .\test.ps1

NAME
    MyFunc

SYNTAX
    MyFunc  [<CommonParameters>]

    MyFunc [[-Action] <Object>]  [<CommonParameters>]


ALIASES
    None


REMARKS
    None

Install chosen!

我可能错了,因为我的Powershell已经生锈了,但它不应该是:安装位置=1吗?