Sharing interesting tiny script that make your life more easier :)
# Verify your machine having proper firewall rules in place.
# Test-NetConnection login.microsoftonline.com -Port 443
# Test-NetConnection autologon.microsoftazuread-sso.com -Port 443
# Enable Tls12 protocol
# [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Verify Microsoft Site
# Invoke-WebRequest -Uri "https://login.microsoftonline.com" -UseBasicParsing
# This report need some Microsoft Graph Command, hence we are installing the modules.
# Install-Module Microsoft.Graph -Scope AllUsers -Force
# Install-Module Microsoft.Graph -Scope AllUsers -Force -AllowClobber
# Connect Microsoft Graph by using your Azure AD credentials.
# Connect-MgGraph -Scopes "User.Read.All","Directory.Read.All"
# $Cred = Get-Credential
$listofusers = Get-content C:\temp\kumar\azuread1.txt
$Properties = @("Id","DisplayName","UserPrincipalName","SignInActivity")
foreach ($usr in $listofusers){
#$Users = Get-MgUser -All -Property $Properties | Select-Object -First 100
$Users1 = get-aduser -filter {UserPrincipalName -eq $usr} -Properties * -Server Server1.test.com -Credential $Cred | Select-Object SamAccountName,LastLogonDate
$Users = Get-MgUser -Filter "userPrincipalName eq '$usr'" -Property $Properties
$Users | Select-Object DisplayName, UserPrincipalName, @{Name="LastLoginDate";Expression={$_.SignInActivity.LastSignInDateTime}}
# Merge into one object
$Combined = [PSCustomObject]@{
AzureDisplayName = $Users.DisplayName
AzureUserPrincipalName = $Users.UserPrincipalName
AzureLastLoginDate = $Users.SignInActivity.LastSignInDateTime
OnPremSamAccountName = $Users1.SamAccountName
OnPremLastLogonDate = $Users1.LastLogonDate
}
# Output or export
$Combined | Export-Csv "AzureAD_LastLogin.csv" -NoTypeInformation -Append
}