Link to home
Start Free TrialLog in
Avatar of K B
K BFlag for United States of America

asked on

Powershell: Modify Exchange 2013 script to work with Office 365

Qlemo wrote this amazing script and I was hoping he (or you) could help me modify it for Office 365.  The email addresses do not output when run against a Office 365 tenant.   Any ideas?

Get-Mailbox -Resultsize 3 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{Name="City"           ;Expression={$usr.City}},
                @{Name="StateOrProvince";Expression={$usr.StateOrProvince}},
                @{Name="PostalCode"     ;Expression={$usr.PostalCode}},
                @{Name="CountryOrRegion";Expression={$usr.CountryOrRegion}},
                @{Name="Phone"          ;Expression={$usr.Phone}},
                @{Name="HomePhone"      ;Expression={$usr.HomePhone}},
                @{Name="MobilePhone"    ;Expression={$usr.MobilePhone}},
                @{Name="Fax"            ;Expression={$usr.Fax}},
                @{Name="Pager"          ;Expression={$usr.Pager}},
                @{Name="Department"     ;Expression={$usr.Department}},
                @{Name="Office"         ;Expression={$usr.Office}},
                @{Name="Title"          ;Expression={$usr.Title}},
                @{Name="WebPage"        ;Expression={$usr.WebPage}},
                @{Name="Notes"          ;Expression={$usr.Notes}},
                @{Name="EmailAddresses" ;Expression={($_.EmailAddresses | ? {$_.PrefixString -ceq "smtp" -or "sip"} | % {$_.SmtpAddress}) -join ","}}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIP.csv -encoding ascii

Open in new window

Avatar of Guy Lidbetter
Guy Lidbetter
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi There K B,

The script is fine but you need to get a PS Session to O365 first, so try add this to the beginning of the script:

$OnlineCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $OnlineCred -Authentication Basic -AllowRedirection
Import-PSSession $Session

Open in new window


It will ask for the credential you use to manage the online mailboxes.

Regards

Guy
Avatar of K B

ASKER

Thank you for your reply!
Apologies if I wasn't more clear - I have a separate .PS1 that connects me with no problems.
both 365 and MsolService in one .PS1
I get all the data from the script I just do not get any email addresses (except the field "primarysmtpaddress")
Did this work on 2013?
Avatar of K B

ASKER

Yes it did.
Odd, its not working with my on premise 2013.
I'll have a look at getting the output required, in the interim, before Qlemo catches this and fixes it!
Avatar of K B

ASKER

HA!  He actually said that 365 is not his realm so he will not be participating in this question :-)
Just out of curiosity, what is the error you run into when running it against 2013?
Avatar of Qlemo
(... but Qlemo is watching you!)
Maybe there is a flaw in the script, but I cannot detect one. As said, it works with 2013 for us both.
Try this

Get-Mailbox -resultsize 3 |
  % {
    $usr = Get-User $_.SamAccountName
      
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{Name="City"           ;Expression={$usr.City}},
                @{Name="StateOrProvince";Expression={$usr.StateOrProvince}},
                @{Name="PostalCode"     ;Expression={$usr.PostalCode}},
                @{Name="CountryOrRegion";Expression={$usr.CountryOrRegion}},
                @{Name="Phone"          ;Expression={$usr.Phone}},
                @{Name="HomePhone"      ;Expression={$usr.HomePhone}},
                @{Name="MobilePhone"    ;Expression={$usr.MobilePhone}},
                @{Name="Fax"            ;Expression={$usr.Fax}},
                @{Name="Pager"          ;Expression={$usr.Pager}},
                @{Name="Department"     ;Expression={$usr.Department}},
                @{Name="Office"         ;Expression={$usr.Office}},
                @{Name="Title"          ;Expression={$usr.Title}},
                @{Name="WebPage"        ;Expression={$usr.WebPage}},
                @{Name="Notes"          ;Expression={$usr.Notes}},
            @{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq “smtp” -or "sip"} | ForEach-Object {$_.SmtpAddress}}}
             
  } | Out-File C:\SMTP_SIP.csv -encoding ascii
 ConvertTo-CSV -notype |
 % { $_ -replace '"'} |
 Out-File C:\SMTP_SIP.csv -encoding ascii
Avatar of K B

ASKER

Thank you for your reply Mks!

I received this when I ran your script:

cmdlet ConvertTo-Csv at command pipeline position 1
Supply values for the following parameters:
InputObject:
My bad, I left an extra line of code.

