【问题标题】:Powershell find users expiring in 7 daysPowershell 查找 7 天后到期的用户
【发布时间】:2012-05-02 02:30:11
【问题描述】:

我正在尝试运行一个 powershell 脚本来查询在 7 天内过期的帐户,我目前有

$a = (get-date).AddDays(7) ;搜索-ADAAccount-AccountExpiring -时间跨度“7”|选择对象 SamAccountName,AccountExpirationDate |排序对象 AccountExpirationDate |导出-Csv 7_days.csv

但是,当我进行以下更改时,似乎遇到了一些问题,我最终得到了一个空的 CSV 文件。最终,我希望帐户在 7 天后到期,不多也不少。

$a = (get-date).AddDays(7) ;搜索-ADAAccount-AccountExpiring -时间跨度“7”|选择对象 SamAccountName,AccountExpirationDate |排序对象 AccountExpirationDate | Where-对象 {$_.AccountExpirationDate 类似 $a } |导出-Csv 7_days.csv

谁能告诉我我做错了什么?我曾尝试移动 "Where-Object {$_.AccountExpirationDate -like $a } " 块,或 "-match" 而不是 "-like" ,但是这些并没有让我取得太大的成功。我哪里错了?

【问题讨论】:

    标签: powershell active-directory pattern-matching user-accounts


    【解决方案1】:

    更新:如果您传递一个字符串值,您可以获取帐户,传递一个整数会将时间跨度初始化为 7 个滴答声!

    Search-ADAccount -AccountExpiring -TimeSpan "7"
    

    其他有效选项:

    Search-ADAccount -AccountExpiring -TimeSpan (New-TimeSpan -Days 7)
    Search-ADAccount -AccountExpiring -TimeSpan ([TimeSpan]::FromDays(7))
    

    可能是一个错误,它对我也不起作用。这是一个解决方法:

    $NeverExpires = 9223372036854775807
    $ExpringIn = (Get-Date).AddDays(7) 
    
    Get-ADUser -Filter * -Properties accountExpires | 
    Where-Object {$_.accountExpires -ne $NeverExpires  -and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) -lt $ExpringIn }
    

    【讨论】:

    • 这是迄今为止最接近的答案;我还没有得到想要的结果,但由于你的帮助已经接近了: $NeverExpires = 9223372036854775807 ; $ExpringIn = (Get-Date).AddDays(7) ; foreach ($item in Get-ADUser -Filter * -Properties accountExpires | Where-Object {$_.accountExpires -ne $NeverExpires -and [datetime]::FromFileTime([int64]::Parse($_.accountExpires)) - lt $ExpringIn } | select-object SamAccountName,accountExpires) { Write-Host $item.SamAccountName ([datetime]::FromFileTime(($item.accountExpires)."msDS-UserPasswordExpiryTimeComputed"))}
    • 您好,我可以从哪里获得 cmdlet“Get-ADUser”?
    • 它是 AD 模块的一部分,它是 RSAT(远程服务器管理工​​具)的一部分。 microsoft.com/en-us/download/details.aspx?id=7887
    【解决方案2】:

    使用的属性是accountExpires,表示自1600年以来的100纳秒包

    PS C:\Windows\system32> Get-ADuser user1 -Properties accountExpires
    
    
    accountExpires    : 129821976000000000
    DistinguishedName : CN=user1 users,OU=OUTest,DC=dom,DC=fr
    Enabled           : True
    GivenName         : user1
    Name              : user1 users
    ObjectClass       : user
    ObjectGUID        : b1bef798-8e36-45ff-ad11-e79f89769efc
    SamAccountName    : user1
    SID               : S-1-5-21-3115856885-816991240-3296679909-1146
    Surname           : Users
    UserPrincipalName : user1@dom.fr
    

    您可以像这样将其转换为 [dateTime]:

    PS> [datetime](Get-ADuser user1 -Properties accountExpires).accountExpires
    
    mardi 22 mai 0412 22:00:00
    

    【讨论】:

      【解决方案3】:

      虽然这是一个旧线程..让我添加一个简短的注释和警告..

      请谨慎询问 7 天前的帐户。 7 天 2 小时不是 7 天,因此与查询不匹配(可能是您的 CSV 为空的原因)。

      因此,您将始终要说超过 7 天且少于 8 天(等等)的帐户来捕获第 7 天内的所有内容。等等……

      另外,上面的代码
      [datetime](Get-ADuser user1 -Properties accountExpires).accountExpires
      给我一个错误
      无法将值“9223372036854775807”转换为类型“System.DateTime”。错误:“刻度必须介于 DateTime.MinValue.Ticks 和 DateTime.MaxValue.Ticks 之间。 参数名称:tick"

      您还可以查看http://social.technet.microsoft.com/Forums/scriptcenter/en-US/b70113b1-a043-4543-afa0-dbba5757d035/powershell-windows-2008-getaduser-accountexpirationdate-returns-wrong-result?forum=ITCG

      【讨论】:

        【解决方案4】:

        尝试以下 PowerShell 命令

        Search-ADAccount -AccountExpiring -TimeSpan 6.00:00:00 | FT Name,ObjectClass -A
        

        https://technet.microsoft.com/en-us/library/ee617247.aspx

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-02-21
          • 1970-01-01
          • 2012-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多