2016-05-16 39 views
0

我有以下代码行,它应该获得以@符号开头的所有Active Directory组,然后从这些组中删除用户;PowerShell - 删除ADGroup不允许从Get-ADGroup-pipeil输入

Get-ADGroup -Filter 'name -like "@*"' | Remove-ADGroup -identity [USERID] 

获取,广告组的伟大工程,成功地抓住所有以@开头的群体,但是通过管道时删除,广告组,我得到以下每个错误和每@组;

Remove-ADGroup : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. 
At line:1 char:41 
+ Get-ADGroup -Filter 'name -like "@*"' | Remove-ADGroup -identity [USERID] 
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo   : InvalidArgument: [email protected],O...ife,DC=co,DC=uk:PSObject) [Remove-ADGroup], ParameterBindingException 
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.RemoveADGroup 

我想不通为什么管道无法正常工作。

+0

这就是为什么你应该总是使用'-WhatIf'用'删除,'的cmdlet,直到你100%肯定你正在做的事情是正确的工作。这可以发生了很大的错误。 :-) –

+0

非常好的一点!感谢您指出这一点,我对PowerShell颇为陌生(正如您可能会说的那样),所以从现在开始我会记住这一点! – haze1434

回答

2

Remove-ADGroup完全删除组 - 这绝对不是你想要的。

使用Remove-ADGroupMember代替:

Get-ADGroup -Filter 'name -like "@*"' | Remove-ADGroupMember -Members [USERID] 
+0

很棒!谢谢。 – haze1434

+0

只是要确认实际上,如果上述-whatif的输出是“如果:执行操作”设置“在目标上”CN = @ [组名] OU = [OU DETAILS]“,这看起来好吗? – haze1434

+0

是的,不幸的是它没有更具体,一些cmdlet有更多有意义的消息,但是至少它说组的set-operation(修改)是正确的,如果你不确定,在一个组上运行该命令来测试第一个'get-adgroup mytestgroup |删除-adgroupmember -members someuser' –