我正在尝试执行AD的PowerShellsearch,以查找在过去30天内已经login的计算机(而不是服务器或其他)。 我写了大部分的脚本,除了限制30天的部分。 任何帮助将不胜感激。
Get-ADComputer -Filter * -Properties * | FT名称,OperatingSystem,LastLogonDate -Autosize | Out-File C:\ Temp \ ComputerLastLogonDate.csv
Get-ADComputer -Filter * – 属性*
只得到你打算使用的属性…它更有效率。 当您不需要所有的属性时,检索域中所有计算机的所有属性对您的域控制器是不必要的。 这是浪费。
Get-ADComputer -Filter * -Properties OperatingSystem, LastLogonDate更好,因为你不需要所有的属性。 (“姓名”属性始终包含在内。)
| FT名称,OperatingSystem,LastLogonDate -Autosize
不要格式化输出,直到最后。 换句话说,Format-Table和Format-List应该是数据pipe理的整个cmdlet链中的最后一个cmdlet。
Get-ADComputer -Filter * -Properties OperatingSystem, LastLogonDate | Where { $_.LastLogonDate -GT (Get-Date).AddDays(-30) }
这有点好一些,但仍然有一些效率低下,因为您仍然在检索所有计算机的数据集…您可以让域控制器为您进行筛选。
$LastMonth = $(((Get-Date).AddDays(-30)).ToFileTime())
Get-ADComputer -LDAPFilter "(lastLogonTimeStamp>=$LastMonth)" -Properties OperatingSystem,LastLogonDate
之所以使用lastLogonTimeStamp(这是“文件时间”,而不是.NET DateTime),是因为“LastLogonDate”不是真正的LDAP属性。 LastLogonDate只是PowerShell自动为您转换lastLogonTimestamp属性的有用方法。 lastLogonTimestamp是“真实的”LDAP属性。
允许域控制器向您返回一个过滤的集合,而不是所有计算机的完整集合,这意味着有更less的数据通过networking传输,而PowerShell处理的数据更less。
另外请注意,你将不得不处理电脑:
您的命令根本不会仅过滤工作站。
您需要使用LastLogonTimeStamp字段,以便您可以轻松地对其进行过滤,然后将其转换为用于导出的DateTime。
这些信息也可以在线获得。 就像这里的这个例子,我略微修改了只返回工作站。 注意他们只查询需要的属性。 这是更高效。 另外,我不知道为什么他们有这个脚本中的$域variables。 看起来完全没用。
# Gets time stamps for all computers in the domain that have NOT logged in since after specified date # Mod by Tilo 2013-08-27 import-module activedirectory $domain = "domain.mydom.com" $DaysInactive = 90 $time = (Get-Date).Adddays(-($DaysInactive)) # Get all AD computers with lastLogonTimestamp less than our time Get-ADComputer -Filter {LastLogonTimeStamp -lt $time -and OperatingSystem -notlike "*server*"} -Properties LastLogonTimeStamp,OperatingSystem | # Output hostname and lastLogonTimestamp into CSV select-object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} | export-csv OLD_Computer.csv -notypeinformation
从这里引用: https : //gallery.technet.microsoft.com/scriptcenter/Get-Inactive-Computer-in-54feafde
另请参阅以下参考资料:
LastLogonTimeStamp属性/它是为什么devise的
将TimeStamp转换为DateTime
这应该让你开始你想要去的方向..
Get-ADComputer -Properties * -Filter { Enabled -eq $True -and OperatingSystem -like 'Windows*' -and OperatingSystem -notlike "Windows Server*" -and OperatingSystem -notlike "Windows 7*" } -SearchBase "DC=hhmtx,DC=org" | FT Name, OperatingSystem, LastLogonDate -Autosize | Out-File C:\Temp\ComputerLastLogonDate.csv
请记住,LastLogonDate是LastLogonTimeStamp的转换版本。 LastLogonTimeStamp不是实际计算机上次login时间的最准确的表示forms。 默认情况下,可能会closures14天。 更多信息 – https://social.technet.microsoft.com/wiki/contents/articles/22461.understanding-the-ad-account-attributes-lastlogon-lastlogontimestamp-and-lastlogondate.aspx
如果您想要获得更精确的上次login时间,则必须使用lastLogon属性,但不会将其复制到所有域控制器,因此您必须迭代所有域控制器以获取最新值。 您必须计算上次login时间,只有这样才能将其限制为“最后30/60/90天”。
在这里,你可以find例子如何实现这个algorithm获取用户的最后login时间:
Import-Module ActiveDirectory function Get-ADUserLastLogon([string]$userName) { $dcs = Get-ADDomainController -Filter {Name -like "*"} $time = 0 foreach($dc in $dcs) { $hostname = $dc.HostName $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon if($user.LastLogon -gt $time) { $time = $user.LastLogon } } $dt = [DateTime]::FromFileTime($time) Write-Host $username "last logged on at:" $dt } Get-ADUserLastLogon -UserName username
还有其他方法可以更容易,更快速地获得相同的结果。 您可以尝试活动目录报告工具 – AD FastReporter Free 。 它会为你做最后的login计算。 只需安装它,进入“计算机”选项卡,然后select“最近30天login的计算机”,然后按生成。 结果也包括DC服务器,但是您可以在导出为.csv,.xlsx文件后轻松删除它们。 PS我是这个工具的所有者和开发者。