This script will connect to Azure Active Directory, and download a user report to CSV file.
Import-Module -Name AzureAD
Connect-AzureAD
$ErrorActionPreference = "Stop"
Function Get-Users {
<#
.SYNOPSIS
Get users from the requested DN
#>
process{
# Set the properties to retrieve
$properties = @(
'ObjectId',
'DisplayName',
'userprincipalname',
'mail',
'jobtitle',
'department',
'telephoneNumber',
'PhysicalDeliveryOfficeName',
'mobile',
'streetAddress',
'city',
'postalcode',
'state',
'country',
'AccountEnabled'
)
$filter = "AccountEnabled eq true"
# Get the users
Get-AzureADUser -Filter $filter -All $true | select $properties
}
}
Function Get-AllAzureADUsers {
<#
.SYNOPSIS
Get all AD users
#>
process {
Write-Host "Collecting users" -ForegroundColor Cyan
$users = @()
# Collect users
$users += Get-Users
# Loop through all users
$users | ForEach {
$manager = ""
If (($getManager.IsPresent)) {
# Get the users' manager
$manager = Get-AzureADUserManager -ObjectId $_.ObjectId | Select -ExpandProperty DisplayName
}
[pscustomobject]@{
"Name" = $_.DisplayName
"UserPrincipalName" = $_.UserPrincipalName
"Emailaddress" = $_.mail
"Job title" = $_.JobTitle
"Manager" = $manager
"Department" = $_.Department
"Office" = $_.PhysicalDeliveryOfficeName
"Phone" = $_.telephoneNumber
"Mobile" = $_.mobile
"Enabled" = if ($_.AccountEnabled) {"enabled"} else {"disabled"}
"Street" = $_.StreetAddress
"City" = $_.City
"Postal code" = $_.PostalCode
"State" = $_.State
"Country" = $_.Country
}
}
}
}
# Split path
$Path = Split-Path -Parent "C:\SWSetup\*.*"
# Create variable for the date stamp in log file
$LogDate = Get-Date -f yyyyMMddhhmm
# Define CSV and log file location variables
# They have to be on the same location as the script
$path = $Path + "\AllAzADUsers_$logDate.csv"
Get-AllAzureADUsers | Sort-Object Name | Export-CSV -Path $path -NoTypeInformation
if ((Get-Item $path).Length -gt 0) {
Write-Host "Report finished and saved in $path" -ForegroundColor Green
# Open the CSV file
Invoke-Item $path
}else{
Write-Host "Failed to create report" -ForegroundColor Red
}
Connect-AzureAD
$ErrorActionPreference = "Stop"
Function Get-Users {
<#
Get users from the requested DN
#>
process{
# Set the properties to retrieve
$properties = @(
'ObjectId',
'DisplayName',
'userprincipalname',
'mail',
'jobtitle',
'department',
'telephoneNumber',
'PhysicalDeliveryOfficeName',
'mobile',
'streetAddress',
'city',
'postalcode',
'state',
'country',
'AccountEnabled'
)
$filter = "AccountEnabled eq true"
# Get the users
Get-AzureADUser -Filter $filter -All $true | select $properties
}
}
Function Get-AllAzureADUsers {
<#
Get all AD users
#>
process {
Write-Host "Collecting users" -ForegroundColor Cyan
$users = @()
# Collect users
$users += Get-Users
# Loop through all users
$users | ForEach {
$manager = ""
If (($getManager.IsPresent)) {
# Get the users' manager
$manager = Get-AzureADUserManager -ObjectId $_.ObjectId | Select -ExpandProperty DisplayName
}
[pscustomobject]@{
"Name" = $_.DisplayName
"UserPrincipalName" = $_.UserPrincipalName
"Emailaddress" = $_.mail
"Job title" = $_.JobTitle
"Manager" = $manager
"Department" = $_.Department
"Office" = $_.PhysicalDeliveryOfficeName
"Phone" = $_.telephoneNumber
"Mobile" = $_.mobile
"Enabled" = if ($_.AccountEnabled) {"enabled"} else {"disabled"}
"Street" = $_.StreetAddress
"City" = $_.City
"Postal code" = $_.PostalCode
"State" = $_.State
"Country" = $_.Country
}
}
}
}
# Split path
$Path = Split-Path -Parent "C:\SWSetup\*.*"
# Create variable for the date stamp in log file
$LogDate = Get-Date -f yyyyMMddhhmm
# Define CSV and log file location variables
# They have to be on the same location as the script
$path = $Path + "\AllAzADUsers_$logDate.csv"
Get-AllAzureADUsers | Sort-Object Name | Export-CSV -Path $path -NoTypeInformation
if ((Get-Item $path).Length -gt 0) {
Write-Host "Report finished and saved in $path" -ForegroundColor Green
# Open the CSV file
Invoke-Item $path
}else{
Write-Host "Failed to create report" -ForegroundColor Red
}
Sources
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.