Get-Mailbox sharmamk |
  % {
    $usr = Get-User $_.SamAccountName
     
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{Name="City"           ;Expression={$usr.City}},
                @{Name="StateOrProvince";Expression={$usr.StateOrProvince}},
                @{Name="PostalCode"     ;Expression={$usr.PostalCode}},
                @{Name="CountryOrRegion";Expression={$usr.CountryOrRegion}},
                @{Name="Phone"          ;Expression={$usr.Phone}},
                @{Name="HomePhone"      ;Expression={$usr.HomePhone}},
                @{Name="MobilePhone"    ;Expression={$usr.MobilePhone}},
                @{Name="Fax"            ;Expression={$usr.Fax}},
                @{Name="Pager"          ;Expression={$usr.Pager}},
                @{Name="Department"     ;Expression={$usr.Department}},
                @{Name="Office"         ;Expression={$usr.Office}},
                @{Name="Title"          ;Expression={$usr.Title}},
                @{Name="WebPage"        ;Expression={$usr.WebPage}},
                @{Name="Notes"          ;Expression={$usr.Notes}},
            @{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq “smtp” -or "sip"} | ForEach-Object {$_.SmtpAddress}}}
             
  } | Export-csv C:\Out.csv -NoTypeinformation
Avatar of K B

ASKER

Thank you.
Same thing Mks... no information in the EmailAddresses field
Strange things happen, obviously. Could it be that the O365 accounts all have SIP. sip or SNMP prefixes (note the case)? Because my line line 22 should be
                @{Name="EmailAddresses" ;Expression={($_.EmailAddresses | ? {'snmp', 'sip' -contains $_.PrefixString} | % {$_.SmtpAddress}) -join ';'}}

Open in new window

But the (wrong) condition is from your original code, K B :D.
I've also changed the email address list delimiter to a semi-colon, as discussed in the prior thread.
Avatar of K B

ASKER

I think i know why this isn't working... Thanks to the Alan Byrne...
https://www.cogmotive.com/blog/office-365-tips/export-all-email-addresses-from-office-356

How do we incorporate this into the script:

Get-Recipient -ResultSize Unlimited | select DisplayName,RecipientType,EmailAddresses | Export-Csv email-recipients.csv
Avatar of K B

ASKER

Get-Recipient, that is.   with SIP and SMTP addresses represented. :-)
Avatar of K B

ASKER

Better yet, how do I get this one-liner to only provide us with SMTP addresses THEN SIP addresses in the last column(s) of each row ?
As it stands now, it also provides X500:addresses co-mingled with the SIP: and SMTP:and I don't wont those (co-mingled).

Get-Recipient -ResultSize Unlimited | select DisplayName,RecipientType,EmailAddresses | Export-Csv email-recipients.csv

Open in new window


like this:
User generated image
Thank you!
What should the exact output be?
You also took notice that there is no prefix?
I didn't receive an error, just no output on the email addresses, well actually just a bunch 'o commas (,,,,,,,,), everything else works fine.

As  a side note, you do not have to use get-recipient for the same output... just modify the last line of the select section that gets the emailaddresses and remove the jiggery pokery... i.e.

@{Name="EmailAddresses" ;Expression={($_.EmailAddresses | ? {$_.PrefixString -ceq "smtp" -or "sip"} | % {$_.SmtpAddress}) -join ","}}

Open in new window

Becomes
@{$_.EmailAddresses}

Open in new window


That DOES work for me... but gets the x500 as well. So I think on my side there is an issue with that piece of the script formatting.

If you want additional columns for each email address, that will take a bit more work, but I can get that done.
If I understood correctly, you want to have SMTP first, then SIP. What about those addresses without prefix?
And do you want to keep the prefix? (Your original code did not keep it).

I'll assume you want to have list of SMTP and SIP, without prefix, and just in alphabetical order:
@{Name="EmailAddresses"
  Expression={
    ($_.EmailAddresses |
      ? {$_.PrefixString -ne 'X500'} |
      sort AddressString |
      Select -Expand AddressString
    ) -join ';'
}}

Open in new window

And of course my original script only works for SMTP addresses :/, because it extracts SmtpAddress - which exists only if it is SMTP (sounds straight-forward, how could I have missed that???).
Avatar of K B

ASKER

Thank you Qlemo!
I am trying to incorporate your latest update

@{Name="EmailAddresses"
  Expression={
    ($_.EmailAddresses |
      ? {$_.PrefixString -ne 'X500'} |
      sort AddressString |
      Select -Expand AddressString
    ) -join ';'
}}

