当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在 PowerShell 和 Cmd 中将 SID 转换为用户名

作者:精品下载站 日期:2024-12-14 16:48:43 浏览:15 分类:玩电脑

在 PowerShell 和 Cmd 中将 SID 转换为用户名


如果您有用户SID,我们可以使用.NET框架类System.Security.Principal.SecurityIdentifier在PowerShell中将SID转换为用户名。使用wmic命令从命令行中的 SID。

安全标识符 (SID) 是分配给对象的唯一值,用于标识受托者,并由权威机构(例如 Windows 域控制器)颁发。

在此示例中,我们将讨论如何使用 .NET Framework 库在 PowerShell 中将 SID 转换为用户名,并在命令行中通过 sid 获取用户。

PowerShell SID 到用户名

使用将 SID(安全标识符)作为输入的 .NET Framework 类 System.Security.Principal.SecurityIdentifier

它通过调用接受 System.Security.Principal.NTAccount 作为输入的 Translate() 方法将 SID 转换为用户名。

请参阅以下 PowerShell 脚本将 SID 转换为用户名。

# Give SID as input to .NET Framework Class
$SID = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-21-1326752099-4012446882-462961959-1103")

# Use Translate to find user from sid
$objUser = $SID.Translate([System.Security.Principal.NTAccount])

# Print the converted SID to username value
$objUser.Value

上述将 SID 转换为用户名的 PowerShell 脚本的输出为:

[玩转系统] 在 PowerShell 和 Cmd 中将 SID 转换为用户名

命令行中的 SID 到用户名

您可以使用命令行 (cmd) 使用 wmic 命令将 SID 转换为用户名。使用wmic命令获取用户帐户,在where子句中指定用户SID,即可从SID中获取用户。

wmic useraccount where sid='S-1-5-21-1326752099-4012446882-462961959-1103' get name, caption,FullName

在上面的命令中,它从 SID 获取用户并返回用户对象属性,如用户名、标题、全名、域等......

使用命令行将 SID 转换为用户名的上述命令的输出是:

C:\>wmic useraccount where sid='S-1-5-21-1326752099-4012446882-462961959-1103' get name, caption,FullName

Caption        FullName   Name
SHELLPRO\toms  Tom Smith  toms


C:\>

结论

希望上面关于如何在PowerShell和命令行中将SID转换为用户名的文章对您有所帮助。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