如何使用PowerShell从SID获取NT帐户 [英] How to get NT Account from the SID using PowerShell
问题描述
在处理另一个问题期间,我发现我需要为NT AUTHORITY\Network Service
帐户获取 localized 帐户名称(&¤%@ localises帐户名称?).
During my work on another question, I found that I need to get the localised account name for the NT AUTHORITY\Network Service
account (who the &¤%@ localises account names??).
我有一个解决方法,其中涉及运行一些VBScript代码,但是由于我的项目的其余部分在Powershell中,所以我认为将代码转换为Powershell是正确的"(因此,我不需要从我的Powershell调用VBScript,反正我也不知道该怎么做...).
I have a work-around which involves running a bit of VBScript code, but since the rest of my project is in Powershell I think it would be "correct" to convert this code to Powershell (so I don't need to call VBScript from my Powershell, which I don't know how to do anyway...).
您知道这是什么功能,以及如何在Powershell中完成此操作吗?
Any idea what this does, and how the same is done in Powershell?
Set objWMI = GetObject ("winmgmts:root\cimv2")
Set objSid = objWMI.Get ("Win32_SID.SID='S-1-5-20'")
Set MyDomain = objSid.ReferencedDomainName
Set MyAccount = objSid.AccountName
推荐答案
这种方法应该可以解决问题;
Something like this should do the trick;
$objSID = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-20")
$objAccount = $objSID.Translate([System.Security.Principal.NTAccount])
$objAccount.Value
输出:
NT AUTHORITY\NETWORK SERVICE
如果您想要更多控制权,可以坚持使用WMI方法并执行类似操作;
If you wanted a bit more control you could stick with the WMI method and do something like this;
$acc = ([wmi]"Win32_SID.SID='S-1-5-20'")
$name = $acc.AccountName
$domain = $acc.ReferencedDomainName
$domain
$name
"{0}\{1}" -f $domain, $name
输出:
NT AUTHORITY
NETWORK SERVICE
NT AUTHORITY\NETWORK SERVICE
这样,您现在具有组成NT帐户名称的两个组件,与上面的脚本相同.
That way you now have the two components that make up the NT Account name separate same as your above script.
此方法可能是最接近原始示例的方法.
This method is probably the closest to your original example.
这篇关于如何使用PowerShell从SID获取NT帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!