如何使用WMI检索Windows计算机的SID?

Mar*_*ark 25 windows dns wmi identity wmi-query

我不是在寻找用户SID.我正在寻找计算机SID,活动目录将用于唯一标识计算机.我也不想查询活动目录服务器,我想查询计算机本身.

ewa*_*all 39

(哦,这是一个有趣的!我继续疯狂追逐,正如他们所说,试图获取Win32_SID实例,这是一个单例,而不是通常的InstancesOf或Query方法可以枚举... yadda yadda yadda.)

嗯,这取决于你想要的计算机SID(严重!).本地计算机使用的是SID ...为此,您只需要获取本地管理员用户的SID,并从末尾删除"-500"以获取计算机的SID.

VBScript中,它看起来像这样:

strComputer = "AFAPC001"
strUsername = "Administrator"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objAccount = objWMIService.Get("Win32_UserAccount.Name='" & strUsername & "',Domain='" & strComputer & "'")
WScript.Echo "Administrator account SID: " & objAccount.SID
WScript.Echo "Computer's SID: " & Left(objAccount.SID, Len(objAccount.SID) - 4)
Run Code Online (Sandbox Code Playgroud)

PowerShell中,像这样:

function get-sid
{
    Param ( $DSIdentity )
    $ID = new-object System.Security.Principal.NTAccount($DSIdentity)
    return $ID.Translate( [System.Security.Principal.SecurityIdentifier] ).toString()
}
> $admin = get-sid "Administrator"
> $admin.SubString(0, $admin.Length - 4)
Run Code Online (Sandbox Code Playgroud)

在.NET 3.5上的C#中:

using System;
using System.Security.Principal;
using System.DirectoryServices;
using System.Linq;
public static SecurityIdentifier GetComputerSid()
{
    return new SecurityIdentifier((byte[])new DirectoryEntry(string.Format("WinNT://{0},Computer", Environment.MachineName)).Children.Cast<DirectoryEntry>().First().InvokeGet("objectSID"), 0).AccountDomainSid;
}
Run Code Online (Sandbox Code Playgroud)

所有这些结果与我从PsGetSid.exe获得的响应相匹配.


另一方面,Active Directory使用SID来识别每个域成员计算机...通过获取域中计算机帐户的SID获取的SID - 以美元符号结尾的SID.

例如,对于名为"CLIENT"的域成员使用上述PowerShell函数,您可以键入get-sid "CLIENT$".

  • $ID.Translate( [System.Security.Principal.SecurityIdentifier] ) - 已经返回了计算机 sid (AccountDomainSid),我建议 Get-Sid 不要尝试将 Id 转换为字符串,以便用户可以选择任何属性 -在这种情况下是 AccountDomainSid。 (2认同)
  • +1,小提示:管理员帐户可能会被重命名(出于安全原因,在企业环境中)。要按 SID 查找标准本地管理员帐户:`(Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount = 'True' AND SID LIKE 'S-1-5-21-%-500'").Name` (2认同)