比较两个DC中的域帐户 - 如何从get-ADUser中排除未找到的邮件?

时间:2014-12-11 20:48:08

标签: powershell

我在两个不同的域之间比较Domainaccounts,其中一些域在两个域上具有相同的SamAccountName。一切都很好,除了我的脚本每次在第二个域上找不到用户时都会生成错误。

有没有办法消除Get-ADUser : Cannot find an object with identity: 'duda的输出,可能会通过write-host写一个错误? 我还使用了-ErrorAction SilentlyContinue但仍然得到错误

以下是我的实际代码片段:

$user_id = Get-ADUser -Filter * -SearchBase $SearchBase | Select-Object SamAccountName

$readthat = $user_id | Foreach-Object {
$user1 = Get-ADUser -Identity $_.SamAccountName -Properties $attributes
$user2 = Get-ADUser -Identity $_.SamAccountName -Properties $attributes -Credential $WPS_AdminCredentials -Server $dc2 -ErrorAction SilentlyContinue

if ($user1.SamAccountName -eq $user2.SamAccountName) {.....

1 个答案:

答案 0 :(得分:0)

试试这个......

$user_id = Get-ADUser -Filter * -SearchBase $SearchBase | Select-Object SamAccountName

$readthat = $user_id | Foreach-Object {
    $user1 = Try{Get-ADUser -Identity $_.SamAccountName -Properties $attributes}
    catch{Write-Error "User &_ not found"}

    $user2 = try{Get-ADUser -Identity $_.SamAccountName -Properties $attributes -Credential $WPS_AdminCredentials -Server $dc2}
    catch{Write-Error "User &_ not found"}

    if ($user1.SamAccountName -eq $user2.SamAccountName) {.....

    }

}