2019-11-01 14:37:15 +05:30
|
|
|
|
#Accept input parameter
|
|
|
|
|
|
Param
|
|
|
|
|
|
(
|
|
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
|
|
[int]$InactiveDays,
|
|
|
|
|
|
[switch]$UserMailboxOnly,
|
|
|
|
|
|
[switch]$ReturnNeverLoggedInMB,
|
|
|
|
|
|
[string]$UserName,
|
|
|
|
|
|
[string]$Password,
|
|
|
|
|
|
[switch]$MFA
|
2020-02-05 16:46:42 +05:30
|
|
|
|
|
2019-11-01 14:37:15 +05:30
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
#Check for MSOnline module
|
2020-02-05 16:46:42 +05:30
|
|
|
|
$Modules=Get-Module -Name MSOnline -ListAvailable
|
2019-11-01 14:37:15 +05:30
|
|
|
|
if($Modules.count -eq 0)
|
|
|
|
|
|
{
|
2020-02-05 16:46:42 +05:30
|
|
|
|
Write-Host Please install MSOnline module using below command: `nInstall-Module MSOnline -ForegroundColor yellow
|
2019-11-01 14:37:15 +05:30
|
|
|
|
Exit
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-05 16:46:42 +05:30
|
|
|
|
#Connect AzureAD and Exchange Online from PowerShell
|
|
|
|
|
|
Get-PSSession | Remove-PSSession
|
2019-11-01 14:37:15 +05:30
|
|
|
|
|
2020-02-05 16:46:42 +05:30
|
|
|
|
#Get friendly name of license plan from external file
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$FriendlyNameHash=Get-Content -Raw -Path .\LicenseFriendlyName.txt -ErrorAction Stop | ConvertFrom-StringData
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-05 16:46:42 +05:30
|
|
|
|
#Set output file
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$ExportCSV=".\LastLogonTimeReport_$((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
|
2020-02-05 16:46:42 +05:30
|
|
|
|
|
|
|
|
|
|
Write-Host You can install module using below blog : `nhttps://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/ `nOR you can install module directly by entering "Y"`n
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$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
|
|
|
|
|
|
{
|
2020-02-05 16:46:42 +05:30
|
|
|
|
Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/'
|
2019-11-01 14:37:15 +05:30
|
|
|
|
Exit
|
|
|
|
|
|
}
|
|
|
|
|
|
$Confirmation= Read-Host Have you installed Exchange Online MFA Module? [Y] Yes [N] No
|
2020-02-05 16:46:42 +05:30
|
|
|
|
|
2019-11-01 14:37:15 +05:30
|
|
|
|
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
|
2020-02-05 16:46:42 +05:30
|
|
|
|
{
|
2019-11-01 14:37:15 +05:30
|
|
|
|
Write-Host Exchange Online PowerShell Module is required
|
2020-02-05 16:46:42 +05:30
|
|
|
|
Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/'
|
2019-11-01 14:37:15 +05:30
|
|
|
|
Exit
|
|
|
|
|
|
}
|
2020-02-05 16:46:42 +05:30
|
|
|
|
|
2019-11-01 14:37:15 +05:30
|
|
|
|
}
|
2020-02-05 16:46:42 +05:30
|
|
|
|
|
2019-11-01 14:37:15 +05:30
|
|
|
|
#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
|
|
|
|
|
|
}
|
2020-02-05 16:46:42 +05:30
|
|
|
|
Connect-MsolService -Credential $credential
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-05 16:46:42 +05:30
|
|
|
|
$Result=""
|
|
|
|
|
|
$Output=@()
|
|
|
|
|
|
$MBUserCount=0
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$OutputCount=0
|
|
|
|
|
|
|
2020-02-05 16:46:42 +05:30
|
|
|
|
|
|
|
|
|
|
Get-Mailbox -ResultSize Unlimited | Where{$_.DisplayName -notlike "Discovery Search Mailbox"} | ForEach-Object{
|
|
|
|
|
|
$upn=$_.UserPrincipalName
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$CreationTime=$_.WhenCreated
|
2020-02-05 16:46:42 +05:30
|
|
|
|
$LastLogonTime=(Get-MailboxStatistics -Identity $upn).lastlogontime
|
|
|
|
|
|
$DisplayName=$_.DisplayName
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$MBType=$_.RecipientTypeDetails
|
2020-02-05 16:46:42 +05:30
|
|
|
|
$Print=1
|
|
|
|
|
|
$MBUserCount++
|
|
|
|
|
|
$RolesAssigned=""
|
|
|
|
|
|
Write-Progress -Activity "`n Processed mailbox count: $MBUserCount "`n" Currently Processing: $DisplayName"
|
|
|
|
|
|
|
|
|
|
|
|
#Retrieve lastlogon time and then calculate Inactive days
|
|
|
|
|
|
if($LastLogonTime -eq $null)
|
|
|
|
|
|
{
|
|
|
|
|
|
$LastLogonTime ="Never Logged In"
|
|
|
|
|
|
$InactiveDaysOfUser="-"
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$InactiveDaysOfUser= (New-TimeSpan -Start $LastLogonTime).Days
|
2020-02-05 16:46:42 +05:30
|
|
|
|
}
|
2019-11-01 14:37:15 +05:30
|
|
|
|
|
2020-02-05 16:46:42 +05:30
|
|
|
|
#Get licenses assigned to mailboxes
|
|
|
|
|
|
$User=(Get-MsolUser -UserPrincipalName $upn)
|
|
|
|
|
|
$Licenses=$User.Licenses.AccountSkuId
|
|
|
|
|
|
$AssignedLicense=""
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$Count=0
|
|
|
|
|
|
|
2020-02-05 16:46:42 +05:30
|
|
|
|
#Convert license plan to friendly name
|
|
|
|
|
|
foreach($License in $Licenses)
|
|
|
|
|
|
{
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$Count++
|
2020-02-05 16:46:42 +05:30
|
|
|
|
$LicenseItem= $License -Split ":" | Select-Object -Last 1
|
|
|
|
|
|
$EasyName=$FriendlyNameHash[$LicenseItem]
|
|
|
|
|
|
if(!($EasyName))
|
|
|
|
|
|
{$NamePrint=$LicenseItem}
|
|
|
|
|
|
else
|
|
|
|
|
|
{$NamePrint=$EasyName}
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$AssignedLicense=$AssignedLicense+$NamePrint
|
|
|
|
|
|
if($count -lt $licenses.count)
|
|
|
|
|
|
{
|
|
|
|
|
|
$AssignedLicense=$AssignedLicense+","
|
|
|
|
|
|
}
|
2020-02-05 16:46:42 +05:30
|
|
|
|
}
|
|
|
|
|
|
if($Licenses.count -eq 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
$AssignedLicense="No License Assigned"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#Inactive days based filter
|
|
|
|
|
|
if($InactiveDaysOfUser -ne "-"){
|
|
|
|
|
|
if(($InactiveDays -ne "") -and ([int]$InactiveDays -gt $InactiveDaysOfUser))
|
|
|
|
|
|
{
|
|
|
|
|
|
$Print=0
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
|
|
#License assigned based filter
|
2019-11-01 14:37:15 +05:30
|
|
|
|
if(($UserMailboxOnly.IsPresent) -and ($MBType -ne "UserMailbox"))
|
2020-02-05 16:46:42 +05:30
|
|
|
|
{
|
|
|
|
|
|
$Print=0
|
|
|
|
|
|
}
|
2019-11-01 14:37:15 +05:30
|
|
|
|
|
|
|
|
|
|
#Never Logged In user
|
|
|
|
|
|
if(($ReturnNeverLoggedInMB.IsPresent) -and ($LastLogonTime -ne "Never Logged In"))
|
|
|
|
|
|
{
|
|
|
|
|
|
$Print=0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-05 16:46:42 +05:30
|
|
|
|
#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
|
|
|
|
|
|
if($Print -eq 1)
|
|
|
|
|
|
{
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$OutputCount++
|
2020-02-05 16:46:42 +05:30
|
|
|
|
$Result=@{'UserPrincipalName'=$upn;'DisplayName'=$DisplayName;'LastLogonTime'=$LastLogonTime;'CreationTime'=$CreationTime;'InactiveDays'=$InactiveDaysOfUser;'MailboxType'=$MBType; 'AssignedLicenses'=$AssignedLicense;'Roles'=$RolesAssigned}
|
|
|
|
|
|
$Output= New-Object PSObject -Property $Result
|
2019-11-01 14:37:15 +05:30
|
|
|
|
$Output | Select-Object UserPrincipalName,DisplayName,LastLogonTime,CreationTime,InactiveDays,MailboxType,AssignedLicenses,Roles | Export-Csv -Path $ExportCSV -Notype -Append
|
2020-02-05 16:46:42 +05:30
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-11-01 14:37:15 +05:30
|
|
|
|
|
2020-02-05 16:46:42 +05:30
|
|
|
|
#Open output file after execution
|
2019-11-01 14:37:15 +05:30
|
|
|
|
Write-Host `nScript executed successfully
|
|
|
|
|
|
if((Test-Path -Path $ExportCSV) -eq "True")
|
|
|
|
|
|
{
|
|
|
|
|
|
Write-Host Result contains $OutputCount mailboxes
|
2020-02-05 16:46:42 +05:30
|
|
|
|
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"
|
|
|
|
|
|
}
|
2019-11-01 14:37:15 +05:30
|
|
|
|
}
|
|
|
|
|
|
Else
|
|
|
|
|
|
{
|
|
|
|
|
|
Write-Host No mailbox found
|
|
|
|
|
|
}
|
2020-02-05 16:46:42 +05:30
|
|
|
|
#Clean up session
|
|
|
|
|
|
#Get-PSSession | Remove-PSSession
|