0

I am creating a script where I would like 2 parameters to be optional (and to be provided with -param val -param2 val2), followed by an unknown number of file paths. The problem I have is that when I dont provide the optional arguments powershell assumes that the first 2 parameters given originally as file paths are the params I would usually pass with a -param command.

For example, if I run

.\script.ps1 test test2

then

$args.Count == 0

Can I work around it somehow? I want the parameters to be optional and enforce it so that they can be passed only with a hyphen and a name before the value.

2
  • like this, using non positional optional parameters - stackoverflow.com/questions/12375919/… Commented Aug 6, 2014 at 10:18
  • Do you mean that the 2 optional parameters must be specified together or not at all? Commented Aug 6, 2014 at 15:57

2 Answers 2

0

Once you declare a param() block, $args is no longer available to use, thus $args.Count will be empty. It is essentially nonexistent. It must be named arguments after that or you have to use ValueFromRemainingArguments in a declared parameter within param(). Either you could pass the paths as an array or use ValueFromRemainingArguments:

#I realize this isn't what you really want, but just a suggestion
Param(
    [Parameter(Mandatory=$False)][String]$Param1,
    [Parameter(Mandatory=$False)][String]$Param2,
    [Parameter(Mandatory=$True, Position=0)][Array]$Path
)
.\script.ps1 C:\Users, C:\Windows

# or

Param(
    [Parameter(Mandatory=$False)][String]$Param1,
    [Parameter(Mandatory=$False)][String]$Param2,
    [Parameter(Position=0, ValueFromRemainingArguments=$True)][String[]]$Path
)
.\script.ps1 C:\Users C:\Windows
Sign up to request clarification or add additional context in comments.

Comments

0
function Test-Params {
[CmdletBinding(DefaultParameterSetName='FilesOnly')]
param(
    [Parameter(
        ParameterSetName='Optionals',
        Mandatory=$true
    )]
    [String]
    $Optional1 ,

    [Parameter(
        ParameterSetName='Optionals',
        Mandatory=$true
    )]
    [String]
    $Optional2 ,

    [Parameter(
        ParameterSetName='FilesOnly',
        Mandatory=$true,
        ValueFromRemainingArguments=$true,,
        Position=0
    )]
    [Parameter(
        ParameterSetName='Optionals',
        Mandatory=$true,
        ValueFromRemainingArguments=$true,
        Position=2
    )]
    [String[]]
    $Files 

)
    "Op1: $Optional1"
    "Op2: $Optional2"
    "Files: $Files"
    "File Count: $($Files.Count)"
}

Get-Help Test-Params

Test-Params 'C:\Windows','C:\Temp'

Test-Params -Optional1 'a' -Optional2 'b' 'C:\Users','C:\eclipse'

Test-Params C:\Windows C:\users C:\temp

Test-Params -Optional1 'a' -Optional2 'b' C:\Windows C:\users C:\temp

Try to run this, it should shed some light on how you can do this with parameter sets.

It wasn't clear to me whether the optional parameters needed to be specified together or not, so I assumed yes.

If not, then it could still be achieved, but the code will be longer. Let me know!

Added the ValueFromRemainingArguments attribute so that you don't have to format the files as an array, since that seemed to be the way you were calling it in the example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.