【问题标题】:PowerShell Add 1 day to the AccountExpire attribute of an AD userPowerShell 为 AD 用户的 AccountExpire 属性添加 1 天
【发布时间】:2012-02-10 09:38:44
【问题描述】:

如主题所述。如何为选定的用户帐户多放 1 天。

我知道 AD 按 Windows 文件时间。有谁知道最简单,代码编写最少的方法吗?

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    您可以通过Set-ADUser cmdlet included in Windows Server 2008 R2修改AD用户的accountExpires属性:

    Import-Module activedirectory
    
    $expireDate = (Get-ADUser -Identity "John Appleseed" -Properties accountExpires).accountExpires
    $renewedExpireDate = ([System.DateTime]::FromFileTime($expireDate)).AddDays(1)
    
    Set-ADUser -Identity "John Appleseed" -AccountExpirationDate $renewedExpireDate
    

    正如您所说,accountExpires 属性的值表示为Windows file time,它是一个 64 位整数。在此示例中,我们将其转换为 DateTime 以轻松修改它,然后将其传递给 -AccountExpirationDate 参数以更新用户。

    【讨论】:

    • 如果您在 FromFileTime 方法中将 $expireDate 替换为 $expireDate.accountExpires 则有效。 $expireDate 是 ADUser 而不是 DateTime。你能改变吗?
    • @JPBlanc 我更正了示例代码。感谢您指出这一点。
    【解决方案2】:

    使用Quest AD module:

    Set-qaduser <username> -AccountExpires ( [datetime]( get-qaduser <username> -IncludeAllProperties ).AccountExpires ).AddDays(1)
    

    【讨论】:

      【解决方案3】:

      这也可以在多域环境中通过添加 -server 开关并传入域字符串(即“domain.corp.root”)来实现。我还必须移动 .accountExpires,因为它在此代码示例中的位置错误。感谢您提供这个,这正是我所需要的。

      Import-Module ActiveDirectory
      
      $expireDate = (Get-ADUser -Identity "samaccountname" -Properties accountExpires -server "domain.corp.root")
      
      $renewedExpireDate = ([System.DateTime]::FromFileTime($expireDate.accountExpires)).AddDays(1)
      
      Set-ADUser -Identity "samaccountname" -AccountExpirationDate $renewedExpireDate -server "domain.corp.root"
      

      【讨论】:

        猜你喜欢
        • 2015-11-05
        • 1970-01-01
        • 2020-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-09
        • 1970-01-01
        相关资源
        最近更新 更多