I have an odd scenario. We had to move certain user mail attributes to otherMailbox to avoid AAD sync guest account create errors coming from affiliate. Now I have to do something like this to make sure an email is picked if mail is empty in AD. The below works fine.
Get-ADUser SamAccountName -pr mail, othermailbox | select @{N='Mail';E={if (!($_.mail)) {$_.otherMailbox}else{$_.mail}}}
Now the hard part. How would I get a user's manager's email in this case, if their mail was moved to OtherMailbox? Doing something like these next 2 lines, I can get one or the other. But how to do the IF ELSE like above in these cases?
(Get-ADUser $_.manager -Properties mail, othermailbox).otherMailbox
(Get-ADUser $_.manager -Properties mail, othermailbox).mail
It doesn't seem possible to do something like this without a foreach loop where a variable can be checked for which attribute exists or not? I'm ok with re-writing this to take care of. Just wondering if there's some other method I might be missing? Thx
Working now (foreach removed to shorten code example). Don't use select after $AD or $MGR Get-ADUser array or things get messy. Expression logic only works on BaseType Microsoft.ActiveDirectory.Management.ADAccount for $AD and $MGR variable types.
$AD = Get-ADUser $User.SamAccountName -Properties Company,employeeId,givenname,initials,sn,Mail,OtherMailbox,SamAccountName,Department,Title,telephoneNumber,mobile,Manager
$MGR = Get-ADUser $AD.Manager -Properties Mail, OtherMailbox
$AD | select @{Name="Org ID"; Expression={$OrgID}},
@{Name="Employee ID"; Expression={$AD.employeeId}},
@{Name="FirstName"; Expression={$AD.givenname}},
@{Name="MiddleName"; Expression={$AD.initials}},
@{Name="LastName"; Expression={$AD.sn}},
@{N='Email';E={if (!($AD.mail)) {$AD.otherMailbox}else{$AD.mail}}},
@{Name="Username"; Expression={$AD.samaccountname}},
@{Name="Manager Email Address"; Expression={if (!($MGR.mail)) {$MGR.otherMailbox}else{$MGR.mail}}} |
export-csv Path -NoTypeInformation -Append