在 PowerShell 中处理 AD 用户 LastLogonDate 和当前日期
Handling AD user LastLogonDate & current date in PowerShell
我正在尝试根据 LastLogondate 从 AD 获取用户信息,并根据 LastLogonDate 和当前日期计算出用户登录后的天数。
这是脚本
$Result = Get-ADUser -filter {SamAccountName -eq 'username'} -Properties UserPrincipalName, mail, LastLogonDate, Enabled
$CurrentDate = (Get-Date)
Write-Host $Result.LastLogonDate
try
{
$LastLogonDays = ($CurrentDate - $Result.LastLogonDate).Days
Write-Host $LastLogonDays
$Json = @{
"UserPrincipalName"= $Result.UserPrincipalName;
"mail"= $Result.mail;
"LastLogonDate"= ($Result.LastLogonDate).ToString();
"days"= $LastLogonDays
}
$User = $Json
}
catch{
$Message = $_.Exception.Message
}
$JSONOutput = @{"result"=$User;"error"=$Message} | ConvertTo-Json #-Compress
Write-Output $JSONOutput
我得到了预期的结果,但日志中也捕获了错误,无法弄清楚why/how发生了错误
8/18/2021 4:16:45 AM
61
{
"error": "Cannot find an overload for \"op_Subtraction\" and the argument count: \"2\".",
"result": {
"LastLogonDate": "8/18/2021 4:16:45 AM",
"mail": "user@user.com",
"UserPrincipalName": "UserPrincipalName",
"days": 61
}
}
解释您收到该错误的原因:
PS > [datetime]::Now - $null
Cannot find an overload for "op_Subtraction" and the argument count: "2".
这意味着,这个用户或这个用户之前的用户在他们的LastLogonDate
属性。请注意,我说的是 users before this one 因为你处理代码的方式,变量 $Message
上的值永远不会被清除,你总是添加它的这里的值:
$JSONOutput = @{"result"=$User;"error"=$Message}
现在,作为替代,这就是我个人处理代码的方式。请注意,在这种情况下我没有使用 try { } catch { }
块,因为我觉得不需要它。
$ErrorActionPreference = 'Stop'
$CurrentDate = [datetime]::Now
$userName = 'john.doe'
# UserPrincipalName and Enabled are not needed. Both are default properties returned by Get-ADUser
$Result = Get-ADUser -LDAFilter "(SamAccountName=$userName)" -Properties mail, LastLogonDate
if(-not $Result)
{
throw "No user found with sAMAccountName $userName"
}
Write-Host $Result.LastLogonDate
$Json = @{
UserPrincipalName = $Result.UserPrincipalName
mail = $Result.mail
}
if($Result.LastLogonDate)
{
$LastLogonDays = ($CurrentDate - $Result.LastLogonDate).Days
Write-Host $LastLogonDays
$Json.LastLogonDate = $Result.LastLogonDate.ToString()
$Json.Days = $LastLogonDays
}
else
{
$Json.Error = "Null value on LastLogonDate for User {0}" -f $Result.Name
}
$Json | ConvertTo-Json
我正在尝试根据 LastLogondate 从 AD 获取用户信息,并根据 LastLogonDate 和当前日期计算出用户登录后的天数。
这是脚本
$Result = Get-ADUser -filter {SamAccountName -eq 'username'} -Properties UserPrincipalName, mail, LastLogonDate, Enabled
$CurrentDate = (Get-Date)
Write-Host $Result.LastLogonDate
try
{
$LastLogonDays = ($CurrentDate - $Result.LastLogonDate).Days
Write-Host $LastLogonDays
$Json = @{
"UserPrincipalName"= $Result.UserPrincipalName;
"mail"= $Result.mail;
"LastLogonDate"= ($Result.LastLogonDate).ToString();
"days"= $LastLogonDays
}
$User = $Json
}
catch{
$Message = $_.Exception.Message
}
$JSONOutput = @{"result"=$User;"error"=$Message} | ConvertTo-Json #-Compress
Write-Output $JSONOutput
我得到了预期的结果,但日志中也捕获了错误,无法弄清楚why/how发生了错误
8/18/2021 4:16:45 AM
61
{
"error": "Cannot find an overload for \"op_Subtraction\" and the argument count: \"2\".",
"result": {
"LastLogonDate": "8/18/2021 4:16:45 AM",
"mail": "user@user.com",
"UserPrincipalName": "UserPrincipalName",
"days": 61
}
}
解释您收到该错误的原因:
PS > [datetime]::Now - $null
Cannot find an overload for "op_Subtraction" and the argument count: "2".
这意味着,这个用户或这个用户之前的用户在他们的LastLogonDate
属性。请注意,我说的是 users before this one 因为你处理代码的方式,变量 $Message
上的值永远不会被清除,你总是添加它的这里的值:
$JSONOutput = @{"result"=$User;"error"=$Message}
现在,作为替代,这就是我个人处理代码的方式。请注意,在这种情况下我没有使用 try { } catch { }
块,因为我觉得不需要它。
$ErrorActionPreference = 'Stop'
$CurrentDate = [datetime]::Now
$userName = 'john.doe'
# UserPrincipalName and Enabled are not needed. Both are default properties returned by Get-ADUser
$Result = Get-ADUser -LDAFilter "(SamAccountName=$userName)" -Properties mail, LastLogonDate
if(-not $Result)
{
throw "No user found with sAMAccountName $userName"
}
Write-Host $Result.LastLogonDate
$Json = @{
UserPrincipalName = $Result.UserPrincipalName
mail = $Result.mail
}
if($Result.LastLogonDate)
{
$LastLogonDays = ($CurrentDate - $Result.LastLogonDate).Days
Write-Host $LastLogonDays
$Json.LastLogonDate = $Result.LastLogonDate.ToString()
$Json.Days = $LastLogonDays
}
else
{
$Json.Error = "Null value on LastLogonDate for User {0}" -f $Result.Name
}
$Json | ConvertTo-Json