Forum Discussion
MartinLangof1285
Jun 28, 2022Copper Contributor
Creating Script to extract last Sing In signInActivity from excluded Users from Azure CA
Dear Community
I try to create a Script what is fetching me out the last signInActivity
and here is the place where I stuck and have no chance to coming forward
(I paste just the necessary columns from the Script $CustomObject is for another Excel Sheet )
##here I fetch the excluded Users from the Group and Store the Value into the Variable Member##
$ExcludeGroupMembers = @()
$ExcludeGroupMembers = foreach ($Object in ($CAPolicies.excludeGroupsId | Select-Object -Unique)) {
Write-Verbose -Verbose -Message "Getting exclude group members for policy $($Policy.displayName)..."
$Uri = "https://graph.microsoft.com/beta/groups/$Object"
$GroupName = { (Get-MsGraph -AccessToken $AccessToken -Uri $Uri -ErrorAction Stop).displayName }
$Uri = "https://graph.microsoft.com/beta/groups/$Object/members"
$Members = (Get-MsGraph -AccessToken $AccessToken -Uri $Uri).userPrincipalName| Sort-Object userPrincipalName
$CustomObject = New-Object -TypeName psobject
$CustomObject | Add-Member -MemberType NoteProperty -Name "Group" -Value $GroupName
$CustomObject | Add-Member -MemberType NoteProperty -Name "Members" -Value $Members
so far so good this works User Objects are stored into the Variable my next step here is to
foreach the Members but here i stuck I allways get an error :
(404) Not Found. In Zeile:16 Zeichen:13 + $Response = Invoke-WebRequest -Method GET -Uri $ApiUrl -ContentType " ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
###########################################
If I try it with a single User, for example email address removed for privacy reasons
https://graph.microsoft.com/beta/users/email address removed for privacy reasons?`$select=displayName,userPrincipalName,signInActivity,userType,assignedLicenses&`$top=999
it works and also when I try to select over all the User also like here:
But if i just store the User into a Variable like
$TheUser = "email address removed for privacy reasons"
i get an Error and also when I wanna use the Varible $Members from above
i get the error
So this would be the Lines I try to run to get just the Selected Values from the Variable $Members
foreach ($Member in ($Members | Select-Object -Unique)) {
$ApiUrl = "https://graph.microsoft.com/beta/users/Member?
$select=displayName,userPrincipalName,signInActivity,userType,assignedLicenses"
$Result = @()
While ($ApiUrl -ne $Null) #Perform pagination if next page link (odata.nextlink) returned.
{
$Response = Invoke-WebRequest -Method GET -Uri $ApiUrl -ContentType "application\json" -Headers $headers | ConvertFrom-Json
}
if($Response.value)
{
$Users = $Response.value
ForEach($User in $Members)
{
$Result += New-Object PSObject -property $([ordered]@{
DisplayName = $User.displayName
UserPrincipalName = $User.userPrincipalName
LastSignInDateTime = if($User.signInActivity.lastSignInDateTime) { [DateTime]$User.signInActivity.lastSignInDateTime } Else {$null}
IsLicensed = if ($User.assignedLicenses.Count -ne 0) { $true } else { $false }
IsGuestUser = if ($User.userType -eq 'Guest') { $true } else { $false }
})
}
}
$ApiUrl=$Response.'@odata.nextlink'
}
$Result | Export-CSV "C:\LastLoginDateReport.CSV" -NoTypeInformation -Encoding UTF8
Would be really happy for any advice here because here i stuck now for a long Time
Kind Regards,
Martin
No RepliesBe the first to reply