Powershell for exporting Mailbox on Office 365 and OnPremise to each respective .CSV as the result?

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

Powershell for exporting Mailbox on Office 365 and OnPremise to each respective .CSV as the result?

Post by ITEngineer » Wed Jan 09, 2019 7:48 pm

Hi All,

I wanted to get the exported.CSV list of the figure of mailboxes resides on-premise and Office 365 environments in each own.CSV

Here's what I've come up with, but not working:

Code: Select all

Try {
    $UserCredential = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
    Import-PSSession $Session -Prefix O365 # Prefix to differentiate O365 cmdlets
    
    # Office 365 Mailbox list export
    $MailboxList = @()
    $MailboxList += Get-O365Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox, SharedMailbox | 
                        Get-O365MailboxStatistics | 
                        Sort-Object lastlogontime -Descending | 
                        Select-Object DisplayName, LastLogonTime | 
                        Export-Csv -Path C:\TEMP\Office365-Mailboxes.CSV

    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://PRDMAIL01-VM/PowerShell/ -Authentication Kerberos
    Import-PSSession $Session -AllowClobber

    # Exchange Server 2013 OnPremise Mailbox list export, The last active mailbox login in the past 30 days determine if it is active mailbox or not
    $MailboxList = @()
    $MailboxList += Get-Mailbox -ResultSize Unlimited –RecipientTypeDetails UserMailbox, SharedMailbox;
    $MailboxList | Where-Object {(Get-MailboxStatistics $_.Identity).LastLogonTime -gt (Get-Date).AddDays(-30)} | 
                    Sort-Object -Property @{e = {( Get-MailboxStatistics $_.Identity).LastLogonTime}} -Descending | 
                    Select-Object DisplayName, @{n = "LastLogonTime"; e = {(Get-MailboxStatistics $_.Identity).LastLogonTime}} | 
                    Export-Csv -Path C:\TEMP\OnPremise-Mailboxes.CSV -NoTypeInformation
}
Catch {
    Throw
}
Finally {
    If ($Session) {
        Remove-PSSession -Session $Session
    }
}
This is the error message:
WARNING: The names of some imported commands from the module 'tmp_3rgd0wvj.ptf' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.

ModuleType Version Name
ExportedCommands ---------- ------- ---- ---------------- Script 1.0 tmp_3rgd0wvj.ptf {Add-O365AvailabilityAddressSpace, Add-O365DistributionGroupMember, Add-O365MailboxFolderPermission, Add-O365MailboxLocation...} The term 'Get-MailboxStatistics' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
+ ... ypeDetails UserMailbox, SharedMailbox | Get-MailboxStatistics | Sort- ... + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Get-MailboxStatistics:String) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : CommandNotFoundException
Any help would be greatly appreciated.

Thanks,
Last edited by ITEngineer on Wed Jan 09, 2019 8:49 pm, edited 1 time in total.
/* IT Engineer */

User avatar
jvierra
Posts: 13290
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell for exporting Mailbox on Office 365 and OnPremise to each respective .CSV as the result?

Post by jvierra » Wed Jan 09, 2019 8:28 pm

That can be ignored. It is just a warning and depends, in part, on other modules you may have loaded. It can aslo happen if you are running on an older version of WMF. If on-premise Exchange then it may be an older or unpatched version of Exchange.

You will have to do some local troubleshooting. Some older modules for Exchange are no compatible with O365 Exchange.

You also didn't post which import command caused this error.

User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

Re: Powershell for exporting Mailbox on Office 365 and OnPremise to each respective .CSV as the result?

Post by ITEngineer » Wed Jan 09, 2019 8:45 pm

When I execute this piece of codes in my PowerShell ISE on the laptop, it works and I can get the .CSV file:

Code: Select all

    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://PRDMAIL01-VM/PowerShell/ -Authentication Kerberos
    Import-PSSession $Session -AllowClobber

    # Exchange Server 2013 OnPremise Mailbox list export, The last active mailbox login in the past 30 days determine if it is active mailbox or not
    $MailboxList = @()
    $MailboxList += Get-Mailbox -ResultSize Unlimited –RecipientTypeDetails UserMailbox, SharedMailbox;
    $MailboxList | Where-Object {(Get-MailboxStatistics $_.Identity).LastLogonTime -gt (Get-Date).AddDays(-30)} | 
                    Sort-Object -Property @{e = {( Get-MailboxStatistics $_.Identity).LastLogonTime}} -Descending | 
                    Select-Object DisplayName, @{n = "LastLogonTime"; e = {(Get-MailboxStatistics $_.Identity).LastLogonTime}} | 
                    Export-Csv -Path C:\TEMP\OnPremise-Mailboxes.CSV -NoTypeInformation
Note, for the Office 365 command, I have modified the Get-O365MailboxStatistics.

Code: Select all

    $UserCredential = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
    Import-PSSession $Session -Prefix O365 # Prefix to differentiate O365 cmdlets
    
    # Office 365 Mailbox list export
    $MailboxList = @()
    $MailboxList += Get-O365Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox, SharedMailbox | 
                        Get-O365MailboxStatistics | 
                        Sort-Object lastlogontime -Descending | 
                        Select-Object DisplayName, LastLogonTime | 
                        Export-Csv -Path C:\TEMP\Office365-Mailboxes.CSV
The Office365-Mailboxes.CSV is produced successfully.

However, when I combine it with the other piece of code for Office 365, it does not working?
/* IT Engineer */

User avatar
jvierra
Posts: 13290
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell for exporting Mailbox on Office 365 and OnPremise to each respective .CSV as the result?

Post by jvierra » Wed Jan 09, 2019 9:12 pm

Connect to both services first but in the reverse order.

User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

Re: Powershell for exporting Mailbox on Office 365 and OnPremise to each respective .CSV as the result?

Post by ITEngineer » Wed Jan 09, 2019 11:02 pm

jvierra wrote:
Wed Jan 09, 2019 9:12 pm
Connect to both services first but in the reverse order.
Yes, you are right, it works now :-)

Thanks
/* IT Engineer */

User avatar
jvierra
Posts: 13290
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Powershell for exporting Mailbox on Office 365 and OnPremise to each respective .CSV as the result?

Post by jvierra » Wed Jan 09, 2019 11:19 pm

Great. I ran across that about two years ago with another module and the O365 module. Not quite sure why but I suspect upgrading your Exchange might fix it.

Locked