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

Remove Single IDs from local administrator group

我想从管理员组中删除个人 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

我面临的问题是如何识别群组中的single/individual ID

下面是我从其中一个服务器获取的不存在个人 ID 的成员的示例输出

Name                                                 SID                                               PrincipalSource ObjectClass
----                                                 ---                                               --------------- -----------
AUTO1AP\csgadm#                                      S-1-5-21-126948685-454775200-1760099607-500                 Local User       
ZA\                                                  S-1-5-21-3095416536-3097367016-2845470932         ActiveDirectory Other      
ZA\A-Auto$                                           S-1-5-21-3095416536-3097367016-2845470932-1423106 ActiveDirectory User       
ZA\A-Server Administrators                           S-1-5-21-3095416536-3097367016-2845470932-128673  ActiveDirectory Group      
ZA\A92361                                            S-1-5-21-3095416536-3097367016-2845470932-1423726 ActiveDirectory User       
ZA\A-SAN-AUTO                                        S-1-5-21-3095416536-3097367016-2845470932-1475616 ActiveDirectory User       
ZA\Domain Admins                                     S-1-5-21-3095416536-3097367016-2845470932-512     ActiveDirectory Group      

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

请让我知道这件事

虽然我仍然不清楚你所说的 识别 你想删除的用户是什么意思,但我相信 Get-LocalGroupMember cmdlet returns 一切你需要识别它们。

$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 然后你可以使用 filter
Where-Object { $ADsidsToRemove -contains $_.SID -and ((Get-ADUser -Identity $_.SID).DistinguishedName -notlike '*OU=ServiceAccounts,DC=Company,DC=com')}
  1. 您可以让服务帐户的描述 属性 全部包含单词 Service account
    然后你可以使用 filter
Where-Object { $ADsidsToRemove -contains $_.SID -and ((Get-ADUser -Identity $_.SID -Properties Description).Description -notlike '*Service account*')}
  1. 也许您在服务帐户上使用了扩展属性来进行测试 ExtensionAttribute1=svc
    然后你可以使用 filter
Where-Object { $ADsidsToRemove -contains $_.SID -and ((Get-ADUser -Identity $_.SID -Properties ExtensionAttribute1).ExtensionAttribute1 -notlike 'svc')}

如您所见,可能性几乎是无限的..