【发布时间】:2011-03-09 04:25:43
【问题描述】:
我不是在寻找用户 SID。我正在寻找计算机 SID,活动目录将使用它来唯一标识计算机。我也不想查询活动目录服务器,我想查询计算机本身。
【问题讨论】:
-
计算机 SID 是否会随着 Windows 的新安装而改变?即它是特定于操作系统实例的,还是基于硬件的?
标签: windows wmi identity dns wmi-query
我不是在寻找用户 SID。我正在寻找计算机 SID,活动目录将使用它来唯一标识计算机。我也不想查询活动目录服务器,我想查询计算机本身。
【问题讨论】:
标签: windows wmi identity dns wmi-query
(哦,这很有趣!正如他们所说,我进行了一场疯狂的追逐,试图获取 Win32_SID 实例,它是一个单例,并且无法通过通常的 InstancesOf 或 Query 方法枚举...... yadda yadda雅达。)
嗯,这取决于您想要哪个计算机 SID(说真的!)。有本地计算机自己使用的SID...为此,您只需要获取本地Administrator用户的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)
在 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)
在 .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;
}
所有这些结果都与我从PsGetSid.exe 得到的响应相匹配。
另一方面,Active Directory 使用 SID 来识别每个域成员计算机...您通过获取域中计算机帐户的 SID 来获取该 SID - 以美元符号结尾的那个。
例如,对名为“CLIENT”的域成员使用上述 PowerShell 函数,您可以键入 get-sid "CLIENT$"。
【讨论】:
$admin.SubString(0, $admin.length() - 4) 应该是 $admin.SubString(0, $admin.Length - 4) 才能工作..
(Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount = 'True' AND SID LIKE 'S-1-5-21-%-500'").Name
您可以简单地从 Windows 命令行运行 reg query HKLM\SOFTWARE\Microsoft\Cryptography /v MachineGuid。
这是 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%
【讨论】:
从微软网站上找到一个工具,可以轻松获取 SID
http://technet.microsoft.com/en-us/sysinternals/bb897417.aspx
只需下载文件,解压缩,打开命令提示符,然后运行 psgetsid.exe。
微软网站上也有一些关于 SID 的很好的解释
http://blogs.msdn.com/b/aaron_margosis/archive/2009/11/05/machine-sids-and-domain-sids.aspx
【讨论】: