sAMAccountName 是用于支持早期版本 Windows(Windows 2000 之前)的客户端和服务器的登录名。尽管年代久远,它仍然是 Active Directory (AD) 中确保向后兼容性的基本属性,作为域中用户和计算机的唯一登录名。Get-ADUser PowerShell cmdlet 常用于识别 AD 用户对象的 sAMAccountName。本文介绍如何使用 PowerShell 以及 AD user reports 在 ADManager Plus 中获取 AD 用户的 sAMAccountName。
下表比较了使用 PowerShell 与 ADManager Plus 查找 AD 用户 sAMAccountName 的过程。
开始之前,请确保:
要使用 PowerShell 获取 AD 用户的 sAMAccountName,请以管理员身份打开 Windows PowerShell 并执行以下脚本:
Get-ADUser -Identity 'John' | Select-Object Name, sAMAccountName
使用 ADManager Plus 查找 AD 用户的 sAMAccountName:
要检索特定 OU 中的用户列表,可以使用 -SearchBase 参数。
Get-ADUser-Filter * -SearchBase$targetOU | Select-Object Name, sAMAccountName
要获取域中所有用户的完整列表,只需删除前一个示例中的 -SearchBase 参数。
Get-ADUser -Filter * | Select-Object Name, sAMAccountName
以下命令获取所有用户并将其名称、sAMAccountName 及状态导出到 CSV 文件。
Get-ADUser -Filter * -Properties Enabled | Select-Object Name, sAMAccountName, Enabled | Export-Csv -Path "C:\AD_Users.csv" -NoTypeInformation
以下是使用 Get-ADUser cmdlet 根据 sAMAccountName 检索用户的一些实用示例。
获取用户最简单的方法是使用 -Identity 参数及其 sAMAccountName。
# Get user by sAMAccountName
Get-ADUser -Identity "John"
若要查看默认属性之外的内容,请使用 -Properties 参数指定要查看的属性,例如电子邮件地址或部门。
# Get the email address and department of a user
Get-ADUser -Identity "John" -Properties email, department | Select-Object name, email, department
使用以下脚本仅通过 sAMAccountName 检查用户是否启用:
# Import the list of sAMAccountNames from a CSV file
Import-Csv -Path "C:\temp\users.csv" | ForEach-Object {
# For each username in the CSV, run Get-ADUser
Get-ADUser -Identity $_.sAMAccountName -Properties Enabled, LastLogonDate | Select-Object sAMAccountName, name, Enabled, LastLogonDate
虽然 PowerShell 是强大的 AD 管理工具,但仅依赖它存在重大挑战,尤其是在复杂的 AD 环境中。
ADManager Plus 让您克服所有 PowerShell 的限制,通过其预定义的 AD 报告轻松获取 AD 用户。
sAMAccountName 是传统的、兼容 NetBIOS 的登录名,而 UserPrincipalName 是更现代的、类似电子邮件格式的登录名。
这是任何用户入职脚本中的关键步骤,以避免错误。最好的方法是尝试检索用户并检查结果是否为 null。以下是一个简单且可靠的函数,您可以将其添加到脚本中:
function Test-sAMAccountNameExists {
param (
[Parameter(Mandatory=$true)]
[string]$UserName
)
# The @() ensures the result is always an array, preventing errors if null
$user = @(Get-ADUser -Filter "sAMAccountName -eq '$UserName'")
if ($user.Count -gt 0) {
# If count is greater than 0, the user exists
Write-Host "Username '$UserName' already exists." -ForegroundColor Red return $true
} else {
Write-Host "Username '$UserName' is available." -ForegroundColor Green return $false
}
}
# --- How to use the function ---
Test-sAMAccountNameExists -UserName "b.cooper"
Test-sAMAccountNameExists -UserName "new.user99"
AD 中的 manager 属性存储的是专有名称,而不是 sAMAccountName。为此,您需要检索用户对象,检查 Manager 属性是否已填充,如果是,则可以使用其值来检索经理的用户对象。以下是脚本:
# The user you want to check
$userName = "b.cooper"
# Step 1: Get the user and specifically request the Manager property
$user = Get-ADUser -Identity $userName -Properties Manager
if ($null -ne $user.Manager) {
# Step 2: If the manager property exists, get the manager's user object
$manager = Get-ADUser -Identity $user.Manager
Write-Host "The manager of '$userName' is '$($manager.sAMAccountName)'."
}
else {
Write-Host "User '$userName' does not have a manager listed."
}