如何创建 PowerShell 函数以从 AD 回收站删除帐户

问题描述 投票:0回答:1

谁能告诉我下面的代码有什么问题吗?我正在尝试清理 AD 中已删除的帐户。

Function Del-User() {
    $To_Be_Deleted = (Get-ADObject -Filter 'isDeleted -eq $True -and -not (isRecycled -eq $true) -and name -ne "Deleted Objects" -and lastKnownParent -eq "CN=Users,DC=example,DC=local"'-includeDeletedObject).DistinguishedName

    foreach ($d in $To_Be_Deleted) {
        Remove-ADObject -Identity $d
    }
}

Del-User

当我将输出直接通过管道传输到

Remove-ADObject
时,它可以工作,但是当我尝试使用该函数时,它不起作用,并且出现以下错误:

在 C:\Users dministrator.example\Documents\del.ps1:18 char:9
+ 删除-ADObject -Identity $d
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (CN=用户名...示例,DC=本地:ADObject) [删除-ADObject], ADIdentityNotFoundException
    +FullyQualifiedErrorId:找不到目录对象,Microsoft.ActiveDirectory.Management.Commands.RemoveADObject

删除-ADObject:找不到目录对象
powershell active-directory
1个回答
0
投票

尝试: 删除-ADObject -Identity $d -IncludeDeletedObjects

https://learn.microsoft.com/en-us/powershell/module/activedirectory/remove-adobject?view=windowsserver2022-ps

删除-ADObject [-如果什么] [-确认] [-验证类型] [-凭证] [-身份] [-包括已删除的对象] [-分割 ] [-递归] [-服务器 ] []

© www.soinside.com 2019 - 2024. All rights reserved.