mirror of
https://github.com/admindroid-community/powershell-scripts.git
synced 2025-12-17 16:35:19 +00:00
Get Guest User Report with MS Graph PowerShell
Get Guest User Report with MS Graph PowerShell
This commit is contained in:
parent
8bd03d7444
commit
dbff9a3c80
@ -1,62 +1,72 @@
|
||||
#Accept input parameter
|
||||
<#
|
||||
=============================================================================================
|
||||
Name: Export Office 365 Guest user and their membership report using MS Graph PowerShell
|
||||
Version: 3.0
|
||||
Website: o365reports.com
|
||||
For detailed Script execution: https://o365reports.com/2020/11/12/export-office-365-guest-user-report-with-their-membership/
|
||||
============================================================================================
|
||||
#>
|
||||
#Accept input parameter
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[int]$StaleGuests,
|
||||
[int]$RecentlyCreatedGuests,
|
||||
[string]$UserName,
|
||||
[string]$Password
|
||||
|
||||
[string]$TenantId,
|
||||
[string]$ClientId,
|
||||
[string]$CertificateThumbprint
|
||||
)
|
||||
|
||||
#Check for AzureAD module
|
||||
$Module=Get-Module -Name AzureAD -ListAvailable
|
||||
if($Module.count -eq 0)
|
||||
$MsGraphModule = Get-Module Microsoft.Graph -ListAvailable
|
||||
if($MsGraphModule -eq $null)
|
||||
{
|
||||
Write-Host AzureAD 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 "Important: Microsoft graph module is unavailable. It is mandatory to have this module installed in the system to run the script successfully."
|
||||
$confirm = Read-Host Are you sure you want to install Microsoft graph module? [Y] Yes [N] No
|
||||
if($confirm -match "[yY]")
|
||||
{
|
||||
Install-Module AzureAD
|
||||
Import-Module AzureAD
|
||||
Write-host "Installing Microsoft graph module..."
|
||||
Install-Module Microsoft.Graph -Scope CurrentUser
|
||||
Write-host "Microsoft graph module is installed in the machine successfully" -ForegroundColor Magenta
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-Host AzureAD module is required to connect AzureAD.Please install module using Install-Module AzureAD cmdlet.
|
||||
Write-host "Exiting. `nNote: Microsoft graph module must be available in your system to run the script" -ForegroundColor Red
|
||||
Exit
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host Connecting Azure AD... -ForegroundColor Yellow
|
||||
#Storing credential in script for scheduling purpose/ Passing credential as parameter
|
||||
if(($UserName -ne "") -and ($Password -ne ""))
|
||||
if(($TenantId -ne "") -and ($ClientId -ne "") -and ($CertificateThumbprint -ne ""))
|
||||
{
|
||||
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
|
||||
$Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword
|
||||
Connect-AzureAD -Credential $credential | Out-Null
|
||||
Connect-MgGraph -TenantId $TenantId -AppId $ClientId -CertificateThumbprint $CertificateThumbprint -ErrorAction SilentlyContinue -ErrorVariable ConnectionError|Out-Null
|
||||
if($ConnectionError -ne $null)
|
||||
{
|
||||
Write-Host $ConnectionError -Foregroundcolor Red
|
||||
Exit
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Connect-AzureAD | Out-Null
|
||||
Connect-MgGraph -Scopes "Directory.Read.All" -ErrorAction SilentlyContinue -Errorvariable ConnectionError |Out-Null
|
||||
if($ConnectionError -ne $null)
|
||||
{
|
||||
Write-Host "$ConnectionError" -Foregroundcolor Red
|
||||
Exit
|
||||
}
|
||||
|
||||
}
|
||||
Write-Host "Microsoft Graph Powershell module is connected successfully" -ForegroundColor Green
|
||||
Select-MgProfile beta
|
||||
$Result=""
|
||||
$Results=@()
|
||||
$GuestCount=0
|
||||
$PrintedGuests=0
|
||||
|
||||
#Output file declaration
|
||||
$ExportCSV=".\GuestUserReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
|
||||
$ExportCSV=".\GuestUserReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm-ss` tt).ToString()).csv"
|
||||
Write-Host `nExporting report...
|
||||
#Getting guest users
|
||||
Get-AzureADUser -All $true -Filter "UserType eq 'Guest'" | foreach {
|
||||
Get-MgUser -All -Filter "UserType eq 'Guest'" -ExpandProperty MemberOf | foreach {
|
||||
$DisplayName = $_.DisplayName
|
||||
$Upn=$_.UserPrincipalName
|
||||
$GuestCount++
|
||||
Write-Progress -Activity "`n Processed mailbox count: $GuestCount "`n" Currently Processing: $DisplayName"
|
||||
$GetCreationTime=$_.ExtensionProperty
|
||||
$CreationTime=$GetCreationTime.createdDateTime
|
||||
$AccountAge= (New-TimeSpan -Start $CreationTime).Days
|
||||
$AccountAge = (New-TimeSpan -Start $_.CreatedDateTime).Days
|
||||
|
||||
#Check for stale guest users
|
||||
if(($StaleGuests -ne "") -and ([int]$AccountAge -lt $StaleGuests))
|
||||
@ -69,41 +79,20 @@ Get-AzureADUser -All $true -Filter "UserType eq 'Guest'" | foreach {
|
||||
{
|
||||
return
|
||||
}
|
||||
|
||||
$ObjectId=$_.ObjectId
|
||||
$Mail=$_.Mail
|
||||
$Company = $_.CompanyName
|
||||
if($Company -eq $null)
|
||||
{
|
||||
$Company = "-"
|
||||
}
|
||||
$CreationType=$_.CreationType
|
||||
$InvitationAccepted=$_.UserState
|
||||
|
||||
#Getting guest user's group membership
|
||||
$Groups=(Get-AzureADUserMembership -ObjectId $ObjectId).DisplayName
|
||||
#$Groups
|
||||
$GroupMembership=""
|
||||
foreach($Group in $Groups)
|
||||
$GroupMembership = @($_.MemberOf.AdditionalProperties.displayName) -join ','
|
||||
if($GroupMembership -eq $null)
|
||||
{
|
||||
#$Group
|
||||
if($GroupMembership -ne "")
|
||||
{
|
||||
$GroupMembership=$GroupMembership+","
|
||||
$GroupMembership = '-'
|
||||
}
|
||||
$GroupMembership=$GroupMembership+$Group
|
||||
}
|
||||
if($GroupMembership -eq "")
|
||||
{
|
||||
$GroupMembership="-"
|
||||
}
|
||||
|
||||
|
||||
#Export result to CSV file
|
||||
$PrintedGuests++
|
||||
$Result=@{'UserPrincipalName'=$upn;'DisplayName'=$DisplayName;'EmailAddress'=$Mail;'Company'=$Company;'CreationTime'=$CreationTime;'AccountAge(days)'=$AccountAge;'InvitationAccepted'=$InvitationAccepted;'CreationType'=$CreationType; 'GroupMembership'=$GroupMembership}
|
||||
$Output= New-Object PSObject -Property $Result
|
||||
$Output | Select-Object DisplayName,UserPrincipalName,Company,EmailAddress,CreationTime,AccountAge'(Days)',CreationType,InvitationAccepted,GroupMembership | Export-Csv -Path $ExportCSV -Notype -Append
|
||||
$Result = [PSCustomObject] @{'DisplayName'=$DisplayName;'UserPrincipalName'=$_.UserPrincipalName;'Company'=$Company;'EmailAddress'=$_.Mail;'CreationTime'=$_.CreatedDateTime ;'AccountAge(days)'=$AccountAge;'CreationType'=$_.CreationType;'InvitationAccepted'=$_.ExternalUserState;'GroupMembership'=$GroupMembership}
|
||||
$Result | Export-Csv -Path $ExportCSV -Notype -Append
|
||||
}
|
||||
|
||||
#Open output file after execution
|
||||
@ -113,14 +102,14 @@ Get-AzureADUser -All $true -Filter "UserType eq 'Guest'" | foreach {
|
||||
Write-Host "Detailed report available in: $ExportCSV" -ForegroundColor Green
|
||||
Write-Host `nThe Output file contains $PrintedGuests guest users.
|
||||
$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)
|
||||
$UserInput = $Prompt.popup("Do you want to open output file?",` 0,"Open Output File",4)
|
||||
if ($UserInput -eq 6)
|
||||
{
|
||||
Invoke-Item "$ExportCSV"
|
||||
}
|
||||
}
|
||||
Else
|
||||
else
|
||||
{
|
||||
Write-Host No guest user found
|
||||
Write-Host "No guest user found" -ForegroundColor Red
|
||||
}
|
||||
Disconnect-MgGraph|Out-Null
|
||||
Loading…
x
Reference in New Issue
Block a user