none
Problem with understanding syntax: mandatory RRS feed

  • Question

  • Hi forum,
    there is a script in the technet.gallery: http://gallery.technet.microsoft.com/scriptcenter/How-to-check-if-Active-f27b7d39
    (How to check fi Active Directory User password is expired).
    I'm having some problems understanding the syntax in the function: Get-OSCADUserPasswordExpiration:

    [CmdletBinding(DefaultParameterSetName='Sam')]
        Param
        (
            [Parameter(Mandatory=$true,Position=0,ParameterSetName='Sam')]
            [Alias('sam')][String[]]$SamAccountName,
            [Parameter(Mandatory=$true,Position=0,ParameterSetName='Csv')]
            [Alias('path')][String]$CsvFilePath,
            [Parameter(Mandatory=$true,Position=1)]
            [Alias('day')][ValidateScript({$($_ -ge 1) -and `
            $($_ -le $((Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.TotalDays))})]
            [Int]$NextDay
        )
    as we can see, all three parameters are mandatory, right?
    How is it possible to call the function - there is an example-Get-OSCADUserPasswordExpiration - SamAccountName doris,katrina -NextDay 10 - where the parameter 'csv' is omitted?

    Thanks for your help
    Purclot


    Tuesday, March 25, 2014 11:51 AM

Answers

  • Hi Purclot,

    this is due to the "ParameterSetName"-Property. You can only meet exactly one ParameterSet. Using ParameterSets means that PowerShell accepts several valid combinations of parameters. One set for each parameterset defined, each set containing all parameters assigned to its parameterset name plus all those that do not have any parameterset assigned.

    In your case, that means the function accepts either of these combinations of parameters:

    sam: SamAccountName, NextDay
    csv: CsvFilePath, NextDay

    Your input defines which parameterset is chosen. Only after choosing a parameterset, powershell will validate, whether all mandatory parameters were passed.

    Cheers,
    Fred

    Edit: By the way, this means the function will throw an error if you specify both SamAccountName and CsvFilePath, because there is no ParameterSet that contains both parameters.


    There's no place like 127.0.0.1



    • Edited by FWN Tuesday, March 25, 2014 1:30 PM
    • Marked as answer by Purclot Tuesday, March 25, 2014 3:12 PM
    Tuesday, March 25, 2014 12:41 PM
  • Hi Purclot,

    sure I can. Sometimes PowerShell has trouble deciding which parameterset is the correct one, based on the user-input. If that's the case, it'll choose the DefaultParameterSetName.

    Get-OSCADUserPasswordExpiration "somestring" 4

    Is a possible input for the function. Now Powershell will try to match input-types to parametersets. However! Both defined parametersets will accept a string first and an integer second (a single string is valid input for a string-array). Thus Powershell can't decide, which parameterset to choose. Since one of the possible parametersets however is the DefaultParameterSet, it will use that Parameterset. This means, "somestring" will be interpreted as a SamAccountName.

    To test this, just remove the DefaultParameterSetName property and try the above command. Powershell won't know how to resolve the input into a parameterset and throw an error.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Purclot Tuesday, March 25, 2014 2:50 PM
    Tuesday, March 25, 2014 2:37 PM

All replies

  • Hi Purclot,

    this is due to the "ParameterSetName"-Property. You can only meet exactly one ParameterSet. Using ParameterSets means that PowerShell accepts several valid combinations of parameters. One set for each parameterset defined, each set containing all parameters assigned to its parameterset name plus all those that do not have any parameterset assigned.

    In your case, that means the function accepts either of these combinations of parameters:

    sam: SamAccountName, NextDay
    csv: CsvFilePath, NextDay

    Your input defines which parameterset is chosen. Only after choosing a parameterset, powershell will validate, whether all mandatory parameters were passed.

    Cheers,
    Fred

    Edit: By the way, this means the function will throw an error if you specify both SamAccountName and CsvFilePath, because there is no ParameterSet that contains both parameters.


    There's no place like 127.0.0.1



    • Edited by FWN Tuesday, March 25, 2014 1:30 PM
    • Marked as answer by Purclot Tuesday, March 25, 2014 3:12 PM
    Tuesday, March 25, 2014 12:41 PM
  • Hi Fred,

    thanks! It explains a lot!
    Can you also please explain [CmdletBinding(DefaultParameterSetName='Sam')] in this example? Thank you Purclot

    Tuesday, March 25, 2014 2:21 PM
  • Hi Purclot,

    sure I can. Sometimes PowerShell has trouble deciding which parameterset is the correct one, based on the user-input. If that's the case, it'll choose the DefaultParameterSetName.

    Get-OSCADUserPasswordExpiration "somestring" 4

    Is a possible input for the function. Now Powershell will try to match input-types to parametersets. However! Both defined parametersets will accept a string first and an integer second (a single string is valid input for a string-array). Thus Powershell can't decide, which parameterset to choose. Since one of the possible parametersets however is the DefaultParameterSet, it will use that Parameterset. This means, "somestring" will be interpreted as a SamAccountName.

    To test this, just remove the DefaultParameterSetName property and try the above command. Powershell won't know how to resolve the input into a parameterset and throw an error.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Purclot Tuesday, March 25, 2014 2:50 PM
    Tuesday, March 25, 2014 2:37 PM
  • Hi Fred,

    perfect, thanks! I wish your explaination would be put in the official PS-help, it's elegant and easy to understand! I red the ps-help on that topic several times - ok, I'm not a native english-speaker - and every time I understood it in a different way.. ;-)

    Thanks
    Purclot

    Tuesday, March 25, 2014 2:47 PM