PSAdsi-Copy.psm1

function Copy-AdsiUser {
    <#
        .SYNOPSIS
            [Descriptif en quelques mots]
        .DESCRIPTION
            [Descriptif en quelques lignes]
        .PARAMETER AdsiModele
            
        .PARAMETER SamAccountName
            
        .PARAMETER ExtraParameters
            
        .EXAMPLE
            Copy-AdsiUser
        .NOTES
            Alban LOPEZ 2020
            alban.lopez@gmail.com
            http://git/PowerTech/
        #>


    [CmdletBinding()]
    param (
        $AdsiModele = (Get-ADSIUser open\jdoe),
        [Parameter(Mandatory=$true)]$SamAccountName = $null,
        [Parameter(ValueFromRemainingArguments=$true)]$ExtraParameters
    )
    begin {
        if ($AdsiModele -is [String]) {
            $AdsiModele = $AdsiModele | Get-ADSIUser
        }
        $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 $_ -and $AdsiModele.properties.$_} | %{
            # on ajoute les autres properties
            # Write-Verbose "`$Properties.$_ = $($AdsiModele.properties.$_)"
            $Properties.$_ = $AdsiModele.properties.$_
        }
        if ($Properties.Keys -notcontains 'Password') {
            $Properties.Password = 'Azerty*123!'
        }
        if ($Properties.Keys -notcontains 'description') {
            # $global:current | Write-Object -backGroundColor Black -foreGroundColor Yellow
            $Properties.description = "Copie de $($AdsiModele.properties.userprincipalname) (par $env:USERDOMAIN\$env:USERNAME) (PSAdsi V$($global:current.version))"
        }
        if ($Properties.Keys -notcontains 'userprincipalname') {
            $Properties.userprincipalname = $AdsiModele.properties.userprincipalname -replace($AdsiModele.properties.samaccountname,$SamAccountName)
        }
        $Properties.Remove('PassThru') | Out-Null
    }
    process {
        # $properties | Write-Object -backGroundColor Black -foreGroundColor Cyan
        if ($AdsiModele.Path -and $AdsiModele.properties) {
            [adsi]$OU = $AdsiModele.Path -replace('(LDAP://)CN=[^,]+,(OU=.*)','$1$2')
            # $ou.distinguishedName.value | Write-Object -backGroundColor Black -foreGroundColor Magenta
            # $ou.distinguishedName.value | Write-Object -backGroundColor Black -foreGroundColor red
            # $ou.distinguishedName | fl *

            try {
                [adsi]$new = $OU.Create("user","CN=$samaccountname")
                $new.Put("samaccountname", $samaccountname) | Out-Null
                $New.CommitChanges() | Out-Null
                $New.SetInfo() | Out-Null
            } catch {
                Write-LogStep -prefix "L.$($_.InvocationInfo.ScriptLineNumber)" "",$_ error
                # $new.Path,$ou.distinguishedName,"LDAP://CN=$samaccountname,$($ou.distinguishedName)" | Write-Object -backGroundColor Black -foreGroundColor Cyan
                [adsi]$new = [adsi]"LDAP://CN=$samaccountname,$($ou.distinguishedName)"
                # $new.Path | Write-Object -backGroundColor Black -foreGroundColor Green
            }
            # $new.Properties | ft * # Write-Object -backGroundColor Black -foreGroundColor Cyan
            $new | Set-AdsiProperties @Properties -PassThru -Verbose:$VerbosePreference
        }
    }
    end {
    }
}