powershell - Powershell 上次登录查询。帮助设置阵列以考虑我所有的域控制器
问题描述
下面的这个脚本可以工作,但是我每次尝试让它在我的所有域控制器中循环都失败了。如何添加一个数组以通过所有域控制器上的所有这些 OU。提前致谢!
$OUs= “OU=Test1,OU=Test1,OU=Test1,OU=Test1,OU=All Users,DC=domain,DC=local",
"OU=Test2,OU=Test2,OU=Test2,OU=All Users,OU=Test2,DC=domain,DC=local",
"OU=Test3,OU=Test3,OU=Test3,OU=All Users,OU=Test3,DC=domain,DC=local",
"OU=test4,OU=test4,OU=test4,OU=All Users,OU=test4,DC=domain,DC=local",
"OU=Test5,OU=test5,OU=Test5,OU=All Users,OU=test5,DC=domain,DC=local”
$OUs | ForEach-Object
{
Get-ADUser -Filter {Enabled -eq $TRUE} -SearchBase $_ -Properties Name,SamAccountName,LastLogonDate |
Where-Object {($_.LastLogonDate -lt (Get-Date).AddDays(-7)) -and ($_.LastLogonDate -ne $NULL)}
} |
Sort LastLogonDate |
Format-Table -Property Name,SamAccountName,LastLogonDate, DistinguishedName |
Out-String
解决方案
Below you have now an array of your OUs. Please try whether that works for you now.
$OUs= @(
“OU=Test1,OU=Test1,OU=Test1,OU=Test1,OU=All Users,DC=domain,DC=local",
"OU=Test2,OU=Test2,OU=Test2,OU=All Users,OU=Test2,DC=domain,DC=local",
"OU=Test3,OU=Test3,OU=Test3,OU=All Users,OU=Test3,DC=domain,DC=local",
"OU=test4,OU=test4,OU=test4,OU=All Users,OU=test4,DC=domain,DC=local",
"OU=Test5,OU=test5,OU=Test5,OU=All Users,OU=test5,DC=domain,DC=local”
)
I would also suggest to break your line after every pipe in order to cut the line. That makes it far easier to read for you, plus your colleagues.
$OUs | ForEach-Object
{
Get-ADUser -Filter {Enabled -eq $TRUE} -SearchBase $_ -Properties Name,SamAccountName,LastLogonDate |
Where-Object {($_.LastLogonDate -lt (Get-Date).AddDays(-7)) -and ($_.LastLogonDate -ne $NULL)}
} |
Sort LastLogonDate |
Format-Table -Property Name,SamAccountName,LastLogonDate, DistinguishedName |
Out-String
推荐阅读
- c# - 如何替换 Queryable 中的列
以通用方式不执行 Queryable - python-3.x - 数字化热图并将像素映射到值
- authentication - Istio authservice oidc 无法交换访问令牌的授权码
- node.js - 运行 ng 命令时“getColorDepth 不是函数”
- tensorflow - keras中Conv1D之前的掩蔽
- c# - System.DirectoryServices.ActiveDirectory 的端口要求
- onenote - 带有 One Note Javascript API 的嵌套部分组
- image - Twilio 向 WhatsApp 发送多张图片
- parallel-processing - 使用多线程进行滚动功能
- go - 使用 golang 在 gcp 中创建实例时添加启动脚本