How to get NT Account from the SID using PowerShell

陌路散爱 提交于 2019-12-24 17:44:38

问题


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??).

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...).

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

回答1:


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

Output:

NT AUTHORITY\NETWORK SERVICE

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

Output:

NT AUTHORITY
NETWORK SERVICE
NT AUTHORITY\NETWORK SERVICE

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.



来源:https://stackoverflow.com/questions/34767058/how-to-get-nt-account-from-the-sid-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!