none
PowerShell - Parameter Dependencies within Parameter Sets RRS feed

  • Question

  • Hi All,

    I have a PowerShell script with three parameter sets as shown below:

    [CmdLetBinding(DefaultParameterSetName="None")]          #Allows case where neither GroupDN nor Searcbase is specified.

    param(
    [Parameter(Mandatory=$true,ParameterSetName = 'ByGroupDN')]
    [ValidateNotNullorEmpty()]
    [string]$GroupDN,

    [Parameter(Mandatory=$true,ParameterSetName = 'BySearch')]
    [ValidateNotNullorEmpty()]
    [string]$SearchBase,

    [Parameter(Mandatory=$true)]
    [ValidateNotNullorEmpty()]
    [string]$LogPath,

    [Parameter(Mandatory=$false)]
    [ValidateNotNullorEmpty()]
    [int32]$LogsToSave=10 #Optional parameter
    )

    This works perfectly the script performs actions against an array of user objects that is either derived from a searchbase search or by membership in a group specified by a group DN, if neither a -searchbase nor -groupDN is specified the script searches the whole domain.  So the only parameter that must be specified is the -LogPath. 

    I would like to add the functionality to email the results captured in the logfile as an option.  To do this I need two parameters an SMTP address to mail to and the SMTP server name.   Since emailing the results is optional, I wanted the SMTPaddress to not be mandatory, however the SMTPServer should be mandatory only if the SMTPAddress is specified.  

    Does anyone have an idea how this could be done? It seems to be that SMTPServer is a sort conditional parameter based on the existence for the SMTPAddress. These two parameters by themselves could be set to function this was by using a parameter set like so:

    Param {

    [Parameter(Mandatory=$false,ParameterSetName = 'MailOutput')]
    [ValidateNotNullorEmpty()]
    [string]$SMTPAddress,

    [Parameter(Mandatory=$true,ParameterSetName = 'MailOutput')]
    [ValidateNotNullorEmpty()]
    [string]$SMTPServer

    }

    But I can't get my head around how this could be set up with the parameter sets currently in place. You can't nest parameter sets and these two parameters should really be members of all three currently defined parameter sets, anyway.  I guess the question boils down to: How do you make a parameter mandatory only if another optional parameter is specified without creating a parameter set?

    Thanks in advance.

    Garrett

     

    Tuesday, June 7, 2016 8:00 PM

Answers

  • I guess the question boils down to: How do you make a parameter mandatory only if another optional parameter is specified without creating a parameter set?
    If your parameter dependencies are too complex to declare in the param statement, then you may need to evaluate your parameters at runtime in the script. (This may be easier to read/understand anyway.)

    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 7, 2016 8:09 PM
    Moderator

All replies

  • I guess the question boils down to: How do you make a parameter mandatory only if another optional parameter is specified without creating a parameter set?
    If your parameter dependencies are too complex to declare in the param statement, then you may need to evaluate your parameters at runtime in the script. (This may be easier to read/understand anyway.)

    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 7, 2016 8:09 PM
    Moderator
  • Hi Garret,

    you can do this with the least hassle by simply validating the input after the parameter binding as first part of the function/script, using if/else and throwing your own exception when one smtp parameter is specified but not the other.

    Cheers,
    Fred

    Edit: Of course you could also implement autodiscover and find the server using only the current domain information, in scenarios of local domain execution, which seems likely, considering your scripts main functionality and parameterization.


    There's no place like 127.0.0.1


    • Edited by FWN Tuesday, June 7, 2016 8:13 PM
    Tuesday, June 7, 2016 8:11 PM