我在两个不同的域之间比较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) {.....
答案 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) {.....
}
}