Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Get users
- $users = get-aduser -searchbase "OU=ou,OU=Hosted Exchange Customers,DC=DMZ,DC=local" | select samaccountname, name,enabled
- #Loop through users to get info
- foreach ($user in $users)
- {
- #Get the forwarding address on the mailbox
- $fwd = get-mailbox -Identity $user.samaccountname | select (get-mailbox -Identity $user.samaccountname).forwardingaddress
- if($fwd)
- {
- #Get the SMTP of the forwarding address if it exists
- $PrimarySMTP = (get-Recipient $fwd | select PrimarySMTPAddress).PrimarySMTPAddress
- }
- else{$PrimarySMTP = ""}
- #Create custom object to export CSV with all info.
- New-Object -TypeName PSCustomObject -Property @{
- User= $User.Name
- Enabled = $user.Enabled
- ForwardingAddress= $PrimarySMTP} >> "c:\Users\x\Desktop\forwards.csv"
- }
- ------------------------------------------------------------------------------------------------------------------------------------------
- This fails on the -expand and is the reason I tried the above method.
- #Get users
- $users = get-aduser -searchbase "OU=ou,OU=Hosted Exchange Customers,DC=DMZ,DC=local" | select samaccountname, name,enabled
- #Loop through users to get info
- foreach ($user in $users)
- {
- #Get the forwarding address on the mailbox
- $fwd = get-mailbox -Identity $user.samaccountname | select -expand ForwardingAddress
- if($fwd)
- {
- #Get the SMTP of the forwarding address if it exists
- $PrimarySMTP = (get-Recipient $fwd | select PrimarySMTPAddress).PrimarySMTPAddress
- }
- else{$PrimarySMTP = ""}
- #Create custom object to export CSV with all info.
- New-Object -TypeName PSCustomObject -Property @{
- User= $User.Name
- Enabled = $user.Enabled
- ForwardingAddress= $PrimarySMTP} >> "c:\Users\x\Desktop\forwards.csv"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement