Skip to content

Instantly share code, notes, and snippets.

@DXPetti
Created April 4, 2020 06:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save DXPetti/670107f2944aad9d0579d722313d9b6b to your computer and use it in GitHub Desktop.
<#
.Synopsis
Restore select Ad Attributes from a mounted AD DS database
.DESCRIPTION
This cmdlet will take a OU path as input, takes a backup of the attributes of all users
in said OU, import the attributes from a currently mounted AD DS database into a
hashtable, loop through each user in the OU and apply said attributes. Finally, a export
of the users current attributes is saved in the same path as the backup for before/after
comparisons
.EXAMPLE
Restore-AdAttributes -OldAd contosodc01:777 -NewAd contosodc01 -$AdOuPath "OU=Users,DC=contoso,DC=co" -BkpPath "C:\Temp\Backup\"
#>
function Restore-AdAttributes
{
[CmdletBinding()]
[Alias()]
[OutputType([int])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$false,
Position=0)]
[string]$OldAd,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$false,
Position=1)]
[string]$NewAd,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$false,
Position=2)]
[string]$AdOuPath,
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$false,
Position=3)]
[string]$BkpPath
)
Begin
{
# Build list of Users
Get-ADUser -Filter * -SearchBase $AdOuPath -Server $NewAd | Select samaccountname | Export-Csv -Path $BkpPath\Users.csv -NoTypeInformation
$UserList = Import-Csv -Path $BkpPath\Users.csv
}
Process
{
foreach ($User in $UserList)
{
#Backup First
Get-ADUser -Identity $User.SamAccountName -Properties * -Server $NewAd | Out-File "$BkpPath\$($User.SamAccountName)_before.txt"
#Get Old Values
$OldProps = Get-ADUser -Identity $User.SamAccountName -Properties * -Server $OldAd
#Build Hash Tables
[hashtable]$OldValues = [ordered]@{
extensionAttribute1 = $OldProps.extensionAttribute1
extensionAttribute2 = $OldProps.extensionAttribute2
extensionAttribute3 = $OldProps.extensionAttribute3
extensionAttribute4 = $OldProps.extensionAttribute4
extensionAttribute5 = $OldProps.extensionAttribute5
extensionAttribute6 = $OldProps.extensionAttribute6
extensionAttribute9 = $OldProps.extensionAttribute9
extensionAttribute13 = $OldProps.extensionAttribute13
extensionAttribute14 = $OldProps.extensionAttribute14
publicDelegates = [array]$OldProps.publicDelegates
publicDelegatesBL = [array]$OldProps.publicDelegatesBL
}
#Set Old Values
foreach ($O in $OldValues.GetEnumerator())
{
Set-ADUser -Identity $User.samaccountname -Add @{$($O.Key)=$($O.Value)} -Server $NewAd -Verbose
}
#Export New Values
Get-ADUser -Identity $User.SamAccountName -Properties * -Server $NewAd | Out-File "$BkpPath\$($User.SamAccountName)_after.txt"
#Reset Hash Tables
$OldValues.Clear()
}
}
}
Restore-AdAttributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment