PowerShell查找内置的本地管理员帐户

在域环境中,在本地计算机(服务器的客户端)上管理帐户并不常见,但是经常会讨论一个帐户-本地管理员。

一些公司在计算机上禁用此帐户,一些公司将其密码设置为标准密码,有些公司在部署时将密码随机化,并在数据库或类似数据库中跟踪它们。事实是,有时在故障排除方案中,手头上拥有本地管理员帐户的密码确实很方便,但是要跟踪在哪个服务器上使用哪个密码可能很棘手。

一些公司使用操作系统的本地化版本,这意味着本地管理员帐户并不总是被命名为Administrator,它可以是Järjestelmänvalvoja,Administrateur,Rendszergazda,Administrador,Администратор,Administrador或Administratör,并且一些公司将本地管理员帐户重命名为完全不同的名称。

那么,如何获得计算机上本地管理员帐户的名称?
每个帐户都有一个唯一的标识符,称为安全标识符(SID)。我不会更深入地介绍SID的生成方式,但是一些帐户的SID总是与特定模式匹配。这些SID称为众所周知的安全标识符。

管理员帐户是唯一具有SID以“ -500”结尾的帐户。利用这些知识,我在Powershell中编写了一个简单的函数,该函数将列出计算机上的所有本地用户,并返回带有以“ -500”结尾的SID的帐户名称。

function Get-SWLocalAdmin {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        $ComputerName
    )
    Process {
        Foreach ($Computer in $ComputerName) {
            Try {
                Add-Type -AssemblyName System.DirectoryServices.AccountManagement
                $PrincipalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine, $Computer)
                $UserPrincipal = New-Object System.DirectoryServices.AccountManagement.UserPrincipal($PrincipalContext)
                $Searcher = New-Object System.DirectoryServices.AccountManagement.PrincipalSearcher
                $Searcher.QueryFilter = $UserPrincipal
                $Searcher.FindAll() | Where-Object {$_.Sid -Like "*-500"}
            }
            Catch {
                Write-Warning -Message "$($_.Exception.Message)"
            }
        }
    }
}

Get-SWLocalAdmin

位于命令管道位置 1 的 cmdlet Get-SWLocalAdmin
请为以下参数提供值:
ComputerName: x220win10


GivenName                         :
MiddleName                        :
Surname                           :
EmailAddress                      :
VoiceTelephoneNumber              :
EmployeeId                        :
AdvancedSearchFilter              : System.DirectoryServices.AccountManagement.AdvancedFilters
Enabled                           : True
AccountLockoutTime                :
LastLogon                         : 2020/1/12 7:36:04
PermittedWorkstations             : {}
PermittedLogonTimes               : {255, 255, 255, 255...}
AccountExpirationDate             :
SmartcardLogonRequired            : False
DelegationPermitted               : True
BadLogonCount                     : 0
HomeDirectory                     :
HomeDrive                         :
ScriptPath                        :
LastPasswordSet                   : 2019/12/3 23:44:40
LastBadPasswordAttempt            :
PasswordNotRequired               : False
PasswordNeverExpires              : True
UserCannotChangePassword          : False
AllowReversiblePasswordEncryption : False
Certificates                      : {}
Context                           : System.DirectoryServices.AccountManagement.PrincipalContext
ContextType                       : Machine
Description                       : 管理计算机(域)的内置帐户
DisplayName                       :
SamAccountName                    : Administrator
UserPrincipalName                 :
Sid                               : S-1-5-21-3763144272-163538275-340441925-500
Guid                              :
DistinguishedName                 :
StructuralObjectClass             :
Name                              : Administrator

PS C:\Users\Administrator>

发布了887 篇原创文章 · 获赞 30 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/allway2/article/details/103943544
今日推荐