SOLVED

Adding a parameter to a script with no value

Copper Contributor

Is there a way to add a parameter to a script that would work like -Force or -Verbose? I don't need to provide a value, I just want it to set a variable so that if set my script will do something extra.

Or is there a way that I could use the -Force parameter? Is there some script variable that would be set that I can check if it was used?

 

Thanks.

2 Replies
best response confirmed by huntantr (Copper Contributor)
Solution

Hello @huntantr,

If I understood you correctlly, you can use Switch parameter type in your script:

Switch parameters are parameters with no parameter value. They're effective only when they're used and have only one effect.

 

For example you can use something similar to this:

function Do-Magic
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    [Alias('magic')]
    Param
    (
      [Switch]
      $SkipRabbitTrick
    )
    Begin{}
    Process{
       if($SkipRabbitTrick){
          Write-Verbose "Performance without Rabbit"
       }else{
          Write-Verbose "Taking Rabbit out of Hat"
       }
   }
   End{}
}

Hope that helps.

 

Thanks, this is what I was looking for.
1 best response

Accepted Solutions
best response confirmed by huntantr (Copper Contributor)
Solution

Hello @huntantr,

If I understood you correctlly, you can use Switch parameter type in your script:

Switch parameters are parameters with no parameter value. They're effective only when they're used and have only one effect.

 

For example you can use something similar to this:

function Do-Magic
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    [Alias('magic')]
    Param
    (
      [Switch]
      $SkipRabbitTrick
    )
    Begin{}
    Process{
       if($SkipRabbitTrick){
          Write-Verbose "Performance without Rabbit"
       }else{
          Write-Verbose "Taking Rabbit out of Hat"
       }
   }
   End{}
}

Hope that helps.

 

View solution in original post