Gabe
Gabe

Reputation: 41

Azure AD Powershell: Extract the User's last Logon Time

I'm trying to extract the user's last logon time on our Active Directory, and I found this script, which should do the trick:

Install-Module AzureADPreview
Import-Module AzureADPreview
$Cred = Get-Credential
Connect-MsolService -Credential $Cred
Connect-AzureAD -Credential $Cred

$Users = Get-MsolUser -all
$Headers = "DisplayName`tUserPrincipalName`tLicense`tLastLogon" >>C:\list.csv
ForEach ($User in $Users)
    {
    $UPN = $User.UserPrincipalName
    $LoginTime = Get-AzureAdAuditSigninLogs -top 1 -filter "userprincipalname eq '$UPN'" | select CreatedDateTime
    $NewLine = $User.DisplayName + "`t" + $User.UserPrincipalName + "`t" + $User.Licenses.AccountSkuId + "`t" + $LoginTime.CreatedDateTime
    $NewLine >>'C:\list.csv'
    }

But for some reason Powershell can't seem to recognize the "Get-AzureAdAuditSigninLogs" input, even though according to technet the correct Module for it is "AzureADPreview" which I install at the beginning of the script: https://learn.microsoft.com/en-us/powershell/module/azuread/get-azureadauditsigninlogs?view=azureadps-2.0-preview

Do you know If i need any other modules to run this script? Are there maybe any other ways to get to this info? I'd need a CSV-File with all users and their last LogonTime.

Thank you for your help.

Cheers,

Gabe

Edit: Here's the Error-Message:

Get-AzureAdAuditSigninLogs : The term 'Get-AzureAdAuditSigninLogs' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or 
if a path was included, verify that the path is correct and try again.
At line:12 char:18
+     $LoginTime = Get-AzureAdAuditSigninLogs -top 1 -filter "userprinc ...
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-AzureAdAuditSigninLogs:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Upvotes: 4

Views: 32124

Answers (1)

yellephen
yellephen

Reputation: 129

This helped me Install-Module AzureADPreview -AllowClobber -Force

Upvotes: 2

Related Questions