首页 > 解决方案 > 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

标签: powershelldomaincontroller

解决方案


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


推荐阅读