powershell - 密码过期前的天数

标签 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

最佳答案

当您说尝试将其设置为变量时,您指的是值吗?我们可以通过在代码之前放置一个变量来将变量分配给结果值,如下所示

$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/

相关文章:

创建 Visual Studio 项目模板扩展 Zip 问题的 PowerShell 脚本

powershell Convert-tojson 未给出最佳结果

powershell - PowerShell 中计算机的 NetBIOS 域

Powershell Invoke-Command 远程手动工作,但不是来自 Jenkins

Powershell 阻止 UTF8 转换

Azure AD 读取扩展属性

java - 在jsp中读取Active Directory用户名

Powershell 主目录未在文件服务器文件系统上创建

powershell - "GetAuthorizationGroups"从 Active Directory 读取用户组时抛出异常

python 打开新的控制台窗口而不是打印到 cmd