密码过期的天数

时间:2014-09-10 21:01:34

标签: powershell active-directory

(([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

1 个答案:

答案 0 :(得分:3)

当你说你试图将它设置为变量时,你的意思是值吗?我们可以通过在代码之前放置一个变量来为结果值赋值,如此

$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设置为人员密码过期的天数,但该功能将更容易多次使用。