问题
Can anyone please give some advice on comparing two ADAccount Objects in PowerShell (v2).
Whenever I run a comparison with Compare-Object, it only shows the difference in the Distinguished name, not the differences in the fields of those accounts.
Short of separately comparing $ADUser.Modified , or $ADUser.DisplayName , etc for every field I want to check, I'm at a loss.
Is there a way to compare each and every field in the ADUser object across the two accounts, showing which fields are different?
(If you are curious... this is actually for comparing two accounts with the same name, but in different domains)
Thanks in advance.
回答1:
This should give you the property name, what each user had as that property and if it was equal or different.
$user1 = get-aduser Test.User1 -Properties *
$user2 = get-aduser Test.User2 -Properties *
$Usercomparison = @()
$user1.GetEnumerator() | ForEach-Object {
If ($User2.($_.Key) -eq $_.Value)
{
$Comparison = 'Equal'
}
else
{
$Comparison = 'Different'
}
$UserObj = New-Object PSObject -Property ([ordered]@{
Property = $_.Key
User1 = $_.Value
User2 = $User2.($_.Key)
Comparison = $Comparison
})
$UserComparison += $UserObj
}
$UserComparison
回答2:
This is a very great solution for comparing object properties:
https://blogs.technet.microsoft.com/janesays/2017/04/25/compare-all-properties-of-two-objects-in-windows-powershell/
Jamie Nelson write a function to compare the properties of 2 AD objects.
So, with a little extra logic, we can do this pretty easily. First, we define the Compare-ObjectProperties function. That function will take any two source objects and get a unique list of all of the property names of both objects we're comparing. This is necessary because objects aren't always going to have the same set of attributes. When that is the case, we want to see where one has a null value and the other is populated. With the list of unique property names, our function can iteratively process them through Compare-Object and only return the properties that are different.
Function Compare-ObjectProperties { Param( [PSObject]$ReferenceObject, [PSObject]$DifferenceObject ) $objprops = $ReferenceObject | Get-Member -MemberType Property,NoteProperty | % Name $objprops += $DifferenceObject | Get-Member -MemberType Property,NoteProperty | % Name $objprops = $objprops | Sort | Select -Unique $diffs = @() foreach ($objprop in $objprops) { $diff = Compare-Object $ReferenceObject $DifferenceObject -Property $objprop if ($diff) { $diffprops = @{ PropertyName=$objprop RefValue=($diff | ? {$_.SideIndicator -eq '<='} | % $($objprop)) DiffValue=($diff | ? {$_.SideIndicator -eq '=>'} | % $($objprop)) } $diffs += New-Object PSObject -Property $diffprops } } if ($diffs) {return ($diffs | Select PropertyName,RefValue,DiffValue)} } $ad1 = Get-ADUser amelia.mitchell -Properties * $ad2 = Get-ADUser carolyn.quinn -Properties * Compare-ObjectProperties $ad1 $ad2
来源:https://stackoverflow.com/questions/25925461/compare-object-on-two-ad-user-accounts