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

Ask your 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.
This topic is 5 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post 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,
Last edited by ITEngineer on Wed Jan 09, 2019 7:49 pm, edited 1 time in total.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post 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?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

Connect to both services first but in the reverse order.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post 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
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
This topic is 5 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked