问题描述
我想运行一个查询的7天内到期的帐户的PowerShell脚本,我现在有
I am trying to run a powershell script that queries for accounts that expire within 7 days, I currently have
$ A =(获得最新).AddDays(7);搜索-ADAccount -AccountExpiring -TimeSpan7|选择-对象SAM帐户名,AccountExpirationDate |排序对象AccountExpirationDate |出口-CSV 7_days.csv
然而,当我做如下改变,它似乎有一些麻烦,我最终得到一个空的CSV文件。最后,我想7天,而不是更多,帐户到期而不是更少。
However when I make the following change, it seems to have some trouble and I end up getting an empty CSV file. Ultimately I want account expiring in 7 days, not more, not less.
$ A =(获得最新).AddDays(7);搜索-ADAccount -AccountExpiring -TimeSpan7|选择-对象SAM帐户名,AccountExpirationDate |排序对象AccountExpirationDate |位置对象 {$ _ AccountExpirationDate般的$一个} |出口-CSV 7_days.csv
有人可以让我知道我做错了什么?我曾尝试移动位置对象{$ _。AccountExpirationDate般的$ A}一片围绕,或-match,而不是般的,但这些没有带落在了我很多的成功。我要去哪里错了呢?
Can someone let me know what I am doing wrong? I have tried moving the "Where-Object {$_.AccountExpirationDate -like $a } " piece around, or "-match" instead of "-like" , however these havn't landed me much success. Where am I going wrong with this?
推荐答案
更新:你可以得到帐户如果你传递一个字符串值,传递一个整数初始化的时间跨度为7蜱
Update: You can get the accounts if you pass a string value, passing an integer initializes the timespan to 7 ticks!
Search-ADAccount -AccountExpiring -TimeSpan "7"
其他有效选项:
other valid options:
Search-ADAccount -AccountExpiring -TimeSpan (New-TimeSpan -Days 7)
Search-ADAccount -AccountExpiring -TimeSpan ([TimeSpan]::FromDays(7))
可能是一个错误,它并没有为我工作,以及。这里有一个解决方法:
Could be a bug, it doesn't work for me as well. Here's a workaround:
$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 }
这篇关于PowerShell中查找用户过期7天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!