Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrieve a Windows Computer's SID using WMI?

I'm not looking for User SIDs. I'm looking for the computer SID, which active directory would use to uniquely identify the computer. I also don't want to query the active directory server, i want to query the computer itself.

like image 270
Mark Avatar asked Jun 28 '10 14:06

Mark


People also ask

How do I find my computer's SID?

If you want to see a computer's SID just pass the computer's name as a command-line argument. If you want to see a user's SID, name the account (e.g. "administrator") on the command-line and an optional computer name.

How do I find the SID of a deleted user?

To determine if the SID is simply that of a deleted user or group, you can search for the object's tombstone, which is something Active Directory (AD) leaves after an object is deleted. Tombstones include the name of the user or group, the object type, and the SID.

What is SID command?

The sid command displays the security identifier in the standard format, for either the current user, or a given user, optionally specified with a domain. Also, if a security identifier is specified, sid sid displays the user associated with that identifier.


1 Answers

(Ooh, this was a fun one! I went on a wild goose chase, as they say, trying to get the Win32_SID instance, which is a singleton and not enumerable by the usual InstancesOf or Query methods... yadda yadda yadda.)

Well, it depends which computer SID you want (seriously!). There's the SID that the local computer uses for itself... For this, you just need to get the SID of the local Administrator user, and remove the "-500" from the end to get the computer's SID.

In VBScript, it looks like this:

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) 

In PowerShell, like this:

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) 

In C# on .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; } 

Results from all of these match the response I get from PsGetSid.exe.


On the other hand, there's the SID that Active Directory uses to identify each domain member computer... That one you fetch by getting the SID of the machine account in the domain--the one that ends with a dollar sign.

E.g., using the above PowerShell function for a domain member called "CLIENT", you can type get-sid "CLIENT$".

like image 135
ewall Avatar answered Sep 28 '22 02:09

ewall