fixed problems related to userpricincipalname (user@domain) and undefined variables

fixed variable capitalization
This commit is contained in:
Hans-Joachim Kliemeck 2016-01-12 09:52:08 +01:00
parent 0688522eb7
commit fe4429ba3e

View file

@ -27,52 +27,49 @@
#Functions #Functions
Function UserSearch Function UserSearch
{ {
Param ([string]$AccountName) Param ([string]$accountName)
#Check if there's a realm specified #Check if there's a realm specified
if ($AccountName.Split("\").count -gt 1)
{
if ($AccountName.Split("\")[0] -eq $env:COMPUTERNAME)
{
$IsLocalAccount = $true
}
Else
{
$IsDomainAccount = $true
$IsUpn = $false
}
} $searchDomain = $false
Elseif ($AccountName.contains("@")) $searchDomainUPN = $false
if ($accountName.Split("\").count -gt 1)
{ {
$IsDomainAccount = $true if ($accountName.Split("\")[0] -ne $env:COMPUTERNAME)
$IsUpn = $true {
$searchDomain = $true
$accountName = $accountName.split("\")[1]
}
}
Elseif ($accountName.contains("@"))
{
$searchDomain = $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 $true) if ($searchDomain -eq $false)
{ {
# 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 $AccountName} $localaccount = get-wmiobject -class "Win32_Account" -namespace "root\CIMV2" -filter "(LocalAccount = True)" | where {$_.Caption -eq $accountName}
if ($localaccount) if ($localaccount)
{ {
return $localaccount.SID return $localaccount.SID
} }
} }
ElseIf ($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=$($accountname.split("\")[1])" $Searcher.Filter = "sAMAccountName=$($accountName)"
} }
Else { Else {
$Searcher.Filter = "userPrincipalName=$($accountname)" $Searcher.Filter = "userPrincipalName=$($accountName)"
} }
$result = $Searcher.FindOne() $result = $Searcher.FindOne()