如何在PowerShell中将SID转换为帐户名? [英] How can I convert a SID to an account name in PowerShell?

查看:182
本文介绍了如何在PowerShell中将SID转换为帐户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题的灵感来自使用C#标签的类似问题。如果我有Windows SID,并且想将其转换为可读的帐户名,该如何使用PowerShell而不是C#来实现?

This question is inspired by this similar question using the C# tag. If I have a Windows SID, and would like to convert it to a readable account name, how can I achieve this using PowerShell instead of C#?

现在,我有下面的代码检索当前登录的用户帐户的组成员身份:

Right now, I have the following code, which retrieves the group memberships for the currently logged on user account:

$Identity = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$Identity.Groups;

Groups 属性的结果不给我任何帐户名,只有SID。如果将 Groups 属性的输出通过管道传递到PowerShell的 Get-Member cmdlet中,则可以看到生成的对象是 System.Security.Principal.SecurityIdentifier 对象。但是,请查看 Groups 属性的文档(和Intellisense)表明它正在返回 IdentityReferenceCollection 对象

The results of the Groups property does not give me any account names, only SIDs. If I pipe the output from the Groups property into PowerShell's Get-Member cmdlet, I can see that the resulting objects are System.Security.Principal.SecurityIdentifier objects. However, looking at the documentation (and Intellisense) for the Groups property shows that it is returning an IdentityReferenceCollection object.

如何将这些 SecurityIdentifier 对象转换为专有名称?

How do I convert these SecurityIdentifier objects into proper names?

推荐答案

解决方案是使用 Translate()方法的translation%28v = vs.110%29.aspx rel = nofollow noreferrer> /msdn.microsoft.com/zh-cn/library/System.Security.Principal.SecurityIdentifier%28v=vs.110%29.aspx rel = nofollow noreferrer> SecurityIdentifier 类。此方法的单个参数是对您要将 SecurityIdentifier 转换为.NET类型的引用。如果检查类似的C#问题的此答案,您会发现您可以简单地传递对 System.Security.Principal。 NTAccount类

The solution is to use the Translate() method of the SecurityIdentifier class. The single parameter for this method is a reference to the .NET type that you would like to convert the SecurityIdentifier to. If you examine this answer to the similar C# question, you will see that you can simply pass in a reference to the System.Security.Principal.NTAccount class.

结果代码如下:

$Identity = [System.Security.Principal.WindowsIdentity]::GetCurrent();
foreach ($Group in $Identity.Groups) {
    $Group.Translate([System.Security.Principal.NTAccount]).Value;
}

这篇关于如何在PowerShell中将SID转换为帐户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