powershell 如何验证我的用户是否具有基于samaccountname的ADM帐户

c3frrgcw  于 2022-12-13  发布在  Shell
关注(0)|答案(1)|浏览(107)

我想验证我的标准用户是否在AD中拥有管理员帐户。
例如,Smith的samaccountname,Joe是SmithJ。我想检查他在AD中是否有ADMSmithJ

$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."

}
j2cgzkjk

j2cgzkjk1#

你可以这样做,而不是try/catch,我个人会过滤一个用户有NameSamAccountName

$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 $_
}

相关问题