Forum Discussion

zac's avatar
zac
Copper Contributor
Nov 29, 2024
Solved

Microsoft Graph Sign in Log Script

Hi all,

 

I'm trying to create a script that will check sign ins based on the location. How ever the location always appears as 'Microsoft.Graph.PowerShell.Models.MicrosoftGraphSignInLocation'.

I am able to see the location if I select the property 'location' by itself and expand the property, but then that only show's a list of the locations. If I add other properties, it either doesn't work or it displays like this:

I tried exporting as a csv and the location column values showed up as 'Microsoft.Graph.PowerShell.Models.MicrosoftGraphSignInLocation'.

This is what I have currently:

Get-MgAuditLogSignIn -Filter "location/countryOrRegion eq 'AU'" -Top 10 | format-list

 

And if I try selecting properties (I would add more properties later, this is just an example:

 

$properties = 'location, userprincipalname'
Get-MgAuditLogSignIn -All -Filter "location/countryOrRegion eq 'AU'" -Top 10 -Property $properties | Select -ExpandProperty $properties

 

Has anyone tried something similar?

  • Hi Zac,

    you can display the location with the other properties by adding it as an expression. You call the respective property with Location.<property>. This works with all properties where the value is an object, i.e. starts with Microsoft.Graph.PowerShell.Models.

    Example:
    Get-MgAuditLogSignIn | Select-Object -First 1 -Property CreatedDateTime, AppDisplayName, ClientAppUsed, IPAddress, @{Name=“City”;Expression={$_.Location.City}}, @{Name=“Country/Region”;Expression={$_. Location.CountryOrRegion}}, @{Name=“State”;Expression={$_.Location.State}}, @{Name=“ErrorCode”;Expression={$_.Status.ErrorCode}}, @{Name=“FailureReason”;Expression={$_.Status.FailureReason}}

    To know which properties exist for an object, you can display the members of the object as follows:

    $signIn = Get-MgAuditLogSignIn | Select-Object -First 1
    $signIn.Location | Get-Member

    Among other things, the properties with the MemberType property are displayed here.

    I hope this is what you meant.

  • dremba's avatar
    dremba
    Copper Contributor

    Hi Zac,

    you can display the location with the other properties by adding it as an expression. You call the respective property with Location.<property>. This works with all properties where the value is an object, i.e. starts with Microsoft.Graph.PowerShell.Models.

    Example:
    Get-MgAuditLogSignIn | Select-Object -First 1 -Property CreatedDateTime, AppDisplayName, ClientAppUsed, IPAddress, @{Name=“City”;Expression={$_.Location.City}}, @{Name=“Country/Region”;Expression={$_. Location.CountryOrRegion}}, @{Name=“State”;Expression={$_.Location.State}}, @{Name=“ErrorCode”;Expression={$_.Status.ErrorCode}}, @{Name=“FailureReason”;Expression={$_.Status.FailureReason}}

    To know which properties exist for an object, you can display the members of the object as follows:

    $signIn = Get-MgAuditLogSignIn | Select-Object -First 1
    $signIn.Location | Get-Member

    Among other things, the properties with the MemberType property are displayed here.

    I hope this is what you meant.

    • zac's avatar
      zac
      Copper Contributor

      Hi Dremba,

      Thank you, that worked!

  • Breezey's avatar
    Breezey
    Copper Contributor

    I am having the same issue. All I can see is the IP address but an actual location would be great.

Resources