ps/Modules/Alkami.PowerShell.Common/Public/Get-SidFromUsername.ps1

36 lines
988 B
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
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
}