Powershell拥有AD对象的所有权

我有一个用户,计算机和群组的列表,随机的人是在AD的所有者。 我想清理它们出于安全原因,只是使域pipe理员的所有这些对象的所有者。 有人可以帮助一个PowerShell脚本?

我谷歌search没有任何运气。 我发现这个旧的代码,但它似乎并没有工作,不断得到一个错误的所有者。 作为域pipe理员,win10机器运行。

Param ( [parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][string]$Identity, [parameter(Position=1,Mandatory=$true,ValueFromPipeline=$true)][string]$Owner ) try { $oADObject = Get-ADObject -Filter { (Name -eq $Identity) -or (DistinguishedName -eq $Identity) }; $oAceObj = Get-Acl -Path ("ActiveDirectory:://RootDSE/" + $oADObject.DistinguishedName); } catch { Write-Error "Failed to find the source object."; return; } try { $oADOwner = Get-ADObject -Filter { (Name -eq $Owner) -or (DistinguishedName -eq $Owner) }; $oNewOwnAce = New-Object System.Security.Principal.NTAccount($oADOwner.Name); } catch { Write-Error "Failed to find the new owner object."; return; } try { $oAceObj.SetOwner($oNewOwnAce); Set-Acl -Path ("ActiveDirectory:://RootDSE/" + $oADObject.DistinguishedName) -AclObject $oAceObj; } catch { $errMsg = "Failed to set the new new ACE on " + $oADObject.Name; Write-Error $errMsg; } 

例如Running .\set-adowner.ps1 -Identity "RANDOMUSER" -Owner "domain admins"

也想通过一个txt文件运行所有的对象的samaccountnames,一旦我得到的基本脚本运行。

谢谢你的帮助,弗雷德

一位同事回答我的问题,对于任何感兴趣的人:

 Param ( [parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][string]$Owner ) $Identities = Import-Csv .\identities.csv foreach ($obj in $Identities) { $Identity = $obj.sAMAccountName; Write-Host "Setting ownership for $Identity..." #Get the object of the identity (group, user, computer account, etc.) you want to change $oADObject = Get-ADObject -Filter { (sAMAccountName -eq $Identity) -or (sAMAccountName -eq $Identity) } -properties sAMAccountName; $oAceObj = Get-Acl -Path ("ActiveDirectory:://RootDSE/" + $oADObject.DistinguishedName); #Get the object of the account you want to take ownership of the object above $oADOwner = Get-ADObject -Filter { (sAMAccountName -eq $Owner) -or (sAMAccountName -eq $Owner) } -properties sAMAccountName; $oNewOwnAce = New-Object System.Security.Principal.NTAccount($oADOwner.sAMAccountName); #Set owner of object $oAceObj.SetOwner($oNewOwnAce); Set-Acl -Path ("ActiveDirectory:://RootDSE/" + $oADObject.DistinguishedName) -AclObject $oAceObj; }