1

I'm trying to get the SAMAccountNames of one domain and compare them with their equals from another domain.

To get all users of dc1 I use:

Get-ADUser -Filter * -SearchBase $SearchBase | Select-Object SamAccountName |
    Export-Csv -path $exports -encoding "unicode" -notype

and then I import the csv again and try to compare them for any differences

$readthat = Import-CSV $exports -Header SamAccountName | ForEach-Object {
  $user1 = Get-ADUser -Identity $_.SamAccountName -Properties $attributes
  $user2 = Get-ADUser -Identity $_.SamAccountName -Properties $attributes -Server $dc2

  $modified = @{}
  $attributes | Where-Object { $user1.$_ -ne $user2.$_ } | ForEach-Object {
    $modified[$_] = $user2.$_
  }
}

All that works great, except that it's also trying to find the SamAccountName which of course genereates an error because the SamAccountName = SamAccountName doesn't exit.

Any hints on how to avoid this or do you guys have a more elegant solution?

the .csv looks like this:

"SamAccountName"
"foo"
"bar"

1 Answer 1

4

Don't use the -Header SamAccountName option on your import-csv should help immensely. The -Header option is for when the CSV file you are importing doesn't have a header. The Export-CSV cmdlet puts the header in there for you, so you don't have to.

Sign up to request clarification or add additional context in comments.

1 Comment

must have missed that part when reading into the import/export function. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.