当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell - 从 DistinguishedName 获取 SamAccountName

作者:精品下载站 日期:2024-12-14 16:45:59 浏览:14 分类:玩电脑

PowerShell - 从 DistinguishedName 获取 SamAccountName


要从给定用户的 Active Directory 中的 DistinguishedName 获取 SAMAccountName,请使用 Get-AdUser 过滤器检查 aduser DistinguishedName 并返回 samaccountname 和其他 Active Directory 用户属性。

PowerShell 中的 Get-AdUser cmdlet 获取指定的用户对象或执行搜索以从活动目录检索多个用户对象。

在本文中,我们将讨论在 PowerShell 中如何从专有名称获取 SAMAccountName。

从 PowerShell 中的 DistinguishedName 获取 SAMAccountName

如果您有 Active Directory 用户的 DistinguishedName,并且想要获取 Active Directory 中给定 aduser 的 SAMAccountName,请运行以下命令。

$user = 'CN=Netya Xu,OU=FINANCE,OU=SHELLUSERS,DC=SHELLPRO,DC=LOCAL'

Get-AdUser -Filter {distinguishedName -eq $user} | Select SAMAccountName,DistinguishedName

在上面的 PowerShell 脚本中,$user 变量存储广告用户的专有名称。

使用 Get-AdUser Filter 命令执行对 aduser 对象的搜索,其中 DistinguishedName 等于指定的 DistinguishedName。

如果结果匹配,它将把 aduser 对象传递给 Select 命令,以显示广告用户的 SAMAccountName 和 DistinguishedName。

上述 PowerShell 脚本的输出使用 DistinguishedName 检索 SAMAccountName 并将其显示在终端上。

[玩转系统] PowerShell - 从 DistinguishedName 获取 SamAccountName

酷提示:如何通过示例使用 Get-AdUser cmdlet!

PowerShell 从 DistinguishedName 列表中查找 SAMAccountName

如果您在 CSV 文件中存储了 DistinguishedName,并且要求从 DistinguishedName 列表中获取用户的 SAMAccountName,我们将使用 PowerShell 中的 Get-AdUser cmdlet 来获取 aduser 属性。

让我们考虑 CSV 文件存储中用户的可分辨名称列表,如下所示:

"DistinguishedName"
"CN=Guest,CN=Users,DC=SHELLPRO,DC=LOCAL"
"CN=krbtgt,CN=Users,DC=SHELLPRO,DC=LOCAL"
"CN=Tom Smith,OU=SALES,DC=SHELLPRO,DC=LOCAL"
"CN=Erick Jones,OU=HR,DC=SHELLPRO,DC=LOCAL"
"CN=Gary Willy,OU=HR,DC=SHELLPRO,DC=LOCAL"

要从 CSV 文件中存储的广告用户的 DistinguishedName 列表中获取 SAMAccountName,我们首先需要导入它并使用 foreach 循环进行迭代。

Import-Csv -path "C:\PowerShell\AdUser-DistinguishedName.csv" | Foreach {Get-ADUser -Filter "distinguishedName -eq '$($_.DistinguishedName)'" | Select SamAccountName,DistinguishedName}

在上面的 PowerShell 脚本中,PowerShell 中的 Import-CSV cmdlet 用于导入具有 DistinguishedName 列表的 CSV 文件,并将其作为输入传递给 Foreach 循环。

Foreach 循环 迭代 CSV 文件中可用的 DistinguishedName 列表。

Get-AdUser 使用 Filter 参数来检查 DistinguishedName 是否等于 CSV 文件中的名称。如果结果匹配,则会显示给定 aduser 的 SAMAccountName。

用于从可分辨名称列表中检索 SAMAccountName 的上述 PowerShell 脚本的输出为:

[玩转系统] PowerShell - 从 DistinguishedName 获取 SamAccountName

酷提示:如何在 Active Directory 中获取 aduser SAMAccountName!

结论

希望上述在PowerShell中从distinguishedName获取SAMAccountName的文章对您有所帮助。

使用 Get-AdUser Filter 参数在 Active Directory 中搜索广告用户对象。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