Comment puis-je récupérer le SID D'un ordinateur Windows en utilisant WMI?

Je ne cherche pas D'utilisateur SIDs. Je cherche le SID de l'ordinateur, que active directory utiliserait pour identifier l'ordinateur de façon unique. Je ne veux pas d'interroger le serveur active directory, je veux interroger l'ordinateur lui-même.

25
demandé sur Mark 2010-06-28 18:48:55

3 réponses

(Ooh, ce fut un plaisir! Je suis allé sur une fausse piste, comme ils disent, en essayant d'obtenir L'instance Win32_SID, qui est un singleton et non énumérable par les instants habituels de ou les méthodes de requête... yadda yadda yadda.)

Eh bien, cela dépend de l'ordinateur SID que vous voulez (sérieusement!). Il y a le SID que l'ordinateur local utilise pour lui-même... Pour cela, vous avez juste besoin d'obtenir le SID de l'utilisateur administrateur local, et de supprimer le "-500" de la fin pour obtenir l'ordinateur SID.

VBScript, il ressemble à ceci:

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)

PowerShell, comme ceci:

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

les résultats de toutes ces réponses correspondent à la réponse que j'obtiens de PsGetSid.exe.


D'un autre côté, il y a le SID Qu'Active Directory utilise pour identifier chaque ordinateur membre du domaine... Celui que vous allez chercher en obtenant le SID du compte machine Dans le domaine--celui qui se termine par un signe dollar.

par exemple, en utilisant la fonction PowerShell ci-dessus pour un membre de domaine appelé "CLIENT", vous pouvez taper get-sid "CLIENT$".

38
répondu ewall 2015-04-10 13:41:37

Vous pouvez simplement exécuter reg query HKLM\SOFTWARE\Microsoft\Cryptography /v MachineGuid à partir de la ligne de commande de 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%
2
répondu l0pan 2014-05-10 18:06:19

J'ai trouvé un outil sur le site Web de microsoft qui peut vous fournir le SID facilement

http://technet.microsoft.com/en-us/sysinternals/bb897417.aspx

il suffit de télécharger le fichier, de le décompresser, d'ouvrir une invite de commande et d'exécuter psgetsid.EXE.

Il y a quelques bonnes explications sur les SID depuis le site de microsoft ainsi

http://blogs.msdn.com/b/aaron_margosis/archive/2009/11/05/machine-sids-and-domain-sids.aspx

1
répondu deadcode 2015-01-09 02:07:12