【发布时间】:2021-07-27 14:28:21
【问题描述】:
目前正在构建一个脚本,该脚本从 AzureAD 收集来宾,并为我们提供自上次登录以来的天数。
当我单独运行 [int]**** 行时,它工作正常,但是当我在 ForEach 中运行时,我得到了错误
At line:8 char:5
+ [int]$LogonAge = [math]::Round(($Today - ($LastLogon.CreatedDateT ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
脚本如下:
$Users = Get-AzureADUser | Where-Object {$_.UserType -eq 'Guest' -and $_.AccountEnabled -eq 'True' } | Select AccountEnabled,DisplayName,ObjectID,UserType
$Today = Get-Date
$DaysOld = 30
ForEach ($User in $Users) {
$ObjectID = $User.ObjectId
$LastLogon = Get-AzureADAuditSignInLogs -Top 1 -Filter "UserID eq '$ObjectID'" | select CreatedDateTime
[int]$LogonAge = [math]::Round(($Today - ($LastLogon.CreatedDateTime).ToDateTime($null)).TotalDays)
if ($LogonAge -gt 30) {
Set-AzureADUser -ObjectID $user.ObjectID -AccountEnabled $true
}
}
知道我做错了什么吗?
【问题讨论】:
-
修改为显示完整代码。
标签: powershell