Share via

Facing error when running a PowerShell script using mggraph

Anonymous
2024-08-30T22:09:40+00:00

I am facing issue in fetching lastsignindate from azure ad using mggraph it returns error of 404 not found whereas user is present in azure ad.

The script i am sharing has some fields blank for security reasons:

Function to authenticate with Microsoft Graph

function Get-GraphToken {
param (
[string]$tenantId,
[string]$clientId,
[string]$clientSecret,
[string]$authUrl
)
$authBody = @{
grant_type = "client_credentials"
scope = "https://graph.microsoft.com/.default"
client_id = $clientId
client_secret = $clientSecret
}
try {
$tokenResponse = Invoke-RestMethod -Method Post -Uri $authUrl -ContentType "application/x-www-form-urlencoded" -Body $authBody
return $tokenResponse.access_token
} catch {
Write-Error "Failed to authenticate with Microsoft Graph: $_"
return $null
}
}

Function to get the most recent LastLogon attribute from all domain controllers

function Get-LastLogon {
param (
[string]$userName
)
$dcs = Get-ADDomainController -Filter * | Select-Object -ExpandProperty HostName
$lastLogon = 0
foreach ($dc in $dcs) {
try {
$user = Get-ADUser $userName -Server $dc -Properties LastLogon
if ($user.LastLogon -gt $lastLogon) {
$lastLogon = $user.LastLogon
}
} catch {
Write-Error "Failed to retrieve LastLogon from $dc for $userName $_"
}
}
if ($lastLogon -ne 0) {
return [DateTime]::FromFileTime($lastLogon)
} else {
return $null
}
}

Function to get last sign-in date from Azure AD using User ID

function Get-UserLastSignIn {
param (
[string]$userId,
[hashtable]$headers
)
try {

Get the user's sign-in activity using userId

$userInfo = Invoke-RestMethod -Uri "https://graph.microsoft.com/v1.0/users/$userId?$select=signInActivity" -Headers $headers
if ($userInfo.signInActivity -and $userInfo.signInActivity.lastSignInDateTime) {

Return the lastSignInDateTime

return [DateTime]::Parse($userInfo.signInActivity.lastSignInDateTime)
} else {
Write-Warning "No sign-in activity available for user with ID $userId."
return $null
}
} catch {
Write-Error "Failed to retrieve sign-in data for user with ID $userId $_"
return $null
}
}

Microsoft 365 and Office | Subscription, account, billing | For business | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2024-08-30T23:38:52+00:00

Hello vegka pandit,

Good day. Thank you for posting in the community!

Sincere apologies for any inconvenience this issue might be causing you. Following the description you have provided; I understand that you are facing an error when running a PowerShell script using mggraph. We mainly focus on the basics of Outlook and would like to provide guidance on your query.

For questions specifically related to PowerShell, I recommend posting your concerns in the appropriate community. You can find expert support in the "PowerShell" section in Microsoft PowerShell Community Experts in this category have extensive knowledge of PowerShell and can provide customized solutions to unique problems.

I apologize for redirecting you to a different community, but I believe this step will ensure faster and more accurate assistance for your specific scenario.

Thank you for your cooperation and understanding. Feel free to post your questions in the Microsoft Community and we will always do our best to help you.

Sincerely,

Md Ashabul Haque | Microsoft Community Moderator

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful