I wonder if anyone here can assist in integrating the below function to Get Last Logon Time in Exchange Online mailbox with the simple script I have put together?
The main script that is already working to collect mailbox size statistics then output to Out-Gridview:
Code: Select all
Write-Host "Gathering Stats, Please Wait.."
$Mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Select-Object UserPrincipalName, identity, ArchiveStatus, RecipientTypeDetails, SKUAssigned
$CountTotalMbx = $Mailboxes.Count
$CountTotalMbxSize = 0
$MailboxSizes = @()
foreach ($Mailbox in $Mailboxes) {
$ObjProperties = New-Object PSObject
$MailboxStats = Get-MailboxStatistics $Mailbox.UserPrincipalname | Select-Object LastLogonTime, TotalItemSize, ItemCount
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "UserPrincipalName" -Value $Mailbox.UserPrincipalName
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Last Logged In" -Value $MailboxStats.LastLogonTime
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Size" -Value $MailboxStats.TotalItemSize
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Size (MB)" -Value ([math]::Round(($MailboxStats.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",", "") / 1MB), 0))
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Item Count" -Value $MailboxStats.ItemCount
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Type Details" -Value $Mailbox.RecipientTypeDetails
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox License assigned" -Value $Mailbox.SKUAssigned
if ($Mailbox.ArchiveStatus -eq "Active") {
$ArchiveStats = Get-MailboxStatistics $Mailbox.UserPrincipalname -Archive | Select-Object TotalItemSize, ItemCount
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value $ArchiveStats.TotalItemSize
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value $ArchiveStats.ItemCount
}
else {
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value "No Archive"
Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value "No Archive"
}
$CountTotalMbxSize += [math]::Round(($MailboxStats.TotalItemSize -replace "(.*\()|,| [a-z]*\)", "") / 1GB, 2) + [math]::Round(($ArchiveStats.TotalItemSize -replace "(.*\()|,| [a-z]*\)", "") / 1GB, 2)
$MailboxSizes += $ObjProperties
}
$MailboxSizes | Out-GridView -Title "There are $($CountTotalMbx) mailboxes with total usage $($CountTotalMbxSize) GB of both Mailbox and Archive in size"
using this function Get_LastLogonTime to return the correct Last logon time for the mailboxes:
Code: Select all
Function Get_LastLogonTime {
$MailboxStatistics = Get-MailboxStatistics -Identity $upn
$LastActionTime = $MailboxStatistics.LastUserActionTime
$LastActionTimeUpdatedOn = $MailboxStatistics.LastUserActionUpdateTime
Write-Progress -Activity "`n Processed mailbox count: $MBUserCount "`n" Currently Processing: $DisplayName"
#Retrieve lastlogon time and then calculate Inactive days
if ($LastActionTime -eq $null) {
$LastActionTime = "Never Logged In"
$InactiveDaysOfUser = "-"
}
else {
$InactiveDaysOfUser = (New-TimeSpan -Start $LastActionTime).Days
#Convert Last Action Time to Friendly Time
if ($friendlyTime.IsPresent) {
$FriendlyLastActionTime = ConvertTo-HumanDate ($LastActionTime)
$friendlyLastActionTime = "(" + $FriendlyLastActionTime + ")"
$LastActionTime = "$LastActionTime $FriendlyLastActionTime"
}
}
#Convert Last Action Time Update On to Friendly Time
if (($friendlyTime.IsPresent) -and ($LastActionTimeUpdatedOn -ne $null)) {
$FriendlyLastActionTimeUpdatedOn = ConvertTo-HumanDate ($LastActionTimeUpdatedOn)
$FriendlyLastActionTimeUpdatedOn = "(" + $FriendlyLastActionTimeUpdatedOn + ")"
$LastActionTimeUpdatedOn = "$LastActionTimeUpdatedOn $FriendlyLastActionTimeUpdatedOn"
}
elseif ($LastActionTimeUpdatedOn -eq $null) {
$LastActionTimeUpdatedOn = "-"
}
#Inactive days based filter
if ($InactiveDaysOfUser -ne "-") {
if (($InactiveDays -ne "") -and ([int]$InactiveDays -gt $InactiveDaysOfUser)) {
return
}
}
#Filter result based on user mailbox
if (($UserMailboxOnly.IsPresent) -and ($MBType -ne "UserMailbox")) {
return
}
#Never Logged In user
if (($ReturnNeverLoggedInMBOnly.IsPresent) -and ($LastActionTime -ne "Never Logged In")) {
return
}
#Export result to CSV file
$Result = @{'LastUserActionTime' = $LastActionTime; 'LastActionTimeUpdatedOn' = $LastActionTimeUpdatedOn; 'CreationTime' = $CreationTime; 'InactiveDays' = $InactiveDaysOfUser; 'MailboxType' = $MBType }
$Output = New-Object PSObject -Property $Result
$Output | Select-Object UserPrincipalName, DisplayName, LastUserActionTime, LastActionTimeUpdatedOn, InactiveDays, CreationTime, MailboxType, AssignedLicenses, Roles | Export-Csv -Path $ExportCSV -Notype -Append
}