Home > Software engineering >  How to validate if my user has an ADM account based on samaccountname
How to validate if my user has an ADM account based on samaccountname

Time:11-23

I would like to validate if my standard user has an admin account in AD.

Example, the samaccountname of Smith, Joe is SmithJ. I want to check if he has an ADMSmithJ in the AD

$samaccountname = Read-Host "Please type the samaccountname"
$AdUser = get-aduser $samaccountname -Properties samaccountname

Try { get-aduser "adm"$samaccountname? -Properties samaccountname

}catch{ write-host "the user $samaccountname doesnt have a priviledge (Adm) Account."

}

CodePudding user response:

You could do it like this, instead of try / catch, I would personally filter for a user having Name or SamAccountName:

$account = Read-Host "Please type the SamAccountName"
try {
    $adUser  = Get-ADUser $account
    $admUser = 'adm'   $adUser.Surname   $adUser.GivenName[0]
    if($adUser = Get-ADUser -LDAPFilter "(|(name=$admUser)(samAccountName=$admUser))") {
        # if the AD object exists in AD, return the object
        $adUser
    }
    else {
        "No user found with SamAccountName '$admUser' in AD."
    }
}
catch {
    Write-Warning $_
}
  • Related