@ -27,52 +27,49 @@
#Functions
Function UserSearch
{
Param ( [ string ] $ A ccountName)
Param ( [ string ] $ a ccountName)
#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 )
{
$IsLocalAccount = $true
}
Else
if ( $accountName . Split ( " \ " ) [ 0 ] -ne $env:COMPUTERNAME )
{
$ I sDomainAccount = $true
$ IsUpn = $false
$searchDomain = $true
$accountName = $accountName . split ( " \ " ) [ 1 ]
}
}
Elseif ( $ A ccountName. contains ( " @ " ) )
Elseif ( $ a ccountName. contains ( " @ " ) )
{
$ I sDomainAccount = $true
$ IsUpn = $true
$ search Domain = $true
$ searchDomainUPN = $true
}
Else
{
#Default to local user account
$accountname = $env:COMPUTERNAME + " \ " + $AccountName
$IsLocalAccount = $true
$accountName = $env:COMPUTERNAME + " \ " + $accountName
}
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
$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 )
{
return $localaccount . SID
}
}
Else If ( $IsDomainAccount -eq $true )
Else
{
#Search by samaccountname
$Searcher = [ adsisearcher ] " "
If ( $ IsUpn -eq $false ) {
$Searcher . Filter = " sAMAccountName= $( $account name. split ( " \ " ) [ 1 ] ) "
If ( $ searchDomainUPN -eq $false ) {
$Searcher . Filter = " sAMAccountName= $( $account Name ) "
}
Else {
$Searcher . Filter = " userPrincipalName= $( $account n ame) "
$Searcher . Filter = " userPrincipalName= $( $account N ame) "
}
$result = $Searcher . FindOne ( )