Open in new window


with this (is this what you envisioned?)

Get-Recipient -ResultSize Unlimited | select DisplayName,RecipientType,EmailAddresses | Export-Csv email-recipients.csv

Open in new window


Is there any way you can help me push them together please sir?
K.B.
Guy told that you do not need Get-Reciepient, and I should agree with him. So let's go back to the original code:
Get-Mailbox -Resultsize 3 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{Name="City"           ;Expression={$usr.City}},
                @{Name="StateOrProvince";Expression={$usr.StateOrProvince}},
                @{Name="PostalCode"     ;Expression={$usr.PostalCode}},
                @{Name="CountryOrRegion";Expression={$usr.CountryOrRegion}},
                @{Name="Phone"          ;Expression={$usr.Phone}},
                @{Name="HomePhone"      ;Expression={$usr.HomePhone}},
                @{Name="MobilePhone"    ;Expression={$usr.MobilePhone}},
                @{Name="Fax"            ;Expression={$usr.Fax}},
                @{Name="Pager"          ;Expression={$usr.Pager}},
                @{Name="Department"     ;Expression={$usr.Department}},
                @{Name="Office"         ;Expression={$usr.Office}},
                @{Name="Title"          ;Expression={$usr.Title}},
                @{Name="WebPage"        ;Expression={$usr.WebPage}},
                @{Name="Notes"          ;Expression={$usr.Notes}},
                @{Name="EmailAddresses"
                  Expression={
                    ($_.EmailAddresses |
                      ? {$_.PrefixString -ne 'X500'} |
                      sort AddressString |
                      Select -Expand AddressString
                    ) -join ';'
                }}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIP.csv -encoding ascii

Open in new window

Avatar of K B

ASKER

That yielded blank emailaddresses column again.

I noticed I get results with

get-recipient | select emailaddresses
get-mailbox | select emailaddresses

Open in new window


but not

get-user | select emailaddresses

Open in new window


but I am not sure how to incorporate that into the script.
Avatar of K B

ASKER

However Get-Recipient is much more thorough.  Get-Mailbox yields half the rows of data in this tenant as Get-Recipient.
Hi K B, you are right there, the emailaddresses property is not retrieved by the get-user cmdlet.

However, in Qlemo's code, its called by the get-mailbox not the get-user.
Avatar of K B

ASKER

Unfortunately it doesn't yield any data for that column.  It appears Get-Mailbox is not the way to go anyway.  Get-Recipient gets any recipient at all.
I am trying to thin it out so we can get it down to just the emailaddresses policy so maybe we can use this going forward?

Get-Mailbox -Resultsize 3 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="EmailAddresses"
                  Expression={
                    ($_.EmailAddresses |
                      ? {$_.PrefixString -ne 'X500'} |
                      sort AddressString |
                      Select -Expand AddressString
                    ) -join ';'
                }}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIP.csv -encoding ascii

Open in new window

Avatar of K B

ASKER

Okay this gives all the data but without your magic of pulling out the prefixes and not including X500:

Get-Recipient -Resultsize 30 | 
  % {
    $usr = Get-Recipient $_.SamAccountName
    $_ | Select-Object DisplayName, PrimarySmtpAddress, EmailAddresses                
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIPRR.csv -encoding ascii

Open in new window

You should be able to just replace Get-Mailbox with Get-Recipient, leaving anything else as-is.
If you want the original Out-put with just a list of email addresses as you've done in that bit of code, just use this...

Get-recipient -resultsize 3 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{Name="City"           ;Expression={$usr.City}},
                @{Name="StateOrProvince";Expression={$usr.StateOrProvince}},
                @{Name="PostalCode"     ;Expression={$usr.PostalCode}},
                @{Name="CountryOrRegion";Expression={$usr.CountryOrRegion}},
                @{Name="Phone"          ;Expression={$usr.Phone}},
                @{Name="HomePhone"      ;Expression={$usr.HomePhone}},
                @{Name="MobilePhone"    ;Expression={$usr.MobilePhone}},
                @{Name="Fax"            ;Expression={$usr.Fax}},
                @{Name="Pager"          ;Expression={$usr.Pager}},
                @{Name="Department"     ;Expression={$usr.Department}},
                @{Name="Office"         ;Expression={$usr.Office}},
                @{Name="Title"          ;Expression={$usr.Title}},
                @{Name="WebPage"        ;Expression={$usr.WebPage}},
                @{Name="Notes"          ;Expression={$usr.Notes}},
                @{Name="EmailAddressess";Expression={$_.EmailAddresses}}
  } |  Export-CSV C:\temp\SMTP_SIP.csv -nti -encoding ascii

