powershell删除文本文件中列出的列出的集体成员,从中删除它们的组是通配符
我正在寻找一种删除从任何文本文件中列出的广告计算机的方法 他们可能在同一OU内的广告组。例如,文本文件包含:
Computer1 $
Computer2 $
Computer3 $
这三台计算机可能出现的组是:
Group1
Group2
Group3
我在2016年的Stackoverflow上找到了这一点,标题为“ PowerShell -remove -Adgroupmember-锁定我的管理员帐户”,并为我编辑:
Get -Adgroup -filter'名称-ike“ group*”'|删除Adgroupmember -Members“ Computer3 $”
删除“ Computer3 $!!但是,当我尝试用我的变量替换“ Computer3 $”时,它会破裂。这是我的代码:
:
$Comps = Get-Content "C:\Users\Administrator\Desktop\Complist.txt"
foreach ($comp in $comps) {
Get-ADGroup -filter 'name -like "Group*"' | Remove-ADGroupMember -Members "$Comps"
}
和:
Remove-ADGroupMember : Cannot find an object with identity: 'Computer1$
Computer2$ Computer3$' under: 'DC=Domain,DC=local'.
At line:3 char:53
+ ... name -like "Group*"' | Remove-ADGroupMember -Members "$Comps"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Computer1...mputer3$:ADP
rincipal) [Remove-ADGroupMember], ADIdentityNotFoundException
+ FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.Ac
tiveDirectory.Management.Commands.RemoveADGroupMember
任何帮助都将不胜感激,都将不胜感激。先感谢您。
I'm looking for a way to remove AD computers that are listed in a text file from any
AD groups they might be in, within the same OU. For example, the text file contains:
Computer1$
Computer2$
Computer3$
The groups those three computers might appear in are:
Group1
Group2
Group3
I found this on StackOverflow from 2016 from the title "PowerShell - Remove-ADGroupMember - Locking my admin account" and edited for me:
Get-ADGroup -filter 'name -like "Group*"' | Remove-ADGroupMember -Members "Computer3$"
This works fine to remove "Computer3$! But when I try to replace "Computer3$" with my variable in a Foreach, it breaks. Here's my code:
$Comps = Get-Content "C:\Users\Administrator\Desktop\Complist.txt"
foreach ($comp in $comps) {
Get-ADGroup -filter 'name -like "Group*"' | Remove-ADGroupMember -Members "$Comps"
}
and errors with:
Remove-ADGroupMember : Cannot find an object with identity: 'Computer1$
Computer2$ Computer3
Any help would be much appreciated, thank you in advance.
under: 'DC=Domain,DC=local'.
At line:3 char:53
+ ... name -like "Group*"' | Remove-ADGroupMember -Members "$Comps"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Computer1...mputer3$:ADP
rincipal) [Remove-ADGroupMember], ADIdentityNotFoundException
+ FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.Ac
tiveDirectory.Management.Commands.RemoveADGroupMember
Any help would be much appreciated, thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是引用
$ comps
(对象组),而不是$ comp
(对象)。更好的可变命名将在这里有所帮助。
You were referencing
$comps
(the group of objects) instead of$comp
(the object) inside your loop.Better variable naming would help here.