lazywinadmin / AdsiPS

PowerShell module to interact with Active Directory using ADSI and the System.DirectoryServices namespace (.NET Framework)

Home Page:http://www.lazywinadmin.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Review ParameterSetName to allow lazy typing

lazywinadmin opened this issue · comments

Example: When using Get-ADSIUser, I should be able to do:
Get-ADSIUser myaccount, instead of having to specify -identity

@lazywinadmin - this should be resolved now. I have included a fix as part of my recent PR.

Let me know if you need anything!

Unsure we should go forward with Position=0 approach in the future, Instead maybe re-arrange params ?

Unsure we should go forward with Position=0 approach in the future, Instead maybe re-arrange params ?

When using ParameterSetName attributes you have to specify Position = x if you want to use "nameless" parameters (meaning, only supplying a value.. ex: Some-Func -Name 'Gary' versus nameless: Some-Func 'Gary' ..

It is worth digging into more, to see if it is possible to force PowerShell to respect order without having to specify Position, when using ParameterSetName..

I wrote the following functions to highlight the root issue (at a high level):

function Test-ParamOrder_One {
    param(
        [Parameter(Mandatory, ParameterSetName="One")]
        [string]$ParamOne,
        
        [Parameter(Mandatory, ParameterSetName="Two")]
        [string]$ParamTwo,
        
        [Parameter(Mandatory, ParameterSetName="One")]
        [string]$ParamThree
    )
    # Test-ParamOrder_One "first" "third" 
    # The above command does not work - this is due to ParameterSetName
    # When using ParameterSetName, and you want to be able to supply "parameterless" values
    #    you have to use the "Position = x" attribute, or else PowerShell does not
    #    know which ParameterSet the given param belongs to
    Write-Host $ParamOne
    Write-Host $ParamTwo
    Write-Host $ParamThree
}

function Test-ParamOrder_Two {
    param(
        [Parameter(Mandatory)]
        [string]$ParamOne,
        
        [Parameter(Mandatory)]
        [string]$ParamTwo,
        
        [Parameter(Mandatory)]
        [string]$ParamThree
    )
    # Test-ParamOrder_Two "first" "second" "third"
    # The above command works without the need to specify parameter names
    Write-Host $ParamOne
    Write-Host $ParamTwo
    Write-Host $ParamThree
}