When I ran command below it works predictably
get-mailbox -identity xxx.xxx@domain .com
when I try to ran the same command in a script it keep getting errors could not be found on domain controller
$users = Import-Csv "C:\Users\xx\Documents\CSV\PORB.csv" foreach ($user in $users) { $Recipients = Get-Mailbox -Identity $user.users [array]$AllEmailAddresses = $Recipient.EmailAddresses [array]$NoX500Addresses = $Recipient.EmailAddresses | Where {$_ -notlike "X500:*"} Set-Mailbox -Identity $Recipient.Identity -EmailAddresses $NoX500Addresses
Error I get: Get-Mailbox : The operation could not be performed because object 'd.j@domain.com' could not be found on domain controller 'XXX.aaa.bbbb.ccc.net'. At line:5 char:19 + $Recipients = Get-Mailbox -Identity $user.users }
**I am not sure what I am doing wrong I also included ** $AdminSessionADSettings.ViewEntireForest = $true
Found another Script on the web to get domain list and act on each domain in the list
$objForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() $DomainList = @($objForest.Domains | Select-Object Name) $Domains = $DomainList | foreach {$_.Name}
Act on each domain
foreach($Domain in ($Domains)){ $users = Import-Csv "C:\Users\ramer\Documents\CSV\PORB.csv" foreach ($user in $users) { $Recipients = Get-Mailbox -Identity $user.users [array]$AllEmailAddresses = $Recipient.EmailAddresses [array]$NoX500Addresses = $Recipient.EmailAddresses | Where {$_ -notlike "X500:*"} Set-Mailbox -Identity $Recipient.Identity -EmailAddresses $NoX500Addresses}