gpt4 book ai didi

powershell - Powershell删除OU中的所有用户

转载 作者:行者123 更新时间:2023-12-02 23:09:32 25 4
gpt4 key购买 nike

我正在尝试使用powershell删除OU中的所有用户,当涉及到SAMAccount名称时,我遇到了以下问题,我希望它删除所有找到的用户。

我在这儿做错了吗?

$search="OU=Staff,OU=Users,DC=Testing, DC=Local"

$deletes= Get-ADUser -SearchBase $search -filter * -properties SamAccountName | Select-Object SamAccountName

$numusers=($deletes.count)
echo "$numusers Users Found"

foreach ($delete in $deletes)
{
echo "Deleting user account $delete . . . "
remove-aduser -identity $delete -confirm:$false
}

这是输出。此处似乎出错了-无法转换“ @ {SamAccountName = bbonhomme}”

找到7位用户
Deleting user account @{SamAccountName=bbonhomme} . . . 
Remove-ADUser : Cannot bind parameter 'Identity'. Cannot convert value "@{SamAccountName=bbonhomme}" to type
"Microsoft.ActiveDirectory.Management.ADUser". Error: "Cannot convert the "@{SamAccountName=bbonhomme}" value of type
"Selected.Microsoft.ActiveDirectory.Management.ADUser" to type "Microsoft.ActiveDirectory.Management.ADUser"."
At C:\Users\Administrator\Desktop\import\getadusers.ps1:11 char:29
+ remove-aduser -identity @delete -confirm:$false
+ ~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.RemoveADUser

最佳答案

更换

$deletes= Get-ADUser -SearchBase $search -filter * -properties SamAccountName | Select-Object SamAccountName 

通过
$deletes= Get-ADUser -SearchBase $search -filter * -properties SamAccountName 

添加Select-Object意味着将ADUser“对象”转换为“Psobject”。因此,后续命令remove-ADuser无法识别这种类型的对象。

也替换:
remove-aduser -identity $deletes -confirm:$false

通过
remove-aduser -identity $delete.SamAccountName -confirm:$false

$ delete更改了$ deletes(您不能在-Identity参数中指定集合)

关于powershell - Powershell删除OU中的所有用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46690065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com