【问题标题】:Select all first level OUs选择所有第一级 OU
【发布时间】:2018-07-18 01:20:54
【问题描述】:

我目前正在创建一个 PowerShell 脚本来扫描 Active Directory 中去年未登录的用户。

Import-Module ActiveDirectory

$DaysInactive = 365
$InactiveDate = (Get-Date).AddDays(-($DaysInactive))

$Users = Get-ADUser -SearchScope OneLevel -SearchBase "ou=staff,ou=brummitt,dc=DUNELAND,dc=LOCAL" -Filter { LastLogonDate -lt $InactiveDate } -Properties LastLogonDate |
         Select-Object @{Name="Username";Expression={$_.SamAccountName}},
             Name, LastLogonDate, DistinguishedName

$Users | Export-Csv C:\Temp\InactiveUsers.csv -NoTypeInformation

如果您看到 users 变量,您会看到其中有一个学校名称和一个教职员工 ou。我们对我们地区的所有建筑物都有这个约定。如何以员工 OU 为第二级扫描所有第一级 OU?

我尝试将 Searchbase 更改为此 -SearchBase "ou=staff,ou=*,dc=DUNELAND,dc=LOCAL",但收到此错误:

Get-ADUser:找不到目录对象 在行:6 字符:10 + $Users = Get-ADUser -SearchScope OneLevel -SearchBase "ou=staff,ou=*,dc=DUNELAND ... + > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (:) [Get-ADUser], ADIdentityNotFoundException + FullyQualifiedErrorId:找不到目录对象,Microsoft.ActiveDirectory.Management.Commands.GetADUser

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    您不能在SearchBase DN 中指定通配符,但您可以这样做:

    1. 查询根目录下的所有OU
    2. 在每个一级 OU 中查询“员工”OU
    3. 为用户查询每个员工 OU

    类似:

    # 1. Find the first-level OU's
    $LevelOne = Get-ADOrganizationalUnit -Filter * -SearchScope OneLevel
    
    # 2. Find the staff OU's
    $StaffOUs = $LevelOne |ForEach-Object {
        Get-ADOrganizationalUnit -Filter "Name -like 'Staff'" -SearchBase $_.DistinguishedName -SearchScope OneLevel -ErrorAction SilentlyContinue
    }
    
    # 3. Query each staff OU
    $StaffOUs |ForEach-Object {
        Get-ADUser -SearchScope OneLevel -SearchBase $_.DistinguishedName -Filter { LastLogonDate -lt $InactiveDate } -Properties LastLogonDate |
        Select-Object @{Name="Username";Expression={$_.SamAccountName}},
            Name, LastLogonDate, DistinguishedName
    } |Export-Csv C:\Temp\InactiveUsers.csv -NoTypeInformation
    

    【讨论】:

      猜你喜欢
      • 2016-03-16
      • 2015-02-25
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2016-08-22
      • 2011-04-30
      相关资源
      最近更新 更多