diff --git a/Office 365 User Last Activity Time Report/LastUserActivityTimeReport.ps1 b/Office 365 User Last Activity Time Report/LastUserActivityTimeReport.ps1 new file mode 100644 index 0000000..014ee08 --- /dev/null +++ b/Office 365 User Last Activity Time Report/LastUserActivityTimeReport.ps1 @@ -0,0 +1,283 @@ +#Accept input parameter +Param +( + [Parameter(Mandatory = $false)] + [string]$MBNamesFile, + [int]$InactiveDays, + [switch]$UserMailboxOnly, + [switch]$LicensedUserOnly, + [switch]$ReturnNeverLoggedInMBOnly, + [string]$UserName, + [string]$Password, + [switch]$FriendlyTime, + [switch]$MFA + +) + +Function Get_LastLogonTime +{ + $MailboxStatistics=Get-MailboxStatistics -Identity $upn + $LastActionTime=$MailboxStatistics.LastUserActionTime + $LastActionTimeUpdatedOn=$MailboxStatistics.LastUserActionUpdateTime + $RolesAssigned="" + 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="-" + } + + #Get licenses assigned to mailboxes + $User=(Get-MsolUser -UserPrincipalName $upn) + $Licenses=$User.Licenses.AccountSkuId + $AssignedLicense="" + $Count=0 + + #Convert license plan to friendly name + foreach($License in $Licenses) + { + $Count++ + $LicenseItem= $License -Split ":" | Select-Object -Last 1 + $EasyName=$FriendlyNameHash[$LicenseItem] + if(!($EasyName)) + {$NamePrint=$LicenseItem} + else + {$NamePrint=$EasyName} + $AssignedLicense=$AssignedLicense+$NamePrint + if($count -lt $licenses.count) + { + $AssignedLicense=$AssignedLicense+"," + } + } + if($Licenses.count -eq 0) + { + $AssignedLicense="No License Assigned" + } + + #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 + } + + #Filter result based on license status + if(($LicensedUserOnly.IsPresent) -and ($AssignedLicense -eq "No License Assigned")) + { + return + } + + #Get roles assigned to user + $Roles=(Get-MsolUserRole -UserPrincipalName $upn).Name + if($Roles.count -eq 0) + { + $RolesAssigned="No roles" + } + else + { + foreach($Role in $Roles) + { + $RolesAssigned=$RolesAssigned+$Role + if($Roles.indexof($role) -lt (($Roles.count)-1)) + { + $RolesAssigned=$RolesAssigned+"," + } + } + } + + #Export result to CSV file + $Result=@{'UserPrincipalName'=$upn;'DisplayName'=$DisplayName;'LastUserActionTime'=$LastActionTime;'LastActionTimeUpdatedOn'=$LastActionTimeUpdatedOn;'CreationTime'=$CreationTime;'InactiveDays'=$InactiveDaysOfUser;'MailboxType'=$MBType; 'AssignedLicenses'=$AssignedLicense;'Roles'=$RolesAssigned} + $Output= New-Object PSObject -Property $Result + $Output | Select-Object UserPrincipalName,DisplayName,LastUserActionTime,LastActionTimeUpdatedOn,InactiveDays,CreationTime,MailboxType,AssignedLicenses,Roles | Export-Csv -Path $ExportCSV -Notype -Append +} + + +Function main() +{ + #Check for MSOnline module + $Modules=Get-Module -Name MSOnline -ListAvailable + if($Modules.count -eq 0) + { + Write-Host Please install MSOnline module using below command: `nInstall-Module MSOnline -ForegroundColor yellow + Exit + } + #Connect AzureAD and Exchange Online from PowerShell + Get-PSSession | Remove-PSSession + + #Get friendly name of license plan from external file + $FriendlyNameHash=Get-Content -Raw -Path .\LicenseFriendlyName.txt -ErrorAction Stop | ConvertFrom-StringData + + + #Set output file + $ExportCSV=".\LastAccessTimeReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" + + #Authentication using MFA + if($MFA.IsPresent) + { + $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1) + If ($MFAExchangeModule -eq $null) + { + Write-Host `nPlease install Exchange Online MFA Module. -ForegroundColor yellow + + Write-Host You can install module using below blog : `nLink `nOR you can install module directly by entering "Y"`n + $Confirm= Read-Host Are you sure you want to install module directly? [Y] Yes [N] No + if($Confirm -match "[yY]") + { + Write-Host Yes + Start-Process "iexplore.exe" "https://cmdletpswmodule.blob.core.windows.net/exopsmodule/Microsoft.Online.CSE.PSModule.Client.application" + } + else + { + Start-Process 'https://o365reports.com/2019/03/23/export-dynamic-distribution-group-members-to-csv/' + Exit + } + $Confirmation= Read-Host Have you installed Exchange Online MFA Module? [Y] Yes [N] No + if($Confirmation -match "[yY]") + { + $MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1) + If ($MFAExchangeModule -eq $null) + { + Write-Host Exchange Online MFA module is not available -ForegroundColor red + Exit + } + } + else + { + Write-Host Exchange Online PowerShell Module is required + Start-Process 'https://o365reports.com/2019/03/23/export-dynamic-distribution-group-members-to-csv/' + Exit + } + } + + #Importing Exchange MFA Module + . "$MFAExchangeModule" + Write-Host Enter credential in prompt to connect to Exchange Online + Connect-EXOPSSession -WarningAction SilentlyContinue + Write-Host Connected to Exchange Online + Write-Host `nEnter credential in prompt to connect to MSOnline + #Importing MSOnline Module + Connect-MsolService | Out-Null + Write-Host Connected to MSOnline `n`nReport generation in progress... + } + #Authentication using non-MFA + else + { + #Storing credential in script for scheduling purpose/ Passing credential as parameter + if(($UserName -ne "") -and ($Password -ne "")) + { + $SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force + $Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword + } + else + { + $Credential=Get-Credential -Credential $null + } + Connect-MsolService -Credential $credential + $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection + Import-PSSession $Session -CommandName Get-Mailbox,Get-MailboxStatistics -FormatTypeName * -AllowClobber | Out-Null + } + + #Friendly DateTime conversion + if($friendlyTime.IsPresent) + { + If(((Get-Module -Name PowerShellHumanizer -ListAvailable).Count) -eq 0) + { + Write-Host Installing PowerShellHumanizer for Friendly DateTime conversion + Install-Module -Name PowerShellHumanizer + } + } + + $Result="" + $Output=@() + $MBUserCount=0 + + #Check for input file + if([string]$MBNamesFile -ne "") + { + #We have an input file, read it into memory + $Mailboxes=@() + $Mailboxes=Import-Csv -Header "MBIdentity" $MBNamesFile + foreach($item in $Mailboxes) + { + $MBDetails=Get-Mailbox -Identity $item.MBIdentity + $upn=$MBDetails.UserPrincipalName + $CreationTime=$MBDetails.WhenCreated + $DisplayName=$MBDetails.DisplayName + $MBType=$MBDetails.RecipientTypeDetails + $MBUserCount++ + Get_LastLogonTime + } + } + + #Get all mailboxes from Office 365 + else + { + Write-Progress -Activity "Getting Mailbox details from Office 365..." -Status "Please wait." + Get-Mailbox -ResultSize Unlimited | Where{$_.DisplayName -notlike "Discovery Search Mailbox"} | ForEach-Object{ + $upn=$_.UserPrincipalName + $CreationTime=$_.WhenCreated + $DisplayName=$_.DisplayName + $MBType=$_.RecipientTypeDetails + $MBUserCount++ + Get_LastLogonTime + } + } + + #Open output file after execution + Write-Host `nScript executed successfully + if((Test-Path -Path $ExportCSV) -eq "True") + { + Write-Host "Detailed report available in: $ExportCSV" + $Prompt = New-Object -ComObject wscript.shell + $UserInput = $Prompt.popup("Do you want to open output file?",` + 0,"Open Output File",4) + If ($UserInput -eq 6) + { + Invoke-Item "$ExportCSV" + } + } + Else + { + Write-Host No mailbox found + } + #Clean up session + Get-PSSession | Remove-PSSession +} + . main \ No newline at end of file diff --git a/Office 365 User Last Activity Time Report/LicenseFriendlyName.txt b/Office 365 User Last Activity Time Report/LicenseFriendlyName.txt new file mode 100644 index 0000000..f1fdb7a --- /dev/null +++ b/Office 365 User Last Activity Time Report/LicenseFriendlyName.txt @@ -0,0 +1,123 @@ + + O365_BUSINESS_ESSENTIALS = Office 365 Business Essentials + O365_BUSINESS_PREMIUM = Office 365 Business Premium + DESKLESSPACK = Office 365 (Plan K1) + DESKLESSWOFFPACK = Office 365 (Plan K2) + LITEPACK = Office 365 (Plan P1) + EXCHANGESTANDARD = Office 365 Exchange Online Only + STANDARDPACK = Enterprise Plan E1 + STANDARDWOFFPACK = Office 365 (Plan E2) + ENTERPRISEPACK = Enterprise Plan E3 + ENTERPRISEPACKLRG = Enterprise Plan E3 + ENTERPRISEWITHSCAL = Enterprise Plan E4 + STANDARDPACK_STUDENT = Office 365 (Plan A1) for Students + STANDARDWOFFPACKPACK_STUDENT = Office 365 (Plan A2) for Students + ENTERPRISEPACK_STUDENT = Office 365 (Plan A3) for Students + ENTERPRISEWITHSCAL_STUDENT = Office 365 (Plan A4) for Students + STANDARDPACK_FACULTY = Office 365 (Plan A1) for Faculty + STANDARDWOFFPACKPACK_FACULTY = Office 365 (Plan A2) for Faculty + ENTERPRISEPACK_FACULTY = Office 365 (Plan A3) for Faculty + ENTERPRISEWITHSCAL_FACULTY = Office 365 (Plan A4) for Faculty + ENTERPRISEPACK_B_PILOT = Office 365 (Enterprise Preview) + STANDARD_B_PILOT = Office 365 (Small Business Preview) + VISIOCLIENT = Visio Pro Online + POWER_BI_ADDON = Office 365 Power BI Addon + POWER_BI_INDIVIDUAL_USE = Power BI Individual User + POWER_BI_STANDALONE = Power BI Stand Alone + POWER_BI_STANDARD = Power-BI Standard + PROJECTESSENTIALS = Project Lite + PROJECTCLIENT = Project Professional + PROJECTONLINE_PLAN_1 = Project Online + PROJECTONLINE_PLAN_2 = Project Online and PRO + ProjectPremium = Project Online Premium + ECAL_SERVICES = ECAL + EMS = Enterprise Mobility Suite + RIGHTSMANAGEMENT_ADHOC = Windows Azure Rights Management + MCOMEETADV = PSTN conferencing + SHAREPOINTSTORAGE = SharePoint storage + PLANNERSTANDALONE = Planner Standalone + CRMIUR = CMRIUR + BI_AZURE_P1 = Power BI Reporting and Analytics + INTUNE_A = Windows Intune Plan A + PROJECTWORKMANAGEMENT = Office 365 Planner Preview + ATP_ENTERPRISE = Exchange Online Advanced Threat Protection + EQUIVIO_ANALYTICS = Office 365 Advanced eDiscovery + AAD_BASIC = Azure Active Directory Basic + RMS_S_ENTERPRISE = Azure Active Directory Rights Management + AAD_PREMIUM = Azure Active Directory Premium + MFA_PREMIUM = Azure Multi-Factor Authentication + STANDARDPACK_GOV = Microsoft Office 365 (Plan G1) for Government + STANDARDWOFFPACK_GOV = Microsoft Office 365 (Plan G2) for Government + ENTERPRISEPACK_GOV = Microsoft Office 365 (Plan G3) for Government + ENTERPRISEWITHSCAL_GOV = Microsoft Office 365 (Plan G4) for Government + DESKLESSPACK_GOV = Microsoft Office 365 (Plan K1) for Government + ESKLESSWOFFPACK_GOV = Microsoft Office 365 (Plan K2) for Government + EXCHANGESTANDARD_GOV = Microsoft Office 365 Exchange Online (Plan 1) only for Government + EXCHANGEENTERPRISE_GOV = Microsoft Office 365 Exchange Online (Plan 2) only for Government + SHAREPOINTDESKLESS_GOV = SharePoint Online Kiosk + EXCHANGE_S_DESKLESS_GOV = Exchange Kiosk + RMS_S_ENTERPRISE_GOV = Windows Azure Active Directory Rights Management + OFFICESUBSCRIPTION_GOV = Office ProPlus + MCOSTANDARD_GOV = Lync Plan 2G + SHAREPOINTWAC_GOV = Office Online for Government + SHAREPOINTENTERPRISE_GOV = SharePoint Plan 2G + EXCHANGE_S_ENTERPRISE_GOV = Exchange Plan 2G + EXCHANGE_S_ARCHIVE_ADDON_GOV = Exchange Online Archiving + EXCHANGE_S_DESKLESS = Exchange Online Kiosk + SHAREPOINTDESKLESS = SharePoint Online Kiosk + SHAREPOINTWAC = Office Online + YAMMER_ENTERPRISE = Yammer for the Starship Enterprise + EXCHANGE_L_STANDARD = Exchange Online (Plan 1) + MCOLITE = Lync Online (Plan 1) + SHAREPOINTLITE = SharePoint Online (Plan 1) + OFFICE_PRO_PLUS_SUBSCRIPTION_SMBIZ = Office ProPlus + EXCHANGE_S_STANDARD_MIDMARKET = Exchange Online (Plan 1) + MCOSTANDARD_MIDMARKET = Lync Online (Plan 1) + SHAREPOINTENTERPRISE_MIDMARKET = SharePoint Online (Plan 1) + OFFICESUBSCRIPTION = Office ProPlus + YAMMER_MIDSIZE = Yammer + DYN365_ENTERPRISE_PLAN1 = Dynamics 365 Customer Engagement Plan Enterprise Edition + ENTERPRISEPREMIUM_NOPSTNCONF = Enterprise E5 (without Audio Conferencing) + ENTERPRISEPREMIUM = Enterprise E5 (with Audio Conferencing) + MCOSTANDARD = Skype for Business Online Standalone Plan 2 + PROJECT_MADEIRA_PREVIEW_IW_SKU = Dynamics 365 for Financials for IWs + STANDARDWOFFPACK_IW_STUDENT = Office 365 Education for Students + STANDARDWOFFPACK_IW_FACULTY = Office 365 Education for Faculty + EOP_ENTERPRISE_FACULTY = Exchange Online Protection for Faculty + EXCHANGESTANDARD_STUDENT = Exchange Online (Plan 1) for Students + OFFICESUBSCRIPTION_STUDENT = Office ProPlus Student Benefit + STANDARDWOFFPACK_FACULTY = Office 365 Education E1 for Faculty + STANDARDWOFFPACK_STUDENT = Microsoft Office 365 (Plan A2) for Students + DYN365_FINANCIALS_BUSINESS_SKU = Dynamics 365 for Financials Business Edition + DYN365_FINANCIALS_TEAM_MEMBERS_SKU = Dynamics 365 for Team Members Business Edition + FLOW_FREE = Microsoft Flow Free + POWER_BI_PRO = Power BI Pro + O365_BUSINESS = Office 365 Business + DYN365_ENTERPRISE_SALES = Dynamics Office 365 Enterprise Sales + RIGHTSMANAGEMENT = Rights Management + PROJECTPROFESSIONAL = Project Professional + VISIOONLINE_PLAN1 = Visio Online Plan 1 + EXCHANGEENTERPRISE = Exchange Online Plan 2 + DYN365_ENTERPRISE_P1_IW = Dynamics 365 P1 Trial for Information Workers + DYN365_ENTERPRISE_TEAM_MEMBERS = Dynamics 365 For Team Members Enterprise Edition + CRMSTANDARD = Microsoft Dynamics CRM Online Professional + EXCHANGEARCHIVE_ADDON = Exchange Online Archiving For Exchange Online + EXCHANGEDESKLESS = Exchange Online Kiosk + SPZA_IW = App Connect + WINDOWS_STORE = Windows Store for Business + MCOEV = Microsoft Phone System + VIDEO_INTEROP = Polycom Skype Meeting Video Interop for Skype for Business + SPE_E5 = Microsoft 365 E5 + SPE_E3 = Microsoft 365 E3 + ATA = Advanced Threat Analytics + MCOPSTN2 = Domestic and International Calling Plan + FLOW_P1 = Microsoft Flow Plan 1 + FLOW_P2 = Microsoft Flow Plan 2 + DeveloperPack = OFFICE 365 ENTERPRISE E3 DEVELOPER +EMSPremium = ENTERPRISE MOBILITY + SECURITY E5 +RightsManagemnt =AZURE INFORMATION PROTECTION PLAN 1 +DYN365_ENTERPRISE_CUSTOMER_SERVICE =DYNAMICS 365 FOR CUSTOMER SERVICE ENTERPRISE EDITION +POWERFLOW_P1 = Microsoft PowerApps Plan 1 + POWERFLOW_P2 = Microsoft PowerApps Plan 2 + AAD_PREMIUM_P1 = Azure Active Directory Premium P1 + AAD_PREMIUM_P2 = Azure Active Directory Premium P2 diff --git a/Office 365 User Last Activity Time Report/Prerequisites.ps1 b/Office 365 User Last Activity Time Report/Prerequisites.ps1 new file mode 100644 index 0000000..b92b930 --- /dev/null +++ b/Office 365 User Last Activity Time Report/Prerequisites.ps1 @@ -0,0 +1,3 @@ +Set-ExecutionPolicy RemoteSigned +Install-Module MSOnline +Import-Module Msonline \ No newline at end of file diff --git a/Office 365 User Last Activity Time Report/RecipientTypeDetails.txt b/Office 365 User Last Activity Time Report/RecipientTypeDetails.txt new file mode 100644 index 0000000..fede245 --- /dev/null +++ b/Office 365 User Last Activity Time Report/RecipientTypeDetails.txt @@ -0,0 +1,80 @@ +UserMailbox +0 +LinkedMailbox +0 +SharedMailbox +0 +LegacyMailbox +0 +RoomMailbox +0 +EquipmentMailbox +0 +MailContact +0 +MailUser +0 +MailUniversalDistributionGroup +0 +MailNonUniversalGroup +0 +MailUniversalSecurityGroup +0 +DynamicDistributionGroup +0 +PublicFolder +0 +SystemAttendantMailbox +0 +SystemMailbox +0 +MailForestContact +0 +User +0 +Contact +0 +UniversalDistributionGroup +0 +UniversalSecurityGroup +0 +NonUniversalGroup +0 +DisabledUser +0 +MicrosoftExchange +0 +ArbitrationMailbox +0 +MailboxPlan +0 +LinkedUser +0 +RoomList +0 +DiscoveryMailbox +0 +RoleGroup +0 +RemoteUserMailbox +0 +Computer +0 +RemoteRoomMailbox +0 +RemoteEquipmentMailbox +0 +RemoteSharedMailbox +0 +PublicFolderMailbox +0 +TeamMailbox +0 +RemoteTeamMailbox +0 +MonitoringMailbox +0 +GroupMailbox +0 +AllUniqueRecipientTypes +0 \ No newline at end of file