mirror of
https://github.com/admindroid-community/powershell-scripts.git
synced 2025-12-29 16:14:48 +00:00
Export Office 365 users last logon time report
Export Office 365 users last logon time report
This commit is contained in:
@@ -1,4 +1,14 @@
|
||||
#Accept input parameter
|
||||
<#
|
||||
=============================================================================================
|
||||
Name: Export Office 365 user last logon time report
|
||||
Description: This script exports Office 365 users' last logon time CSV
|
||||
Version: 3.0
|
||||
website: o365reports.com
|
||||
Script by: O365Reports Team
|
||||
For detailed Script execution: https://o365reports.com/2019/03/07/export-office-365-users-last-logon-time-csv/
|
||||
============================================================================================
|
||||
#>
|
||||
#Accept input parameter
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory = $false)]
|
||||
@@ -11,15 +21,25 @@ Param
|
||||
|
||||
)
|
||||
|
||||
#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
|
||||
#Check for MSOnline module
|
||||
$Module=Get-Module -Name MSOnline -ListAvailable
|
||||
if($Module.count -eq 0)
|
||||
{
|
||||
Write-Host MSOnline module is not available -ForegroundColor yellow
|
||||
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
|
||||
if($Confirm -match "[yY]")
|
||||
{
|
||||
Install-Module MSOnline
|
||||
Import-Module MSOnline
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-Host MSOnline module is required to connect AzureAD.Please install module using Install-Module MSOnline cmdlet.
|
||||
Exit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Connect AzureAD and Exchange Online from PowerShell
|
||||
#Clear session
|
||||
Get-PSSession | Remove-PSSession
|
||||
|
||||
#Get friendly name of license plan from external file
|
||||
@@ -29,72 +49,50 @@ $FriendlyNameHash=Get-Content -Raw -Path .\LicenseFriendlyName.txt -ErrorAction
|
||||
#Set output file
|
||||
$ExportCSV=".\LastLogonTimeReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
|
||||
|
||||
#Authentication using MFA
|
||||
#Check for EXO v2 module inatallation
|
||||
$Module = Get-Module ExchangeOnlineManagement -ListAvailable
|
||||
if($Module.count -eq 0)
|
||||
{
|
||||
Write-Host Exchange Online PowerShell V2 module is not available -ForegroundColor yellow
|
||||
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
|
||||
if($Confirm -match "[yY]")
|
||||
{
|
||||
Write-host "Installing Exchange Online PowerShell module"
|
||||
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-Host EXO V2 module is required to connect Exchange Online.Please install module using Install-Module ExchangeOnlineManagement cmdlet.
|
||||
Exit
|
||||
}
|
||||
}
|
||||
|
||||
#Connect Exchange Online with 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 : `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
|
||||
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/04/17/connect-exchange-online-using-mfa/'
|
||||
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/04/17/connect-exchange-online-using-mfa/'
|
||||
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
|
||||
Write-Host Connecting Exchange Online...
|
||||
Connect-ExchangeOnline | Out-Null
|
||||
Write-Host Connecting to Office 365...
|
||||
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
|
||||
#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
|
||||
}
|
||||
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
|
||||
else
|
||||
{
|
||||
$Credential=Get-Credential -Credential $null
|
||||
}
|
||||
Write-Host Connecting Exchange Online...
|
||||
Connect-ExchangeOnline -Credential $Credential
|
||||
Write-Host Connecting to Office 365...
|
||||
Connect-MsolService -Credential $Credential
|
||||
}
|
||||
|
||||
$Result=""
|
||||
@@ -203,8 +201,10 @@ Get-Mailbox -ResultSize Unlimited | Where{$_.DisplayName -notlike "Discovery Sea
|
||||
Write-Host `nScript executed successfully
|
||||
if((Test-Path -Path $ExportCSV) -eq "True")
|
||||
{
|
||||
Write-Host Result contains $OutputCount mailboxes
|
||||
|
||||
Write-Host "Detailed report available in: $ExportCSV"
|
||||
|
||||
Write-Host Exported report has $OutputCount mailboxes
|
||||
$Prompt = New-Object -ComObject wscript.shell
|
||||
$UserInput = $Prompt.popup("Do you want to open output file?",`
|
||||
0,"Open Output File",4)
|
||||
@@ -218,4 +218,4 @@ Else
|
||||
Write-Host No mailbox found
|
||||
}
|
||||
#Clean up session
|
||||
#Get-PSSession | Remove-PSSession
|
||||
Get-PSSession | Remove-PSSession
|
||||
|
||||
Reference in New Issue
Block a user