@ -22,52 +22,49 @@
#Functions
#Functions
Function UserSearch
Function UserSearch
{
{
Param ( [ string ] $ A ccountName)
Param ( [ string ] $ a ccountName)
#Check if there's a realm specified
#Check if there's a realm specified
if ( $AccountName . Split ( " \ " ) . count -gt 1 )
$searchDomain = $false
$searchDomainUPN = $false
if ( $accountName . Split ( " \ " ) . count -gt 1 )
{
{
if ( $AccountName . Split ( " \ " ) [ 0 ] -eq $env:COMPUTERNAME )
if ( $accountName . Split ( " \ " ) [ 0 ] -ne $env:COMPUTERNAME )
{
$IsLocalAccount = $true
}
Else
{
{
$ I sDomainAccount = $true
$searchDomain = $true
$ IsUpn = $false
$accountName = $accountName . split ( " \ " ) [ 1 ]
}
}
}
}
Elseif ( $ A ccountName. contains ( " @ " ) )
Elseif ( $ a ccountName. contains ( " @ " ) )
{
{
$ I sDomainAccount = $true
$ search Domain = $true
$ IsUpn = $true
$ searchDomainUPN = $true
}
}
Else
Else
{
{
#Default to local user account
#Default to local user account
$accountname = $env:COMPUTERNAME + " \ " + $AccountName
$accountName = $env:COMPUTERNAME + " \ " + $accountName
$IsLocalAccount = $true
}
}
if ( $ IsLocalAccount -eq $tru e)
if ( $ searchDomain -eq $fals e)
{
{
# do not use Win32_UserAccount, because e.g. SYSTEM (BUILTIN\SYSTEM or COMPUUTERNAME\SYSTEM) will not be listed. on Win32_Account groups will be listed too
# do not use Win32_UserAccount, because e.g. SYSTEM (BUILTIN\SYSTEM or COMPUUTERNAME\SYSTEM) will not be listed. on Win32_Account groups will be listed too
$localaccount = get-wmiobject -class " Win32_Account " -namespace " root\CIMV2 " -filter " (LocalAccount = True) " | where { $_ . Caption -eq $ A ccountName}
$localaccount = get-wmiobject -class " Win32_Account " -namespace " root\CIMV2 " -filter " (LocalAccount = True) " | where { $_ . Caption -eq $ a ccountName}
if ( $localaccount )
if ( $localaccount )
{
{
return $localaccount . SID
return $localaccount . SID
}
}
}
}
Else If ( $IsDomainAccount -eq $true )
Else
{
{
#Search by samaccountname
#Search by samaccountname
$Searcher = [ adsisearcher ] " "
$Searcher = [ adsisearcher ] " "
If ( $ IsUpn -eq $false ) {
If ( $ searchDomainUPN -eq $false ) {
$Searcher . Filter = " sAMAccountName= $( $account name. split ( " \ " ) [ 1 ] ) "
$Searcher . Filter = " sAMAccountName= $( $account Name ) "
}
}
Else {
Else {
$Searcher . Filter = " userPrincipalName= $( $account n ame) "
$Searcher . Filter = " userPrincipalName= $( $account N ame) "
}
}
$result = $Searcher . FindOne ( )
$result = $Searcher . FindOne ( )