Powershell function forcing only ONE switch

This topic contains 8 replies, has 6 voices, and was last updated by  Fredrik Kacsmarck 3 weeks, 3 days ago.

  • Author
    Posts
  • #82937

    Gavin Williams
    Participant

    [apols if this appears twice, I submitted before but it didn't show up on the list]
    Hi,

    I have a function which I want to set up. I would like the function to either take the -e or -f switches, but not both.

    e.g.
    foo -e = OK
    foo -f = OK, but then would need a mandatory filename input
    foo -f filename = OK
    foo -e filename = not-OK
    foo -e -f = not-OK as the foo function should either work in e-mode or f-mode.
    foo -f -e = not-OK
    foo -f filename -e = not OK

    Here is what I have so far, but it doesn't do what I need, as it doesn't force only one switch, (and then if the switch is -f it should force a filename input)

    function scrap-Item
        {
        [CmdletBinding()]
        param (
            #[Parameter (Mandatory=$true)]
            [string] $input,
            [switch] $extensionmode,
            [switch] $filemode
    
            )
        if ($extensionmode)
        {
            write-output "Executing Script in Extension mode"
            if ($input -ne "")
            {
                write-output "Scrapping $input" 
                $files = Get-ChildItem -Path 'T:\Downloads' | where {$_.Extension -eq $input}
                $shell = New-Object -ComObject 'Shell.Application'
                ForEach ($file in $files)
                {
    	            $shell.NameSpace(0).ParseName($file.FullName).InvokeVerb('delete')
                }
            }
            else {Write-Output "empty input"}
        }
        else {Write-Output "executing script in null mode"}
        
    }
    

    Any help is gratefully appreciated!

  • #82939

    Fredrik Kacsmarck
    Participant

    As far as I know you need to check that manually.

    E.g.

    if(($extension -eq $true) -and ($filemode -eq $true))
    {
       throw "You can't use both modes at the same time"
    }
    
  • #82942

    Gavin Williams
    Participant

    Thanks for the swift response Fredrik. Slightly disappointing that PS doesn't handle natively but your answer looks great as a workaround. 🙂

    What do you suggest regarding making the $input parameter mandatory if using -f (filemode), but not -e (extensionmode)?
    I could do a similar throw statement I guess but was interested in understanding if parameters can be made "conditionally mandatory" in the definition vs. later in the script.

    • #82945

      Olaf Soyk
      Participant

      ... not sure if does exactly what you're asking for but you could think about parameter sets.

      get-help about_Functions_Advanced_Parameters
    • #82952

      Curtis Smith
      Participant

      Sounds to me like Parameter Sets are exactly what Gavin is looking for.

  • #82955

    Gavin Williams
    Participant

    Thanks Curtis.. I'm very impressed that such a young boy as Olaf would know about parameter sets! 🙂 I'll give them a whirl

  • #83002

    Richard Siddaway
    Moderator

    Parameter sets are definitely the way to go. PowerShell v6 may get the option of declaring 2 parameters to be mutually exclusive but the way that functionality will work is still under debate

  • #83042

    postanote
    Participant

    Here is a quick example, using your snippet of what the folks re suggesting...

    function scrap-Item
    {
    [CmdletBinding()]

    param
    (
    [Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True)]
    [string]$input,
    [Parameter(Position=1,Mandatory=$True,ValueFromPipeline=$True)]
    [Parameter(ParameterSetName='$extensionmode')]
    [switch]$ext,
    [Parameter(ParameterSetName='$filemode')]
    [switch]$file
    )
    if ($extensionmode)
    {
    write-output "Executing Script in Extension mode"
    if ($input -ne "")
    {
    "Scrapping $input"

    }
    else {Write-Output "empty input"}
    }
    else {Write-Output "executing script in null mode"}

    }

    #results
    scrap-Item -input TestUser -ext
    scrap-Item -input TestUser -file

  • #83114

    Fredrik Kacsmarck
    Participant

    Ah, so that is one use case for parameterset.
    You learn something every day 🙂

You must be logged in to reply to this topic.