
Write-host
write-host "-------------------------------------------------------"
Write-host

Import-module ActiveDirectory

Function RestoreObject($Object) {
    #-----------------------------------------------------------------
    If ($Object.substring($Object.length - 1, 1) -ne '*') {
        $Object += '*'
    }
    #-----------------------------------------------------------------

    $existing = Get-ADObject -Filter {name -like $Object}

    If (!$existing) {
        "Aucun objet existant trouve, la recherche d objets supprimes."
        $deleted = Get-ADObject -IncludeDeletedObjects -Filter {name -like $Object -and Deleted -eq $True}

        If ($deleted) {
            "Objet supprime trouve, restauration en cour ..."
            $deleted | Restore-ADObject
            Start-Sleep -s 5
            $restoredobject = Get-ADObject -Filter {name -like $Object}

            If ($restoredobject) {
                "Objet restauré avec succes. Essayer de trouver des informations de recuperation ..."
                $recoveryinfos = Get-ADObject -IncludeDeletedObjects -Filter {lastKnownParent -like $restoredobject.DistinguishedName -and Deleted -eq $True -and objectClass -eq 'msFVE-RecoveryInformation'}

                ForEach($recoveryinfo in $recoveryinfos) {
                    If ($recoveryinfo) {
                        "Les informations de recuperation trouve, en essayant de restaurer ..."
                        $recoveryinfo | Restore-ADObject
                        
                        Start-Sleep -s 5

                        $restoredinfo = Get-ADObject -Filter {ObjectGUID -eq $recoveryinfo.ObjectGUID}

                        If ($restoredinfo) {
                            "Les informations de recuperation restaure avec succes."
                        }
                        Else {
                            "Impossible de restaurer les informations de recuperation, interruption du script."
                            return $false
                        }
                    }
                    Else {
                        "Aucune information de recuperation trouve pour objet, interruption du script."
                        return $true
                    }
                }
            }
            Else {
                "Quelque-chose s est mal passe. Impossible de trouver l objet restaure, interruption du script."
                return $false
            }
        }
        Else {
            "Pas Object supprime trouve, interruption du script."
            return $false;
        }
    }
    Else {
        "Object existe deja, interruption du script."
        Write-host $existing
        return $false
    }
    "Restauration de Object realise avec success."
    "Terminee."
    return $true
}

$cn = Read-Host "Saisie ObjectGUID de l objet a restaurer ?"

RestoreObject($cn)

Write-host
write-host "-------------------------------------------------------"
Write-host