从本地管理员组中删除单个ID



我想从管理员组中删除个人ID

我有下面的代码来获得成员并删除他们

Get-LocalGroupMember -Name 'Administrators'
$AdminGroup = [ADSI]"WinNT://$ComputerName/Administrators,group"
$User = [ADSI]"WinNT://$DomainName/$UserName,user"
$AdminGroup.Remove($User.Path)
Write-Host "Successfull:" $ComputerName

我面临的问题是如何识别组中的单个/个人ID

下面是我从一个服务器提取的一个样本输出,其中没有个人ID存在

Name                                                 SID                                               PrincipalSource ObjectClass
----                                                 ---                                               --------------- -----------
AUTO1APcsgadm#                                      S-1-5-21-126948685-454775200-1760099607-500                 Local User       
ZA                                                  S-1-5-21-3095416536-3097367016-2845470932         ActiveDirectory Other      
ZAA-Auto$                                           S-1-5-21-3095416536-3097367016-2845470932-1423106 ActiveDirectory User       
ZAA-Server Administrators                           S-1-5-21-3095416536-3097367016-2845470932-128673  ActiveDirectory Group      
ZAA92361                                            S-1-5-21-3095416536-3097367016-2845470932-1423726 ActiveDirectory User       
ZAA-SAN-AUTO                                        S-1-5-21-3095416536-3097367016-2845470932-1475616 ActiveDirectory User       
ZADomain Admins                                     S-1-5-21-3095416536-3097367016-2845470932-512     ActiveDirectory Group      

我现在有上面的数据,我想从中删除个人账户,它是ZA\A92361(这里我知道这是个人账户,但在实际情况下我需要找出并删除(

请让我知道这个

虽然我还不清楚识别要删除的用户是什么意思,但我相信Get-LocalGroupMembercmdlet会返回识别他们所需的一切。

$ADusersToRemove = 'jdoe', 'cblossom'  # example some SamAccountNames of users to remove from the group
# from these users to remove, get an array of their Security IDs
$ADsidsToRemove = $ADusersToRemove | ForEach-Object { (Get-ADUser -Identity $_ -ErrorAction SilentlyContinue).SID }
# get a list of members of the local group, AD users only
$allADMembers = Get-LocalGroupMember -Name 'Administrators' | Where-Object { $_.ObjectClass -eq 'user' -and $_.PrincipalSource -eq 'ActiveDirectory' }
# remove the wanted AD users from the group
$ADmembersToRemove = @($allADMembers | Where-Object { $ADsidsToRemove -contains $_.SID })
if ($ADmembersToRemove.Count) {
Remove-LocalGroupMember -Name 'Administrators' -Member $membersToRemove.SID
}

如果你也想删除LOCAL用户(而不是AD(,你可以做一些类似的

# get an array of LocalPrincipal objects
$LocalUsersToRemove = @(Get-LocalUser -Name 'someguy', 'anotheruser' )
if ($LocalUsersToRemove.Count) {
Remove-LocalGroupMember -Name 'Administrators' -Member $LocalUsersToRemove
}

如果你说你想排除服务帐户,那么这个问题实际上是如何区分服务帐户和普通用户帐户。

我不知道你是如何组织你的广告的,但当然有几个选择:

  1. 您可以让所有服务帐户的samaccountname都以一个特殊的前缀开头,比如svc_之类的
    然后您可以更改过滤器,使其成为
Where-Object { $ADsidsToRemove -contains $_.SID -and (($_.Name -split '\') -notlike 'svc_*')}
  1. 您可以有一个特殊的OU,其中所有服务帐户都像OU=ServiceAccounts,DC=Company,DC=com一样存储然后你可以使用过滤器
Where-Object { $ADsidsToRemove -contains $_.SID -and ((Get-ADUser -Identity $_.SID).DistinguishedName -notlike '*OU=ServiceAccounts,DC=Company,DC=com')}
  1. 您可以让服务帐户的Description属性全部包含单词Service account
    然后您可以使用filter
Where-Object { $ADsidsToRemove -contains $_.SID -and ((Get-ADUser -Identity $_.SID -Properties Description).Description -notlike '*Service account*')}
  1. 也许您在服务帐户上使用了Extension属性来测试ExtensionAttribute1=svc
    然后您可以使用filter
Where-Object { $ADsidsToRemove -contains $_.SID -and ((Get-ADUser -Identity $_.SID -Properties ExtensionAttribute1).ExtensionAttribute1 -notlike 'svc')}

正如你所看到的,可能性几乎是无穷无尽的。。

最新更新