Author Posts

October 26, 2017 at 11:11 am

[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!

October 26, 2017 at 11:27 am

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"
}

October 26, 2017 at 11:43 am

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.

October 26, 2017 at 11:50 am

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

get-help about_Functions_Advanced_Parameters

October 26, 2017 at 1:30 pm

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

October 26, 2017 at 1:32 pm

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

October 26, 2017 at 3:50 pm

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

October 26, 2017 at 7:12 pm

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

October 27, 2017 at 3:21 pm

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