none
[Powershell] Is there a way to have a parameter as switch as well as string RRS feed

  • Question

  • hi,

    I searched the internet on this. However, I couldnt find anything that would give me an answer to this.

    Finally turning here for help. What I'm trying in powershell is as follows:

    I created a script like this (please forgive me if i've made mistakes anywhere, i'm still learning powershell):

    [CmdletBinding(PositionalBinding=$false)] Param( [switch]$Param1, [string]$Param2, )

    For Param1, I can check in the script for true OR false values and perform some actions. But for Param2, I need to pass a string while using this (i.e. -Param2 <someString>) or else it throws an error. However, lets say, I want to pass it as a switch as well. So say, if the file is PSTest.ps1,

    and I want to pass on the command line as :

    Powershell -File PSTest.ps1 -Param1 -Param2

    without passing any string value to Param2. I know we can pass a default value to a string. So if we don't pass the parameter -Param2, it would take the default value for Param2 that we define in script. Thats the thing. It should not take the default value unless I pass -Param2 (hence the switch). Also '-Param2 <someString>' should also work.
    Is it possible to do such thing?

    Hope I'm able to convey my question there. Thanks in advance.




    • Edited by AniketGM Tuesday, February 26, 2019 2:11 PM
    Tuesday, February 26, 2019 1:28 PM

Answers

  • A switch parameter is either specified or not. They do not accept strings.

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-6#switch-parameters


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    Tuesday, February 26, 2019 2:50 PM
    Moderator
  • A parameter can be either a switch, which can only accept a Boolean, or a string, which can only accept a string. I know of no way to get around it, except perhaps to define a default. Note, a string can be just about anything except missing.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by AniketGM Wednesday, February 27, 2019 6:53 AM
    Tuesday, February 26, 2019 2:09 PM
    Moderator
  • Actually a string can be missing and used as a Boolean:

    function test{
        Param (
            [string]$Param1,
            [string]$Param2
        )
        if($Param1){ 
            # do something with param1
        }
        if ($Param2) {
            # do something with param2
        }
    }
    

    If the string is missing the parameter will evaluate as $false.


    \_(ツ)_/

    • Marked as answer by AniketGM Wednesday, February 27, 2019 6:52 AM
    Tuesday, February 26, 2019 2:16 PM
  • jrv is correct. What I meant is if you specify the parameter at the command line, as in the example the OP gave, you need to pass a value. PowerShell will then expect a value. If there is no value, don't specify the parameter.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by AniketGM Wednesday, February 27, 2019 6:53 AM
    Tuesday, February 26, 2019 2:31 PM
    Moderator
  • To set it to true give a value.  To set false don't use it.

    function test{
        Param (
            [string]$Param1,
            [string]$Param2
        )
        if($Param1){ 
            Write-Host do something with param1
        }
        if ($Param2) {
            Write-Host  do something with param2
        }
    }
    test -Param1 $true -paran $false

    You cannot use it as a switch parameter as noted above.


    \_(ツ)_/

    • Marked as answer by AniketGM Wednesday, February 27, 2019 6:53 AM
    Tuesday, February 26, 2019 2:32 PM

All replies

  • A parameter can be either a switch, which can only accept a Boolean, or a string, which can only accept a string. I know of no way to get around it, except perhaps to define a default. Note, a string can be just about anything except missing.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by AniketGM Wednesday, February 27, 2019 6:53 AM
    Tuesday, February 26, 2019 2:09 PM
    Moderator
  • Actually a string can be missing and used as a Boolean:

    function test{
        Param (
            [string]$Param1,
            [string]$Param2
        )
        if($Param1){ 
            # do something with param1
        }
        if ($Param2) {
            # do something with param2
        }
    }
    

    If the string is missing the parameter will evaluate as $false.


    \_(ツ)_/

    • Marked as answer by AniketGM Wednesday, February 27, 2019 6:52 AM
    Tuesday, February 26, 2019 2:16 PM
  • I tried this. When I pass,
    Powershell -File PSTest.ps1 -Param1, -Param2
    It gives me error that -Param2 is missing string value.
    Particularly, this one:
    PSTest.ps1 : Missing an argument for parameter 'Param2'. Specify a parameter of type 'System.String' and try again.
    Tuesday, February 26, 2019 2:28 PM
  • jrv is correct. What I meant is if you specify the parameter at the command line, as in the example the OP gave, you need to pass a value. PowerShell will then expect a value. If there is no value, don't specify the parameter.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by AniketGM Wednesday, February 27, 2019 6:53 AM
    Tuesday, February 26, 2019 2:31 PM
    Moderator
  • To set it to true give a value.  To set false don't use it.

    function test{
        Param (
            [string]$Param1,
            [string]$Param2
        )
        if($Param1){ 
            Write-Host do something with param1
        }
        if ($Param2) {
            Write-Host  do something with param2
        }
    }
    test -Param1 $true -paran $false

    You cannot use it as a switch parameter as noted above.


    \_(ツ)_/

    • Marked as answer by AniketGM Wednesday, February 27, 2019 6:53 AM
    Tuesday, February 26, 2019 2:32 PM
  • ok, so, switch can also take a stringValue. why didn't google give me that answer. anyways. I'm gonna try this one. hoping to work. let's see. thanks.

    • Edited by AniketGM Tuesday, February 26, 2019 2:41 PM
    Tuesday, February 26, 2019 2:39 PM
  • A switch parameter is either specified or not. They do not accept strings.

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-6#switch-parameters


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)



    Tuesday, February 26, 2019 2:50 PM
    Moderator
  • Thanks guys, [ Richard, jrv ]. It looks like what I had in mind to implement may not be possible, since the string parameter doesn't have this feature of switch that I expected. I have no option but to pass a string to it anyways. But I'm glad I got to know new things. I'm marking this thread as answered.

    Thanks, AniketGM.


    • Edited by AniketGM Wednesday, February 27, 2019 6:56 AM
    Wednesday, February 27, 2019 6:50 AM