Open in new window


I can work on the formatting you'd like off line... but will need some effort.
Next test steps:
Get-Recipient -Resultsize 30 | 
  % {
    $_ | Select-Object DisplayName, PrimarySmtpAddress, @{n='EmailAddresses'; e={$_.EmailAddresses}}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIPRR.csv -encoding ascii

Open in new window

Get-Recipient -Resultsize 30 | 
  % {
    $_ | Select-Object DisplayName, PrimarySmtpAddress, @{n='EmailAddresses'; e={$_.EmailAddresses | Select -Expand AddressString}}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIPRR.csv -encoding ascii

Open in new window

Avatar of K B

ASKER

First script gave emailaddresses column filled but without commas in between
Second script no data in emailaddresses column
I'm flabbergasted. What does
 Get-Recipient -Resultsize 30 | Select -Expand EmailAddresses | gm *addr*, *prefix*
show as object types and properties?
Avatar of K B

ASKER

Here is the information.. no *addr* or *prefix* in there. so here is all
GM.xlsx
What the heck? That is a [String], but should be something like ProxyAddress, SMTPAddress or the like. So the type gets lost, and we have no proper means to dissect single addresses but to perform text processing.
Avatar of K B

ASKER

 Get-Recipient -resultsize 3 |select Name, DisplayName, Alias, PrimarySmtpAddress, @{Name="EmailAddresses";Expression={($_.EmailAddresses)}}| Export-Csv hey.csv

Open in new window


this gives SMTP and SIP and X500 with no commas inbetween but on same row.  Progress?
Interesting interesting interesting!!!!

Remember I mentioned this was not working on my 2013 on Premise??

WELL!!! It DOES work... but only when run locally on an exchange server, or a server with Management tools installed. it does NOT work on a called session...

Consider this (run against my own mailbox):

On Exchange Server:
PS C:\Windows\system32> Get-Recipient glidbetter | Select -Expand EmailAddresses | gm *addr*, *prefix*


   TypeName: Microsoft.Exchange.Data.SmtpProxyAddress

Name               MemberType Definition
----               ---------- ----------
AddressString      Property   string AddressString {get;}
IsPrimaryAddress   Property   bool IsPrimaryAddress {get;}
ProxyAddressString Property   string ProxyAddressString {get;}
SmtpAddress        Property   string SmtpAddress {get;}
Prefix             Property   Microsoft.Exchange.Data.ProxyAddressPrefix Prefix {get;}
PrefixString       Property   string PrefixString {get;}


   TypeName: Microsoft.Exchange.Data.X400ProxyAddress

Name               MemberType Definition
----               ---------- ----------
AddressString      Property   string AddressString {get;}
IsPrimaryAddress   Property   bool IsPrimaryAddress {get;}
ProxyAddressString Property   string ProxyAddressString {get;}
Prefix             Property   Microsoft.Exchange.Data.ProxyAddressPrefix Prefix {get;}
PrefixString       Property   string PrefixString {get;}

Open in new window


On my PC with a PSSession to Exchange:
PS C:\Windows\system32>  Get-Recipient glidbetter | Select -Expand EmailAddresses | gm *addr*, *prefix*
PS C:\Windows\system32>

Open in new window

No Result?!?!?!

So I imagine the code is perfect... it has something to do with the way Powershell is managing the sessions, it must be losing something in translation...
This is indeed related to remote sessions. When importing the session, proxy functions are created, which use marshalled data types - which get deserialized, that is loose all of their special properties.

So, here we go. That should work for the Exchange Shell and in a remote session. I'm stripping off the prefix; if you would like to keep them, remove the -replace 'SMTP:|SIP:'.
Get-Recipient -Resultsize 3 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, UserPrincipalName, ForwardingAddress, Alias, LegacyExchangeDN, RecipientType,RecipientTypeDetails,PrimarySmtpAddress,
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{Name="City"           ;Expression={$usr.City}},
                @{Name="StateOrProvince";Expression={$usr.StateOrProvince}},
                @{Name="PostalCode"     ;Expression={$usr.PostalCode}},
                @{Name="CountryOrRegion";Expression={$usr.CountryOrRegion}},
                @{Name="Phone"          ;Expression={$usr.Phone}},
                @{Name="HomePhone"      ;Expression={$usr.HomePhone}},
                @{Name="MobilePhone"    ;Expression={$usr.MobilePhone}},
                @{Name="Fax"            ;Expression={$usr.Fax}},
                @{Name="Pager"          ;Expression={$usr.Pager}},
                @{Name="Department"     ;Expression={$usr.Department}},
                @{Name="Office"         ;Expression={$usr.Office}},
                @{Name="Title"          ;Expression={$usr.Title}},
                @{Name="WebPage"        ;Expression={$usr.WebPage}},
                @{Name="Notes"          ;Expression={$usr.Notes}},
                @{Name="EmailAddresses"
                  Expression={
                    $(if ($_.EmailAddresses[0].AddressString)
                      {
                        $_.EmailAddresses |
                         ? {$_.PrefixString -ne 'X500'} |
                         Select -Expand ProxyAddressString
                      } else {
                        $_.EMailAddresses | 
                        ? {$_ -notlike 'X500:*'}
                      }
                    ) -replace 'SMTP:|SIP:' -join ';'
                }}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIP.csv -encoding ascii

Open in new window

Avatar of K B

ASKER

Amazing!  Thank you!  I am going to open a separate question about exporting the same for Distribution Groups and Contacts.  

One question if I may please.  I modified the script ... (otherwise I got this error often
"The operation couldn't be performed because object 'XYZ WestOffice Distribution Group' couldn't be found on 'XXXXXXDC001.namprd0X.prod.outlook.com'.")
...to filter for only mailuser etc... and I got all the attributes that are included in the script except two:

1. LegacyExchangeDN
2. ForwardingAddress

It seems these attribute require Get-Mailbox.  Is there anyway to include attributes from Get-Mailbox in this same script?  Here is the modified script and I thank you very much again!!

Get-Recipient -resultsize 20 | Where-Object {($_.RecipientType -eq 'UserMailbox') -or ($_.RecipientType -eq 'MailUser') -or ($_.RecipientType -eq 'User')}| 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, Alias, RecipientType, RecipientTypeDetails, PrimarySmtpAddress,
                @{Name="UserPrincipalName"      ;Expression={$usr.UserPrincipalName}},
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{Name="City"           ;Expression={$usr.City}},
                @{Name="StateOrProvince";Expression={$usr.StateOrProvince}},
                @{Name="PostalCode"     ;Expression={$usr.PostalCode}},
                @{Name="CountryOrRegion";Expression={$usr.CountryOrRegion}},
                @{Name="Phone"          ;Expression={$usr.Phone}},
                @{Name="HomePhone"      ;Expression={$usr.HomePhone}},
                @{Name="MobilePhone"    ;Expression={$usr.MobilePhone}},
                @{Name="Fax"            ;Expression={$usr.Fax}},
                @{Name="Pager"          ;Expression={$usr.Pager}},
                @{Name="Department"     ;Expression={$usr.Department}},
                @{Name="Office"         ;Expression={$usr.Office}},
                @{Name="Title"          ;Expression={$usr.Title}},
                @{Name="WebPage"        ;Expression={$usr.WebPage}},
                @{Name="Notes"          ;Expression={$usr.Notes}},
                @{Name="EmailAddresses"
                  Expression={
                    $(if ($_.EmailAddresses[0].AddressString)
                      {
                        $_.EmailAddresses |
                         ? {$_.PrefixString -ne 'X500'} |
                         Select -Expand ProxyAddressString
                      } else {
                        $_.EMailAddresses | 
                        ? {$_ -notlike 'X500:*'}
                      }
                    ) -replace 'SMTP:|SIP:' -join ','
                }}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIP.csv -Encoding ascii

Open in new window

Done!

Get-Recipient -resultsize 20 | Where-Object {($_.RecipientType -eq 'UserMailbox') -or ($_.RecipientType -eq 'MailUser') -or ($_.RecipientType -eq 'User')}| 
  % {
    $usr = Get-User $_.SamAccountName
	$MailUsr =  Get-Mailbox $_.SamAccountName
    $_ | Select-Object DisplayName, Alias, RecipientType, RecipientTypeDetails, PrimarySmtpAddress,
                @{Name="UserPrincipalName"      ;Expression={$usr.UserPrincipalName}},
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{Name="City"           ;Expression={$usr.City}},
                @{Name="StateOrProvince";Expression={$usr.StateOrProvince}},
                @{Name="PostalCode"     ;Expression={$usr.PostalCode}},
                @{Name="CountryOrRegion";Expression={$usr.CountryOrRegion}},
                @{Name="Phone"          ;Expression={$usr.Phone}},
                @{Name="HomePhone"      ;Expression={$usr.HomePhone}},
                @{Name="MobilePhone"    ;Expression={$usr.MobilePhone}},
                @{Name="Fax"            ;Expression={$usr.Fax}},
                @{Name="Pager"          ;Expression={$usr.Pager}},
                @{Name="Department"     ;Expression={$usr.Department}},
                @{Name="Office"         ;Expression={$usr.Office}},
                @{Name="Title"          ;Expression={$usr.Title}},
                @{Name="WebPage"        ;Expression={$usr.WebPage}},
                @{Name="Notes"          ;Expression={$usr.Notes}},
				@{Name="LegacyExchangeDN"	;Expression={$MailUsr.legacyExchangeDn}},
				@{Name="ForwardingAddress"	;Expression={$MailUsr.ForwardingAddress}},
                @{Name="EmailAddresses"
                  Expression={
                    $(if ($_.EmailAddresses[0].AddressString)
                      {
                        $_.EmailAddresses |
                         ? {$_.PrefixString -ne 'X500'} |
                         Select -Expand ProxyAddressString
                      } else {
                        $_.EMailAddresses | 
                        ? {$_ -notlike 'X500:*'}
                      }
                    ) -replace 'SMTP:|SIP:' -join ','
                }}
  } 

Open in new window

Avatar of K B

ASKER

Thank for the reply Guy!
That seems to give the error:

"The operation couldn't be performed because object 'XYZ 012012' couldn't be found on 'XXXXXXDC001.namprd0X.prod.outlook.com'."

It also seems to shift data to other columns and leaves some blank rows. Any idea why?
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of K B

ASKER

hmm let me give it a try
Keep in mind, the difference I mentioned earlier. Get-mailbox will return only mailbox USERS. Get-recipient will return distribution groups, rooms, contacts... Anything with an email.

Get-mailbox also returns a lot more information on the objects as well.

Also, the reason you're getting the error is its trying to get mailbox on something get-recipient is returning. It may be trying to get a mailbox for a distribution or similar which it can't return.
Avatar of K B

ASKER

Final script

Get-Mailbox -resultsize 30 | 
  % {
    $usr = Get-User $_.SamAccountName
    $_ | Select-Object DisplayName, Alias, RecipientType, RecipientTypeDetails, PrimarySmtpAddress, ForwardingAddress, LegacyExchangeDN,
                @{Name="UserPrincipalName"      ;Expression={$usr.UserPrincipalName}},
                @{Name="FirstName"      ;Expression={$usr.FirstName}},
                @{Name="LastName"       ;Expression={$usr.LastName }},
                @{Name="StreetAddress"  ;Expression={$usr.StreetAddress}},
                @{Name="City"           ;Expression={$usr.City}},
                @{Name="StateOrProvince";Expression={$usr.StateOrProvince}},
                @{Name="PostalCode"     ;Expression={$usr.PostalCode}},
                @{Name="CountryOrRegion";Expression={$usr.CountryOrRegion}},
                @{Name="Phone"          ;Expression={$usr.Phone}},
                @{Name="HomePhone"      ;Expression={$usr.HomePhone}},
                @{Name="MobilePhone"    ;Expression={$usr.MobilePhone}},
                @{Name="Fax"            ;Expression={$usr.Fax}},
                @{Name="Pager"          ;Expression={$usr.Pager}},
                @{Name="Department"     ;Expression={$usr.Department}},
                @{Name="Office"         ;Expression={$usr.Office}},
                @{Name="Title"          ;Expression={$usr.Title}},
                @{Name="WebPage"        ;Expression={$usr.WebPage}},
                @{Name="Notes"          ;Expression={$usr.Notes}},
                @{Name="EmailAddresses"
                  Expression={
                    $(if ($_.EmailAddresses[0].AddressString)
                      {
                        $_.EmailAddresses |
                         ? {$_.PrefixString -ne 'X500'} |
                         Select -Expand ProxyAddressString
                      } else {
                        $_.EMailAddresses | 
                        ? {$_ -notlike 'X500:*'}
                      }
                    ) -replace 'SMTP:|SIP:' -join ','
                }}
  } |
  ConvertTo-CSV -notype |
  % { $_ -replace '"'} |
  Out-File C:\scripts\SMTP_SIP.csv -Encoding ascii

Open in new window