You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell如何跨语言识别对象类类型 规避本地化术语问题

跨语言获取本地管理员组成员、判断对象类型的方案

不要依赖objectClass属性的本地化显示值做类型判断,这类面向终端用户展示的文案会随系统语言变化,所有多语言兼容的脚本都应该使用系统内置的非本地化固定标识做逻辑判断。

方案1:无额外模块依赖(兼容所有PowerShell版本)

使用.NET内置的System.DirectoryServices.AccountManagement类实现,全程通过固定SID、对象托管类型做判断,完全不读取任何本地化显示字段:

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
# 初始化本地机器上下文
$ctx = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine)
# 通过内置管理员组固定SID定位组,彻底规避组名本地化问题
$adminGroup = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($ctx, [System.DirectoryServices.AccountManagement.IdentityType]::Sid, "S-1-5-32-544")
# 遍历成员,通过对象的派生类型直接判断类别,无语言依赖
$result = foreach ($member in $adminGroup.GetMembers($false)) {
    [PSCustomObject]@{
        名称 = $member.Name
        类型 = switch ($member) {
            {$_ -is [System.DirectoryServices.AccountManagement.UserPrincipal]} { "用户" }
            {$_ -is [System.DirectoryServices.AccountManagement.GroupPrincipal]} { "组" }
            {$_ -is [System.DirectoryServices.AccountManagement.ComputerPrincipal]} { "计算机" }
            default { "其他" }
        }
    }
}
$result | Format-Table -AutoSize

方案2:适用于PowerShell 5.1+(更简洁)

使用微软官方内置的Microsoft.PowerShell.LocalAccounts模块,该模块返回的ObjectClass属性是固定枚举值,不会随系统语言变化:

# 固定SID定位管理员组
$adminGroup = Get-LocalGroup -Sid "S-1-5-32-544"
# 获取成员,返回的ObjectClass固定为User/Group/Computer三个枚举值,无本地化
$groupMembers = Get-LocalGroupMember -Group $adminGroup | Select-Object Name, ObjectClass
$groupMembers

注意:不要通过“Administrators”这类显示名查找本地管理员组,不同语言系统下组名同样存在本地化差异,使用内置组固定SID定位是唯一全版本兼容的方案。

内容的提问来源于stack exchange,提问作者Noemie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.07 16:15:41