PSAdsi-Copy.psm1

function Copy-AdsiUser {
    [CmdletBinding()]
    param (
        $AdsiModele = (get-adsiuser open\jdoe),
        [Parameter(Mandatory=$true)]$SamAccountName = $null,
        [Parameter(ValueFromRemainingArguments=$true)]$ExtraParameters
    )
    begin {
        $Properties = [ordered]@{}
        $UnnamedParams = @()
        $Name = $null
        $ExtraParameters | ForEach-Object -Process {
            if ($_ -match "^-") {
                if ($Name) {
                    $Properties.$Name = $true
                }
                $Name = $_ -replace "^-|:$"
            } else {
                if ($Name) {
                    $Properties.$Name += $_
                    $Name = $null
                } else {
                    $UnnamedParams += $_
                }
            }
        } -End {
            if ($Name) {
                $Properties.$Name = $true
            }
        }
        @(
            # 'samaccountname',
            # 'userprincipalname',
            'displayname',
            'department',
            'title',
            'givenname',
            'sn',
            'company',
            'description',
            'mstsallowlogon',
            'mail',
            'codepage',
            'scriptpath',
            'title',
            # 'msds-supportedencryptiontypes',
            'userparameters',
            # 'useraccountcontrol',
            'memberof',
            # 'lockouttime',
            # 'dscorepropagationdata',
            'telephonenumber',
            'ipphone',
            'mobile',
            'homephone',
            'facsimiletelephonenumber',
            # 'info',
            'profilepath',
            'department',
            'countrycode',
            'primarygroupid',
            # 'othermailbox',
            'msexchomaadminwirelessenable',
            'extensionattribute1',
            'streetaddress',
            'co',
            'st',
            'l',
            'c',
            'postofficebox',
            'postalcode',
            # 'extensionattribute2',
            # 'extensionattribute3',
            # 'extensionattribute4',
            # 'extensionattribute5',
            # 'extensionattribute6',
            # 'extensionattribute7',
            # 'extensionattribute8',
            # 'extensionattribute9',
            # 'extensionattribute10',
            # 'extensionattribute11',
            # 'extensionattribute12',
            # 'extensionattribute13',
            'extensionattribute14',
            'extensionattribute15'
        ) | ?{$Properties.Keys -notcontains $_} | %{
            $Properties.$_ = $AdsiModele.properties.$_
        }
        # $Properties.SamAccountName = $SamAccountName
        $Properties.userprincipalname = $AdsiModele.properties.userprincipalname -replace($AdsiModele.properties.samaccountname,$SamAccountName)
        $Properties.Password = 'Azerty*123!'
    }
    process {
        $properties | ft
        if ($AdsiModele.Path -and $AdsiModele.properties) {
            [adsi]$OU = $AdsiModele.Path -replace('(LDAP://)CN=[^,]+,(OU=.*)','$1$2')
            [adsi]$new = $OU.Create("user","CN=$samaccountname")
            # $($new) | ft # Write-Object -backGroundColor red
            $new.Put("samaccountname", $samaccountname)
            $New.CommitChanges()
            $New.SetInfo() | Out-Null
            $new = $new | Set-AdsiProperties @Properties -PassThru
            $new.Properties | ft PropertyName,Value
        }
    }
    end {

    }
}