
function New-PSADUserAccount
{
    Param(
        [Parameter(
            Mandatory = $true
        )]
        [ValidatePattern('^\d{5}$')]
        [string]$ID,

        [Parameter(
            Mandatory = $true
        )]
        [ValidateLength(4, 40)]
        [string]$SurName,

        [Parameter(
            Mandatory = $true
        )]
        [ValidateLength(4, 40)]
        [string]$GivenName,
        
        [Parameter(
            Mandatory = $true
        )]
        [ValidateCount(1, 10)]
        [string[]]$GroupsList,

        [Parameter(
            Mandatory = $false
        )]
        [ValidateScript({
                Test-Path -Path $_ -PathType Container
            })]
        [string]$ProfilePath,

        [string]$Email,

        [Parameter(
            Mandatory = $false
        )]
        [ValidateScript({ $_ -like '0[1245]*' })]
        [string]$PhoneNumber,
        
        [Parameter(
            Mandatory = $true
        )]
        [validateSet('Paris', 'Rennes', 'Nantes', 'Lyon', 'Toulouse')]
        [ArgumentCompleter(
            {
                param ( 
                    $command,
                    $parameterName,
                    $wordToComplete,
                    $commandAst,
                    $fakeBoundParameters
                )
                if ($fakeBoundParameters.ContainsKey('OUPath'))
                {
                    (Get-ADUser -Filter * `
                        -SearchBase $fakeBoundParameters['OUPath'] `
                        -Properties $parameterName `
                        -ErrorAction SilentlyContinue | 
                        Where-Object {
                            $_.SamAccountName -match '^a[0-9]{6}$' `
                                -And $_.$parameterName -like "$wordToComplete*"
                        }
                    ).$parameterName | Sort-Object -Unique
                    }
            })]
        [string]$City,

        [Parameter(
            Mandatory = $true
        )]
        [validateSet('Comptabilite', 'Direction', 'Informatique', 'RH', 'Prod')]
        [ArgumentCompleter( {
                param ( 
                    $command,
                    $parameterName,
                    $wordToComplete,
                    $commandAst,
                    $fakeBoundParameters
                )
                if ($fakeBoundParameters.ContainsKey('OUPath'))
                {
                (Get-ADUser -Filter * `
                        -SearchBase $fakeBoundParameters['OUPath'] `
                        -Properties $parameterName `
                        -ErrorAction SilentlyContinue | 
                        Where-Object {
                            $_.SamAccountName -match '^a[0-9]{6}$'`
                                -And $_.$parameterName -like "$wordToComplete*"
                        }
                ).$parameterName | Sort-Object -Unique
                    }
        })]
        [string]$Department
    )

            # instruction de creation d'utilisateur
        }

# Avec une fonction
function Get-ArgComplADProperty
{
    param ( 
        $command,
        $parameterName,
        $wordToComplete,
        $commandAst,
        $fakeBoundParameters
    )

    if ($fakeBoundParameters.ContainsKey('OUPath'))
    {
        (Get-ADUser -Filter * `
            -SearchBase $fakeBoundParameters['OUPath'] `
            -Properties $parameterName -ErrorAction SilentlyContinue | 
            Where-Object {
                $_.SamAccountName -match '^a[0-9]{6}$' -And `
                    $_.$parameterName -like "$wordToComplete*"
            }
        ).$parameterName | Sort-Object -Unique
    }
}


function New-PSADUserAccount
{
    Param(
        [Parameter(
            Mandatory = $true
        )]
        [ValidatePattern('^\d{5}$')]
        [string]$ID,

        [Parameter(
            Mandatory = $true
        )]
        [ValidateLength(4, 40)]
        [string]$SurName,

        [Parameter(
            Mandatory = $true
        )]
        [ValidateLength(4, 40)]
        [string]$GivenName,
        
        [Parameter(
            Mandatory = $true
        )]
        [ValidateCount(1, 10)]
        [string[]]$GroupsList,

        [Parameter(
            Mandatory = $false
        )]
        [ValidateScript({
                Test-Path -Path $_ -PathType Container
            })]
        [string]$ProfilePath,

        [string]$Email,

        [Parameter(
            Mandatory = $false
        )]
        [ValidateScript({ $_ -like '0[1245]*' })]
        [string]$PhoneNumber,
        
        [Parameter(
            Mandatory = $true
        )]
        [validateSet('Paris', 'Rennes', 'Nantes', 'Lyon', 'Toulouse')]
        [ArgumentCompleter(
            {
                param ( 
                    $command,
                    $parameterName,
                    $wordToComplete,
                    $commandAst,
                    $fakeBoundParameters
                )
                    Get-ArgComplADProperty @PSBoundParameters
            })]
        [string]$City,

        [Parameter(
            Mandatory = $true
        )]
        [validateSet('Comptabilite', 'Direction', 'Informatique', 'RH', 'Prod')]
        [ArgumentCompleter( {
                param ( 
                    $command,
                    $parameterName,
                    $wordToComplete,
                    $commandAst,
                    $fakeBoundParameters
                )
                Get-ArgComplADProperty @PSBoundParameters
        })]
        [string]$Department
    )

    # instruction de creation d'utilisateur
}