首页 > 解决方案 > Azure AD Powershell:提取用户的上次登录时间

问题描述

我试图在我们的 Active Directory 上提取用户的上次登录时间,我发现了这个脚本,它应该可以解决问题:

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'
    }

但由于某种原因,Powershell 似乎无法识别“Get-AzureAdAuditSigninLogs”输入,即使根据 technet 的正确模块是我在脚本开头安装的“AzureADPreview”: https ://docs.microsoft .com/en-us/powershell/module/azuread/get-azureadauditsigninlogs?view=azureadps-2.0-preview

你知道我是否需要任何其他模块来运行这个脚本?是否有其他方法可以获取此信息?我需要一个包含所有用户及其最后登录时间的 CSV 文件。

谢谢您的帮助。

干杯,

加布

编辑:这是错误消息:

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

标签: azurepowershell

解决方案


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


推荐阅读