代码之家  ›  专栏  ›  技术社区  ›  Mark

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

  •  25
  • Mark  · 技术社区  · 16 年前

    我不是在找用户SID。我正在寻找计算机SID,active directory将使用它来唯一标识计算机。我也不想查询active directory服务器,我想查询计算机本身。

    3 回复  |  直到 16 年前
        1
  •  43
  •   ewall    11 年前

    在 ,看起来是这样的:

    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)
    

    动力壳 ,如下所示:

    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)
    

    C级# 在.NET 3.5上:

    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;
    }
    

    所有这些的结果都符合我的反应 PsGetSid.exe


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

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

        2
  •  2
  •   l0pan    12 年前

    你可以直接跑 reg query HKLM\SOFTWARE\Microsoft\Cryptography /v MachineGuid 从Windows命令行。

    以下是Windows批处理文件示例:

    set KEY_REGKEY=HKLM\SOFTWARE\Microsoft\Cryptography
    set KEY_REGVAL=MachineGuid
    
    REM Check for presence of key first.
    reg query %KEY_REGKEY% /v %KEY_REGVAL% 2>nul || (echo No theme name present! & exit /b 1)
    
    REM query the value. pipe it through findstr in order to find the matching line that has the value. only grab token 3 and the remainder of the line. %%b is what we are interested in here.
    set KEY_NAME=
    for /f "tokens=2,*" %%a in ('reg query %KEY_REGKEY% /v %KEY_REGVAL% ^| findstr %KEY_REGVAL%') do (
        set KEY_NAME=%%b
    )
    echo %KEY_NAME%