0

I've writen a short function to retrive info/status on an installed vpn client - it has 4 switches to specify what information is returned:

Function Get-ConnectInfo() {
    [CmdletBinding()]
    Param(
        [Parameter(ParameterSetName='Binaries')][switch]$BinaryPaths,
        [Parameter(ParameterSetName='Status')][switch]$ConnectionStatus,
        [Parameter(ParameterSetName='Profiles')][switch]$Profiles,
        [Parameter(ParameterSetName='Version')][switch]$Version
    )
#
    Begin {
    # Some code here
    }
    #
    Process {
        Switch ($PSBoundParameters.Keys) {
            BinaryPaths {
                Write-Host "BinaryDetail"
            }
            Version {
                Write-Host "VersionInfo"
            }
            Profiles {
                Write-Host "Profile Info"
            }
            ConnectionStatus {
                Write-Host "Connection Status"
            }
        }
    }
}

Thing is, if you don't pass any parameters, this is the error message:

Get-ConnectInfo : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Get-ConnectInfo
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ConnectInfo], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Get-ConnectInfo

Is there an elegant way to change this error to something more informative like "Parameter not specified"?

1
  • 1
    Apparently, PowerShell Core 6 supports custom error messages with their Validate* attributes. I don't really see any official documentation on that though. I am sure that won't meet the subjective elegant requirement. Commented Nov 19, 2019 at 16:31

1 Answer 1

2

You could:

  • Set a default parameter set
  • Throw a custom error and return if it's set inside the function:
function Get-ConnectInfo() {
    [CmdletBinding(DefaultParameterSetName='noOptions')]
    Param(
        [Parameter(ParameterSetName='Binaries')][switch]$BinaryPaths,
        [Parameter(ParameterSetName='Status')][switch]$ConnectionStatus,
        [Parameter(ParameterSetName='Profiles')][switch]$Profiles,
        [Parameter(ParameterSetName='Version')][switch]$Version
    )
#
    Begin {
        if($PSCmdlet.ParameterSetName -eq 'noOptions'){
            throw 'Please pass a switch argument of either "-Version", "-Profiles", "-ConnectionStatus", or "-BinaryPaths"'
            return
        }
    }
    #
    Process {
        Switch ($PSBoundParameters.Keys) {
            BinaryPaths {
                Write-Host "BinaryDetail"
            }
            Version {
                Write-Host "VersionInfo"
            }
            Profiles {
                Write-Host "Profile Info"
            }
            ConnectionStatus {
                Write-Host "Connection Status"
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.