删除用户和“服务器返回的推荐”

时间:2015-03-10 03:03:04

标签: powershell active-directory

我正在编写一个脚本,用于搜索具有特定属性组合的AD用户,并删除它们。当我使用“company.local”作为“-Server”参数时,整个脚本都有效。当我选择2008R2 DC而不是删除对象时,我收到以下错误:

Remove-ADUser : A referral was returned from the server At line:1 char:1
+ Remove-ADUser $user
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : ResourceUnavailable: (CN=John Doe...mpany,DC=local:ADUser) [Remove-ADUser], ADRefer   ralException    + FullyQualifiedErrorId : ActiveDirectoryServer:8235,Microsoft.ActiveDirectory.Management.Commands.RemoveADUser

是什么给出的?感谢。

以下是相关代码: 获得DC:

Function Get-DomainControllerName {
    Param (
        [Parameter(Mandatory=$True)]
        $DNSDomainName
    )

    $domainControllers = Get-ADDomainController -Filter * -Server $DNSDomainName

    Foreach ($domainController in $domainControllers) {
        If ($domainController.OperatingSystem -like '*2003*') {
            Write-Warning ("Skipping {0}, because it is not Server 2008 (or higher)." -f $domainController.HostName)
        }
        Else {
            $dc = $domainController.HostName
        }
    }

    Write-Output ("Using {0} to connect to Active Directory." -f $dc) | Out-File -FilePath $outputPath\$OutputFileName -Append
    Return $dc
}

获取用户:

$allDisabledUsers = Get-ADUser -Filter * -Properties ExtensionAttribute4,msExchUserAccountControl -Server $dc

删除用户:

Foreach ($user in $allDisabledUsers) {
    Remove-ADObject $user -Server $dc
}

1 个答案:

答案 0 :(得分:0)

您使用的是服务器的FQDN吗?即 -Server "DOMAINCONTROLLER.domain.local"