(([datetime]::FromFileTime((Get-ADUser –Identity username -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed"))-(Get-Date)).Days
将输出 AD 帐户到期之前的时间量。然而,当我尝试通过设置一个等于它的变量将其放入脚本中时,它会崩溃并快速关闭,并且不起作用。我不确定为什么。
编辑:
仅在我的脚本中运行此命令,出现错误:
$Expiration = (([datetime]::FromFileTime((Get-ADUser –Identity 'MyTestUser' -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed"))-(Get-Date)).Days
错误:
At C:\Users\ajstepanik\Desktop\test.ps1:1 char:161
+ ... iryTimeComputed"))-(Get-Date)).Days
+ ~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
At C:\Users\ajstepanik\Desktop\test.ps1:1 char:181
+ ... Get-Date)).Days
+ ~
Missing closing ')' in expression.
At C:\Users\ajstepanik\Desktop\test.ps1:1 char:181
+ ... Get-Date)).Days
+ ~
Missing ')' in method call.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx
ception
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
最佳答案
当您说尝试将其设置为变量时,您指的是值吗?我们可以通过在代码之前放置一个变量来将变量分配给结果值,如下所示
$Expiration = (([datetime]::FromFileTime((Get-ADUser –Identity 'MyTestUser' -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed"))-(Get-Date)).Days
如果你想让它成为一个函数,你可以这样做,并执行相同的操作,但调用函数而不是代码
function Get-PasswordExpirationDays ($User)
{
(([datetime]::FromFileTime((Get-ADUser –Identity $User -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed"))-(Get-Date)).Days
}
$Expiration = Get-PasswordExpirationDays 'MyTestUser'
两者都会将 $Expiration 设置为一个人的密码过期的天数,但该功能将更容易多次使用。
关于powershell - 密码过期前的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25775031/