Nor*_*rec 7 powershell active-directory domain-controller
我正在尝试对 AD 进行 PowerShell 搜索,以仅查找在过去 30 天内登录的计算机(而不是服务器或其他计算机)。除了 30 天的限制之外,我已经编写了大部分脚本。任何帮助将不胜感激。
Get-ADComputer -Filter * -Properties * | FT 名称、操作系统、LastLogonDate -Autosize | 外文件 C:\Temp\ComputerLastLogonDate.csv
Rya*_*ies 15
Get-ADComputer -Filter * -Properties *
只获取您打算使用的属性......它更有效。当您并不真正需要所有属性时,检索域中所有计算机的所有属性对域控制器的要求是不必要的。很浪费。
Get-ADComputer -Filter * -Properties OperatingSystem, LastLogonDate更好,因为您不需要所有属性。(始终包含“名称”属性。)
| FT 名称、操作系统、LastLogonDate -Autosize
直到最后才格式化输出。换句话说,Format-Table 和 Format-List 应该是数据通过管道传输到的整个 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 属性。
允许域控制器向您返回过滤后的集合,而不是所有计算机的完整集合,这意味着通过网络传输的数据更少,PowerShell 需要处理的数据更少。
另请注意,您将不得不处理以下计算机:
小智 0
这应该会让你开始朝着你想要的方向前进。
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19053 次 |
| 最近记录: |