4

I have the following Param block at the start of my script:

Param
(
    [Parameter(Mandatory = $true)]
    [ValidateScript({Test-Path $_ -PathType Leaf})]
    [string]$Config,

    [switch]$OverThresholdOnly,
    [switch]$SendEmail,
    [switch]$Debug
)

When I run the script I get the error:

"A parameter with the name 'Debug' was defined multiple times for this command. At line:1 char:1"

Line:1 and char:1 is the start of the Param block.

If I change the $Debug to $Verbose I get the same error about Verbose. I've tried putting the $debug at the top of the Param block with the same error.

If I remove the [ValidateScript] section it works fine.

Can anybody tell me why it does this? Why [ValidateScript] is using $Debug and how to get around this short of renaming the variable?

Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
pauby
  • 683
  • 3
  • 9

1 Answers1

4

PowerShell has ubiquitous parameters which exist for every cmdlet.

Check out get-help about_common_parameters or click here.

-Debug and -Verbose are two of the common parameters. Chose a different name to avoid the naming collision.

When you add the parameter attribute it changes the way PowerShell treats the parameters. It becomes an advanced function at that point a.k.a a script cmdlet. Script cmdlets receive the common parameters automatically.

Check out get-help about_Functions_Advanced or click here

And get-help about_Functions_Advanced_Parameters or click here

Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • I was using the parameters because they were common. Stick to a common set to make life easier. I also checked they were not reserved. Even though the $Debug parameter is not being validated by the [ValidateScript] it still stops me from using the Debug parameter in my own script? – pauby Mar 01 '12 at 08:47
  • 3
    @Pauby Did you see what I wrote? When you add the parameter attribute `[Parameter(Mandatory = $true)]` PowerShell automatically adds the common parameters so that's why you are getting a naming collision. – Andy Arismendi Mar 01 '12 at 14:15