36 lines
988 B
PowerShell
36 lines
988 B
PowerShell
|
function Get-SidFromUsername {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Returns a domain or local user's SID based on username
|
||
|
#>
|
||
|
|
||
|
[CmdletBinding()]
|
||
|
Param(
|
||
|
[Parameter(Mandatory=$true)]
|
||
|
[Alias("User")]
|
||
|
[string]$userName,
|
||
|
|
||
|
[Parameter(Mandatory=$false)]
|
||
|
[Alias("Domain")]
|
||
|
[string]$domainName
|
||
|
)
|
||
|
|
||
|
$logLead = (Get-LogLeadName);
|
||
|
|
||
|
if ([String]::IsNullOrEmpty($domainName))
|
||
|
{
|
||
|
Write-Verbose ("$logLead : Looking for local user account {0}" -f $userName)
|
||
|
$objUser = New-Object System.Security.Principal.NTAccount($userName)
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
Write-Verbose ("$logLead : Looking for domain user account {0} in domain {1}" -f $userName, $domainName)
|
||
|
$objUser = New-Object System.Security.Principal.NTAccount($domainName, $userName)
|
||
|
}
|
||
|
|
||
|
Write-Verbose "$logLead : Translating user to SecurityIdentifier"
|
||
|
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
|
||
|
return $strSID.Value
|
||
|
}
|
||
|
|