简体   繁体   English

如何通过PowerShell从Active Directory中删除终止的经理的DirectReports

[英]How to remove terminated manager's DirectReports from Active Directory through PowerShell

I created a script to clear terminated user's manager in Active Directory. 我创建了一个脚本来清除Active Directory中终止的用户的管理员。 But want to remove his direct reportees through PowerShell 但是想通过PowerShell删除他的直接下属

The Reports attribute is a linked attribute, and its forward link is the Manager attribute. Reports属性是链接属性,其前向链接是Manager属性。

Remove (or replace) the manager in the Manager attribute of the users and the Reports values will disappear automatically 删除(或替换)用户“ Manager属性中的Manager ,“ Reports值将自动消失

I use this script to clear Direct Reports from all users in a specific OU. 我使用此脚本清除特定OU中所有用户的直接报告。 It creates a list of the Manager's direct reports, and then loops through that list and nulls the Manager property. 它创建了Manager的直接报告的列表,然后遍历该列表并使Manager属性为空。 Run the script with -WhatIf to see the accounts that will be affected. 使用-WhatIf运行脚本以查看将受到影响的帐户。

$TSManagerList = (Get-ADUser -Filter * -SearchBase "OU=Tombstone,DC=Contoso" -Properties directreports, description | where{$_.directreports -ne ""}).samaccountname | sort

foreach($TSManager in $TSManagerList)
{
    $DirReportList = (Get-ADUser $TSManager -Properties directreports).directreports
    foreach($DirReport in $DirReportList)
    {
        $DirReportSam = (Get-ADUser -Filter * | where{$_.distinguishedname -eq $DirReport}).samaccountname
        Set-ADUser -Identity $DirReportSam -Manager $null -WhatIf 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM