powershell - 获取 ADUser-Identity

标签 powershell active-directory

无法将变量传递给 Powershell 中的 Identity 参数。

$username = "John.Doe"
Get-ADUser -Identity "$username"
Get-ADUser : Cannot find an object with identity: 'John.Doe' under: 'DC=contoso,DC=com'.
At line:1 char:1
+ Get-ADUser -Identity "$username"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (John.Doe:ADUser) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,M
   icrosoft.ActiveDirectory.Management.Commands.GetADUser

如果我只是输入 Get-ADUser -Identity "John.Doe",结果就会很好。

最佳答案

-Identity 参数接受以下内容:

  • A distinguished name
  • A GUID (objectGUID)
  • A security identifier (objectSid)
  • A SAM account name (sAMAccountName)

如果您想根据其他属性进行搜索,则需要使用-Filter开关。例如,要根据 UserPrincipalName 查找用户,您可以执行以下操作:

Get-ADUser -Filter "UserPrincipalName -eq '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d69cb9beb8f892b9b396b5b9b8a2b9a5b9f8b5b9bb" rel="noreferrer noopener nofollow">[email protected]</a>'"

参见Get-ADUser了解更多详情。

关于powershell - 获取 ADUser-Identity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56064624/

相关文章:

c# - 在 C# 中使用 vpn 的事件目录身份验证

xml - 如何在 PowerShell 中为 XML 添加子元素

c# - ExchangeService with AutodiscoverUrl 从错误的电子邮件地址发送

C# Active Directory PrincipalContext/UserPrincipal.IsMemberOf 错误

powershell - 使用 PowerShellGet 安装 PowerShell 模块后写入配置文件

active-directory - User.IsInRole ("fake group") 结果为 "The trust relationship between the primary domain and the trusted domain failed"

c# - 如何使用 C# 更新 Active Directory 属性。

wpf - 如何使用 Powershell 在 XAML 数据网格中的列上设置样式属性?

Powershell脚本 “on exit”事件?

powershell - 如何在运行窗口服务 msi 的 powershell 脚本中包含用户名和密码