如何在PowerShell中将SID转换为帐户名?

编程入门 行业动态 更新时间:2024-10-27 18:27:04
本文介绍了如何在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/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转换为帐户名?

本文发布于:2023-11-09 17:07:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572941.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   中将   帐户名   如何在   PowerShell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!