powershell删除文本文件中列出的列出的集体成员,从中删除它们的组是通配符

发布于 2025-01-24 16:19:09 字数 1258 浏览 0 评论 0原文

我正在寻找一种删除从任何文本文件中列出的广告计算机的方法 他们可能在同一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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萧瑟寒风 2025-01-31 16:19:09
$Comps = Get-Content "C:\Users\Administrator\Desktop\Complist.txt"
foreach ($comp in $comps) {
Get-ADGroup -filter 'name -like "Group*"' | Remove-ADGroupMember -Members "$comp"
}

您是引用$ comps(对象组),而不是$ comp(对象)。

Import-Module -Name ActiveDirectory
$listofcomputers = Get-Content -Path "$env:HOMEDRIVE\Users\Administrator\Desktop\Complist.txt"

foreach ($computer in $listofcomputers) {
    Get-ADGroup -Filter 'name -like "Group*"' | Remove-ADGroupMember -Members ('{0}' -f $computer)
}

更好的可变命名将在这里有所帮助。

$Comps = Get-Content "C:\Users\Administrator\Desktop\Complist.txt"
foreach ($comp in $comps) {
Get-ADGroup -filter 'name -like "Group*"' | Remove-ADGroupMember -Members "$comp"
}

You were referencing $comps (the group of objects) instead of $comp (the object) inside your loop.

Import-Module -Name ActiveDirectory
$listofcomputers = Get-Content -Path "$env:HOMEDRIVE\Users\Administrator\Desktop\Complist.txt"

foreach ($computer in $listofcomputers) {
    Get-ADGroup -Filter 'name -like "Group*"' | Remove-ADGroupMember -Members ('{0}' -f $computer)
}

Better variable naming would help here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文