Advertisement
Tekwhat

Exchange forward / enabled

Jun 22nd, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Get users
  2. $users = get-aduser -searchbase "OU=ou,OU=Hosted Exchange Customers,DC=DMZ,DC=local" | select samaccountname, name,enabled
  3.  
  4. #Loop through users to get info
  5. foreach ($user in $users)
  6. {
  7.  
  8. #Get the forwarding address on the mailbox
  9. $fwd = get-mailbox -Identity $user.samaccountname | select (get-mailbox -Identity $user.samaccountname).forwardingaddress
  10. if($fwd)
  11. {
  12.  
  13. #Get the SMTP of the forwarding address if it exists
  14. $PrimarySMTP =  (get-Recipient $fwd | select  PrimarySMTPAddress).PrimarySMTPAddress
  15. }
  16. else{$PrimarySMTP = ""}
  17.  
  18. #Create custom object to export CSV with all info.
  19.     New-Object -TypeName PSCustomObject -Property @{
  20.                 User= $User.Name
  21.                 Enabled = $user.Enabled
  22.                 ForwardingAddress= $PrimarySMTP} >> "c:\Users\x\Desktop\forwards.csv"
  23.     }
  24.  
  25.  
  26. ------------------------------------------------------------------------------------------------------------------------------------------
  27.  
  28. This fails on the -expand and is the reason I tried the above method.
  29.  
  30. #Get users
  31. $users = get-aduser -searchbase "OU=ou,OU=Hosted Exchange Customers,DC=DMZ,DC=local" | select samaccountname, name,enabled
  32.  
  33. #Loop through users to get info
  34. foreach ($user in $users)
  35. {
  36.  
  37. #Get the forwarding address on the mailbox
  38. $fwd = get-mailbox -Identity $user.samaccountname | select -expand ForwardingAddress
  39. if($fwd)
  40. {
  41.  
  42. #Get the SMTP of the forwarding address if it exists
  43. $PrimarySMTP =  (get-Recipient $fwd | select  PrimarySMTPAddress).PrimarySMTPAddress
  44. }
  45. else{$PrimarySMTP = ""}
  46.  
  47. #Create custom object to export CSV with all info.
  48.     New-Object -TypeName PSCustomObject -Property @{
  49.                 User= $User.Name
  50.                 Enabled = $user.Enabled
  51.                 ForwardingAddress= $PrimarySMTP} >> "c:\Users\x\Desktop\forwards.csv"
  52.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement