Powershell – Parameter switching on command line

So you want to have the parameters for your command line change depending on the parameters you select

Example

    function Start-APIStuff {
        [CmdletBinding(DefaultParameterSetName="WindowsAuth")]

            Param (

#define the ParameterSets
                [Parameter(ParameterSetName='WindowsAuth')]
                [Switch]$WindowsAuth,
    
                [Parameter(ParameterSetName='InternalAuth')]
                [Switch]$InternalAuth,
#Define which parameters are common to all parameter sets                
                [Parameter(Mandatory=$true)]
                [String]$Computername,

                [Parameter(Mandatory=$true)]
                [String]$apiKey,
#Define the parameters that are unique to each parameter set
#In my case i only want to allow the default windows credentials to be used for windowsauth 
#So credentials parameter is only to get an encrypted credential and then parse it to test for an Invoke-webrequest             
                [Parameter(ParameterSetName='InternalAuth', Mandatory=$true)]
                [ValidateNotNull()]
                [System.Management.Automation.PSCredential]
                [System.Management.Automation.Credential()]
                $Credential = [System.Management.Automation.PSCredential]::Empty
#refer https://duffney.io/addcredentialstopowershellfunctions/#adding-a-credential-parameter about credentials    
                
            )
    }    

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.