通过SID以多种语言将成员添加到本地组

时间:2014-12-04 14:12:09

标签: powershell sid

我对PowerShell /脚本/生活一般都很陌生,但最后我遇到了一个值得寻求帮助的问题: 我在环境中的各种Windows本地化 - 当前环境中的英语,芬兰语和俄语,但有可能有其他斯堪的纳维亚/欧洲本地化。我需要将Authenticated用户添加到Administrators组。我可以用英文编写脚本:

NET LOCALGROUP Administrators "Authenticated Users" /add,

但我不知道所有本地化的名字。例如,在俄语中它将是" Administratori"和" Proshedshie Proverku。"在cyrilic中,无论如何我并不那么强大。 当然,我知道SID - 管理员的S-1-5-32-544和认证用户的S-1-5-11。但是,运行

NET LOCALGROUP S-1-5-32-544 S-1-5-11 /add returns error that group doesn't exist. Ok, so I found a script to check it -

$objUser = New-Object System.Security.Principal.NTAccount("kenmyer")

$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])

$strSID.Value

这返回了期望值,到目前为止一直很好。然后我试着仔细检查它 - 通过运行行从SID获取名称 -

$Admin = (Get-WMIObject -Class Win32_Group -Filter "LocalAccount=True and SID='S-1-5-32-544'").Name

$Auth = (Get-WMIObject -Class Win32_Group -Filter "LocalAccount=True and SID='S-1-5-11'").Name

$Admin = Administratori(应该是),而$Auth =没有。没有名字。那就是我停下来的地方。我在英语环境中尝试过这种方法 - 仍然没有这样的小组"信息。运行我写的第一个命令,两个名字都是英文的 - 完美地运作。

有什么想法吗?

UPD: 也许我无法正确解释我想要做的事情,所以让脚本进行讨论:

#Task: to add "Authenticated users" to "Administrators" group in any languange OS.
$objSID = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")
$objgroup = $objSID.Translate( [System.Security.Principal.NTAccount])
$objgroupnameAdm = ($objgroup.Value).Split("\")[1]

$objSID = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-11")
$objgroup = $objSID.Translate( [System.Security.Principal.NTAccount])
$objgroupnameAuth = ($objgroup.Value).Split("\")[1]

#Administratörer
#Autentiserade användare

net localgroup $objgroupnameAdm $objgroupnameAuth /add

我现在在瑞典Win7上尝试这个。结果是:

net.exe : Syntaxen för kommandot är:
At line:13 char:4
+ net <<<<  localgroup $objgroupnameAdm $objgroupnameAuth /add
    + CategoryInfo          : NotSpecified: (Syntaxen för kommandot är::String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

我还尝试将$objgroupnameAuth放在引号中,因为它包含两个单词,但结果相同。将变量定义为字符串 - 无变化,并将$objGroupNameAdm替换为实际值 - 无变化。

如果我无法在英文Windows中这样做,我会认为它在功能上是不可能的。

3 个答案:

答案 0 :(得分:1)

我使用此方法从SID转换为本地化名称:

.SYNOPSIS 将&#34; NT AUTHORITY \ Interactive安全主体添加到本地计算机管理员组&#34;

.DESCRIPTION 该脚本使用SID转换来接收Interactive主体和Administrators组的本地化名称,然后使用本地化名称将主体添加到组中。

# Translate the S-1-5-32-544 (.\Administrators) SID to a group name, the name varies depending on the language version of Windows.
$sid2 = 'S-1-5-32-544'
$objSID2 = New-Object System.Security.Principal.SecurityIdentifier($sid2)
$localadminsgroup = (( $objSID2.Translate([System.Security.Principal.NTAccount]) ).Value).Split("\")[1]

# Translate the S-1-5-4 (NT AUTHORITY\Interactive) SID to an account name, the name varies depending on the language version of Windows.
$sid1 = 'S-1-5-4'
$objSID1 = New-Object System.Security.Principal.SecurityIdentifier($sid1)
$interactive = (( $objSID1.Translate([System.Security.Principal.NTAccount]) ).Value).Split("\")[1]

# Add the security principal name to the local administrators group. (used old style of adding group members due to compatibility reasons)

try {
    Write-Host "Adding security principal: $interactive to the $localadminsgroup group..."

    $group = [ADSI]"WinNT://$env:computername/$localadminsgroup,group"
    $ismember = "False"

    @($group.Invoke("Members")) | ForEach-Object {
        If ($interactive -match $_.GetType.Invoke().InvokeMember("Name", 'GetProperty', $null, $_, $null)) {
            $ismember = "True"
        }
    }

    If ($ismember -eq "True") {
        write-host "user $interactive is already a member of $localadminsgroup"
    }
    Else {
        $result = $group.Add("WinNT://NT AUTHORITY/$interactive,user")
        write-host "user $interactive is added to $localadminsgroup"
    }
}
Catch {
    write-host $_.Exception.Message
}

它并非完全针对您的需求量身定制,但我确信您可以使其发挥作用。

此致

柯恩。

答案 1 :(得分:0)

SID S-1-5-11用于Authenticated UsersWell known SIDs)。这是一个无法修改的BUILTIN组。像这样的其他群组是EveryoneAnonymous等。

这种类型的组不存在于“物理”意义或单词中,即在本地SAM和Active Directory中都没有创建对象。

它们完全由Windows生成和管理。

根据您的连接和/或登录方式,您会在会话中收到SID。

因此,发出WMI Win32_Group请求或使用Get-ADGroup不会返回任何内容。

您可以调用Get-ADAccountAuthorizationGroup,查看特定身份是否属于此类群组。

您可以使用SID检索创建指向System.Security.Principal.NTAccount的创建Authenticated Users对象:

$auth = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-11") 
$name = $auth.Translate([System.Security.Principal.NTAccount])

更新: 我无法将$ auth的本地化名称添加到该组。看起来只有英文版本有效。

答案 2 :(得分:0)

问题很简单,一旦被正确询问,答案可以在这里找到:Microsoft Supprt: NET /ADD command。 如果NET限制为20个字符,而“Autentiseradeanvändare”为24个字符,则不应该起作用。解决方法可以在同一链接中找到。