在特定的OU中查找X天无效的帐户

时间:2015-09-16 14:51:21

标签: windows powershell active-directory

我正在尝试运行PowerShell脚本,该脚本将显示在x天内处于非活动状态(或未登录)的所有用户的列表。该部分很容易找到并修改脚本,但我在设置它时遇到问题,因此我只能在域中指定某些OU和子OU。这是我到目前为止所做的,但我认为我可能不得不使用另一种方法来实现这一目标:

#Import Ad Module
Import-Module ActiveDirectory

#SearchBase
$searchB = (Get-Content -Path C:\scripts\ous.txt)

#Time accounts have been inactive
$tSpan = "145"

Search-ADAccount -SearchBase $searchB -AccountInactive -UserOnly -Timepsan $tSpan |
  Where {($_.DistinguishedName -notlike "specific sub-ou I don't want to check")} |
  FT name,ObjectClass -A

文本文件的格式为:

OU=first ou,OU=Parent OU,DC= thisDC,DC=dc,DC=DC 
OU=third ou,OU=Parent OU,DC= this DC,DC=dc,DC=DC 
OU=fourthou,OU=Parent OU,DC= thisDC,DC=dc,DC=DC

当我运行时,我收到错误

  

Search-ADAccount:找不到目录对象

1 个答案:

答案 0 :(得分:1)

您的-SearchBase参数中看起来类型不匹配。

请参阅Get-Help Search-ADAccount

请注意-SearchBase的值类型为string。您的文本文件中有三个OU,因此该文件上的Get-Content将生成一个字符串数组(string[])。

由于-SearchBase参数只接受单个值,因此您需要通过OU列表foreach,一次只能输入一个OU:

foreach ($OU in $SearchB)
{
  search-adaccount -searchbase $OU -accountinactive -useronly -timepsan $tSpan.....
}