Page 1 of 1

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

Posted: Wed Jan 09, 2019 6:48 pm
by ITEngineer
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,

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

Posted: Wed Jan 09, 2019 7:28 pm
by jvierra
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.

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

Posted: Wed Jan 09, 2019 7:45 pm
by ITEngineer
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?

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

Posted: Wed Jan 09, 2019 8:12 pm
by jvierra
Connect to both services first but in the reverse order.

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

Posted: Wed Jan 09, 2019 10:02 pm
by ITEngineer
jvierra wrote: ↑Wed Jan 09, 2019 8:12 pm Connect to both services first but in the reverse order.
Yes, you are right, it works now :-)

Thanks

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

Posted: Wed Jan 09, 2019 10:19 pm
by jvierra
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.