mirror of
https://github.com/admindroid-community/powershell-scripts.git
synced 2025-12-17 16:35:19 +00:00
Export Office 365 Users last Logon Time Report
Updated links
This commit is contained in:
parent
1eee308f44
commit
632e8ec561
@ -8,25 +8,25 @@ Param
|
|||||||
[string]$UserName,
|
[string]$UserName,
|
||||||
[string]$Password,
|
[string]$Password,
|
||||||
[switch]$MFA
|
[switch]$MFA
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
#Check for MSOnline module
|
#Check for MSOnline module
|
||||||
$Modules=Get-Module -Name MSOnline -ListAvailable
|
$Modules=Get-Module -Name MSOnline -ListAvailable
|
||||||
if($Modules.count -eq 0)
|
if($Modules.count -eq 0)
|
||||||
{
|
{
|
||||||
Write-Host Please install MSOnline module using below command: `nInstall-Module MSOnline -ForegroundColor yellow
|
Write-Host Please install MSOnline module using below command: `nInstall-Module MSOnline -ForegroundColor yellow
|
||||||
Exit
|
Exit
|
||||||
}
|
}
|
||||||
|
|
||||||
#Connect AzureAD and Exchange Online from PowerShell
|
#Connect AzureAD and Exchange Online from PowerShell
|
||||||
Get-PSSession | Remove-PSSession
|
Get-PSSession | Remove-PSSession
|
||||||
|
|
||||||
#Get friendly name of license plan from external file
|
#Get friendly name of license plan from external file
|
||||||
$FriendlyNameHash=Get-Content -Raw -Path .\LicenseFriendlyName.txt -ErrorAction Stop | ConvertFrom-StringData
|
$FriendlyNameHash=Get-Content -Raw -Path .\LicenseFriendlyName.txt -ErrorAction Stop | ConvertFrom-StringData
|
||||||
|
|
||||||
|
|
||||||
#Set output file
|
#Set output file
|
||||||
$ExportCSV=".\LastLogonTimeReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
|
$ExportCSV=".\LastLogonTimeReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
|
||||||
|
|
||||||
#Authentication using MFA
|
#Authentication using MFA
|
||||||
@ -36,8 +36,8 @@ $ExportCSV=".\LastLogonTimeReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt
|
|||||||
If ($MFAExchangeModule -eq $null)
|
If ($MFAExchangeModule -eq $null)
|
||||||
{
|
{
|
||||||
Write-Host `nPlease install Exchange Online MFA Module. -ForegroundColor yellow
|
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
|
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
|
||||||
$Confirm= Read-Host Are you sure you want to install module directly? [Y] Yes [N] No
|
$Confirm= Read-Host Are you sure you want to install module directly? [Y] Yes [N] No
|
||||||
if($Confirm -match "[yY]")
|
if($Confirm -match "[yY]")
|
||||||
{
|
{
|
||||||
@ -46,11 +46,11 @@ $ExportCSV=".\LastLogonTimeReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Start-Process 'https://o365reports.com/2019/03/23/export-dynamic-distribution-group-members-to-csv/'
|
Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/'
|
||||||
Exit
|
Exit
|
||||||
}
|
}
|
||||||
$Confirmation= Read-Host Have you installed Exchange Online MFA Module? [Y] Yes [N] No
|
$Confirmation= Read-Host Have you installed Exchange Online MFA Module? [Y] Yes [N] No
|
||||||
|
|
||||||
if($Confirmation -match "[yY]")
|
if($Confirmation -match "[yY]")
|
||||||
{
|
{
|
||||||
$MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1)
|
$MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1)
|
||||||
@ -61,14 +61,14 @@ $ExportCSV=".\LastLogonTimeReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Write-Host Exchange Online PowerShell Module is required
|
Write-Host Exchange Online PowerShell Module is required
|
||||||
Start-Process 'https://o365reports.com/2019/03/23/export-dynamic-distribution-group-members-to-csv/'
|
Start-Process 'https://o365reports.com/2019/04/17/connect-exchange-online-using-mfa/'
|
||||||
Exit
|
Exit
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#Importing Exchange MFA Module
|
#Importing Exchange MFA Module
|
||||||
. "$MFAExchangeModule"
|
. "$MFAExchangeModule"
|
||||||
Write-Host Enter credential in prompt to connect to Exchange Online
|
Write-Host Enter credential in prompt to connect to Exchange Online
|
||||||
@ -92,78 +92,78 @@ $ExportCSV=".\LastLogonTimeReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt
|
|||||||
{
|
{
|
||||||
$Credential=Get-Credential -Credential $null
|
$Credential=Get-Credential -Credential $null
|
||||||
}
|
}
|
||||||
Connect-MsolService -Credential $credential
|
Connect-MsolService -Credential $credential
|
||||||
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
|
$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
|
Import-PSSession $Session -CommandName Get-Mailbox,Get-MailboxStatistics -FormatTypeName * -AllowClobber | Out-Null
|
||||||
}
|
}
|
||||||
|
|
||||||
$Result=""
|
$Result=""
|
||||||
$Output=@()
|
$Output=@()
|
||||||
$MBUserCount=0
|
$MBUserCount=0
|
||||||
$OutputCount=0
|
$OutputCount=0
|
||||||
|
|
||||||
|
|
||||||
Get-Mailbox -ResultSize Unlimited | Where{$_.DisplayName -notlike "Discovery Search Mailbox"} | ForEach-Object{
|
Get-Mailbox -ResultSize Unlimited | Where{$_.DisplayName -notlike "Discovery Search Mailbox"} | ForEach-Object{
|
||||||
$upn=$_.UserPrincipalName
|
$upn=$_.UserPrincipalName
|
||||||
$CreationTime=$_.WhenCreated
|
$CreationTime=$_.WhenCreated
|
||||||
$LastLogonTime=(Get-MailboxStatistics -Identity $upn).lastlogontime
|
$LastLogonTime=(Get-MailboxStatistics -Identity $upn).lastlogontime
|
||||||
$DisplayName=$_.DisplayName
|
$DisplayName=$_.DisplayName
|
||||||
$MBType=$_.RecipientTypeDetails
|
$MBType=$_.RecipientTypeDetails
|
||||||
$Print=1
|
$Print=1
|
||||||
$MBUserCount++
|
$MBUserCount++
|
||||||
$RolesAssigned=""
|
$RolesAssigned=""
|
||||||
Write-Progress -Activity "`n Processed mailbox count: $MBUserCount "`n" Currently Processing: $DisplayName"
|
Write-Progress -Activity "`n Processed mailbox count: $MBUserCount "`n" Currently Processing: $DisplayName"
|
||||||
|
|
||||||
#Retrieve lastlogon time and then calculate Inactive days
|
#Retrieve lastlogon time and then calculate Inactive days
|
||||||
if($LastLogonTime -eq $null)
|
if($LastLogonTime -eq $null)
|
||||||
{
|
{
|
||||||
$LastLogonTime ="Never Logged In"
|
$LastLogonTime ="Never Logged In"
|
||||||
$InactiveDaysOfUser="-"
|
$InactiveDaysOfUser="-"
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$InactiveDaysOfUser= (New-TimeSpan -Start $LastLogonTime).Days
|
$InactiveDaysOfUser= (New-TimeSpan -Start $LastLogonTime).Days
|
||||||
}
|
}
|
||||||
|
|
||||||
#Get licenses assigned to mailboxes
|
#Get licenses assigned to mailboxes
|
||||||
$User=(Get-MsolUser -UserPrincipalName $upn)
|
$User=(Get-MsolUser -UserPrincipalName $upn)
|
||||||
$Licenses=$User.Licenses.AccountSkuId
|
$Licenses=$User.Licenses.AccountSkuId
|
||||||
$AssignedLicense=""
|
$AssignedLicense=""
|
||||||
$Count=0
|
$Count=0
|
||||||
|
|
||||||
#Convert license plan to friendly name
|
#Convert license plan to friendly name
|
||||||
foreach($License in $Licenses)
|
foreach($License in $Licenses)
|
||||||
{
|
{
|
||||||
$Count++
|
$Count++
|
||||||
$LicenseItem= $License -Split ":" | Select-Object -Last 1
|
$LicenseItem= $License -Split ":" | Select-Object -Last 1
|
||||||
$EasyName=$FriendlyNameHash[$LicenseItem]
|
$EasyName=$FriendlyNameHash[$LicenseItem]
|
||||||
if(!($EasyName))
|
if(!($EasyName))
|
||||||
{$NamePrint=$LicenseItem}
|
{$NamePrint=$LicenseItem}
|
||||||
else
|
else
|
||||||
{$NamePrint=$EasyName}
|
{$NamePrint=$EasyName}
|
||||||
$AssignedLicense=$AssignedLicense+$NamePrint
|
$AssignedLicense=$AssignedLicense+$NamePrint
|
||||||
if($count -lt $licenses.count)
|
if($count -lt $licenses.count)
|
||||||
{
|
{
|
||||||
$AssignedLicense=$AssignedLicense+","
|
$AssignedLicense=$AssignedLicense+","
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if($Licenses.count -eq 0)
|
if($Licenses.count -eq 0)
|
||||||
{
|
{
|
||||||
$AssignedLicense="No License Assigned"
|
$AssignedLicense="No License Assigned"
|
||||||
}
|
}
|
||||||
|
|
||||||
#Inactive days based filter
|
#Inactive days based filter
|
||||||
if($InactiveDaysOfUser -ne "-"){
|
if($InactiveDaysOfUser -ne "-"){
|
||||||
if(($InactiveDays -ne "") -and ([int]$InactiveDays -gt $InactiveDaysOfUser))
|
if(($InactiveDays -ne "") -and ([int]$InactiveDays -gt $InactiveDaysOfUser))
|
||||||
{
|
{
|
||||||
$Print=0
|
$Print=0
|
||||||
}}
|
}}
|
||||||
|
|
||||||
#License assigned based filter
|
#License assigned based filter
|
||||||
if(($UserMailboxOnly.IsPresent) -and ($MBType -ne "UserMailbox"))
|
if(($UserMailboxOnly.IsPresent) -and ($MBType -ne "UserMailbox"))
|
||||||
{
|
{
|
||||||
$Print=0
|
$Print=0
|
||||||
}
|
}
|
||||||
|
|
||||||
#Never Logged In user
|
#Never Logged In user
|
||||||
if(($ReturnNeverLoggedInMB.IsPresent) -and ($LastLogonTime -ne "Never Logged In"))
|
if(($ReturnNeverLoggedInMB.IsPresent) -and ($LastLogonTime -ne "Never Logged In"))
|
||||||
@ -171,51 +171,51 @@ Get-Mailbox -ResultSize Unlimited | Where{$_.DisplayName -notlike "Discovery Sea
|
|||||||
$Print=0
|
$Print=0
|
||||||
}
|
}
|
||||||
|
|
||||||
#Get roles assigned to user
|
#Get roles assigned to user
|
||||||
$Roles=(Get-MsolUserRole -UserPrincipalName $upn).Name
|
$Roles=(Get-MsolUserRole -UserPrincipalName $upn).Name
|
||||||
if($Roles.count -eq 0)
|
if($Roles.count -eq 0)
|
||||||
{
|
{
|
||||||
$RolesAssigned="No roles"
|
$RolesAssigned="No roles"
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
foreach($Role in $Roles)
|
foreach($Role in $Roles)
|
||||||
{
|
{
|
||||||
$RolesAssigned=$RolesAssigned+$Role
|
$RolesAssigned=$RolesAssigned+$Role
|
||||||
if($Roles.indexof($role) -lt (($Roles.count)-1))
|
if($Roles.indexof($role) -lt (($Roles.count)-1))
|
||||||
{
|
{
|
||||||
$RolesAssigned=$RolesAssigned+","
|
$RolesAssigned=$RolesAssigned+","
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Export result to CSV file
|
#Export result to CSV file
|
||||||
if($Print -eq 1)
|
if($Print -eq 1)
|
||||||
{
|
{
|
||||||
$OutputCount++
|
$OutputCount++
|
||||||
$Result=@{'UserPrincipalName'=$upn;'DisplayName'=$DisplayName;'LastLogonTime'=$LastLogonTime;'CreationTime'=$CreationTime;'InactiveDays'=$InactiveDaysOfUser;'MailboxType'=$MBType; 'AssignedLicenses'=$AssignedLicense;'Roles'=$RolesAssigned}
|
$Result=@{'UserPrincipalName'=$upn;'DisplayName'=$DisplayName;'LastLogonTime'=$LastLogonTime;'CreationTime'=$CreationTime;'InactiveDays'=$InactiveDaysOfUser;'MailboxType'=$MBType; 'AssignedLicenses'=$AssignedLicense;'Roles'=$RolesAssigned}
|
||||||
$Output= New-Object PSObject -Property $Result
|
$Output= New-Object PSObject -Property $Result
|
||||||
$Output | Select-Object UserPrincipalName,DisplayName,LastLogonTime,CreationTime,InactiveDays,MailboxType,AssignedLicenses,Roles | Export-Csv -Path $ExportCSV -Notype -Append
|
$Output | Select-Object UserPrincipalName,DisplayName,LastLogonTime,CreationTime,InactiveDays,MailboxType,AssignedLicenses,Roles | Export-Csv -Path $ExportCSV -Notype -Append
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Open output file after execution
|
#Open output file after execution
|
||||||
Write-Host `nScript executed successfully
|
Write-Host `nScript executed successfully
|
||||||
if((Test-Path -Path $ExportCSV) -eq "True")
|
if((Test-Path -Path $ExportCSV) -eq "True")
|
||||||
{
|
{
|
||||||
Write-Host Result contains $OutputCount mailboxes
|
Write-Host Result contains $OutputCount mailboxes
|
||||||
Write-Host "Detailed report available in: $ExportCSV"
|
Write-Host "Detailed report available in: $ExportCSV"
|
||||||
$Prompt = New-Object -ComObject wscript.shell
|
$Prompt = New-Object -ComObject wscript.shell
|
||||||
$UserInput = $Prompt.popup("Do you want to open output file?",`
|
$UserInput = $Prompt.popup("Do you want to open output file?",`
|
||||||
0,"Open Output File",4)
|
0,"Open Output File",4)
|
||||||
If ($UserInput -eq 6)
|
If ($UserInput -eq 6)
|
||||||
{
|
{
|
||||||
Invoke-Item "$ExportCSV"
|
Invoke-Item "$ExportCSV"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Else
|
Else
|
||||||
{
|
{
|
||||||
Write-Host No mailbox found
|
Write-Host No mailbox found
|
||||||
}
|
}
|
||||||
#Clean up session
|
#Clean up session
|
||||||
#Get-PSSession | Remove-PSSession
|
#Get-PSSession | Remove-PSSession
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user