How do I provide param attributes for a script in PowerShell?

前端 未结 2 1046
臣服心动
臣服心动 2020-12-30 02:26

I have this script that can be called in two ways:

MyScript -foo path\\to\\folder

or

MyScript -bar path\\to\\folder
         


        
相关标签:
2条回答
  • 2020-12-30 03:14

    A couple of things: You need to use parameter sets to tell PowerShell that there are mutually exclusive ways to invoke your script; that is to say, you cannot use the switch and the string at the same time. The sets also serve to allow you to set the position of both $bar and $filepath to be at index 0. Switches don't need to be positionally placed as they are not ambiguous to the binder and be placed anywhere. Also, at least one parameter in each set should be mandatory.

    function test-set {
        [CmdletBinding(DefaultParameterSetName = "BarSet")]
        param(
            [parameter(
                mandatory=$true,
                parametersetname="FooSet"
            )]
            [switch]$Foo,
    
            [parameter(
                mandatory=$true,
                position=0,
                parametersetname="BarSet"
            )]
            [string]$Bar,
    
            [parameter(
                mandatory=$true,
                position=1
            )]
            [io.fileinfo]$FilePath
        )
    @"
      Parameterset is: {0}
      Bar is: '{1}'
      -Foo present: {2}
      FilePath: {3}
    "@ -f $PSCmdlet.ParameterSetName, $bar, $foo.IsPresent, $FilePath
    }
    

    The CmdletBinding attribute is needed to specify which parameter set should be the default if the function is invoked without parameters.

    Here's the syntax help for the above configuration:

    PS> test-set -?
    
    NAME
        test-set
    
    SYNTAX
        test-set [-Bar] <string> [-FilePath] <FileInfo>  [<CommonParameters>]
    
        test-set [-FilePath] <FileInfo> -Foo  [<CommonParameters>]
    

    And here's the output for various invocations:

    PS> test-set barval C:\temp\foo.zip
      Parameterset is: BarSet
      Bar is: 'barval'
      -Foo present: False
      FilePath: C:\temp\foo.zip
    
    PS> test-set -foo c:\temp\foo.zip
      Parameterset is: FooSet
      Bar is: ''
      -Foo present: True
      FilePath: c:\temp\foo.zip
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-30 03:21

    As explained here you can specify the parameter position of your parameter.

    [parameter(Position=0)]
    
    0 讨论(0)
提交回复
热议问题