简体   繁体   中英

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://docs.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

这帮助我Install-Module AzureADPreview -AllowClobber -Force

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM