【问题标题】:Get-ADUser -Identity获取 ADUser -Identity
【发布时间】:2020-12-20 20:17:47
【问题描述】:

无法将变量传递给 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",结果会很好。

【问题讨论】:

  • 你确定这是你的$username 变量吗?看起来你有$username = 'John.Doe@contoso.com'。更不用说 Get-ADUser 函数中不需要引号,你可以只使用 Get-ADUser -Identity $username
  • 看起来我复制/粘贴了错误的错误信息。我已经更新了帖子。
  • 您能否使用-Server 参数来保证您在两个测试用例中查询的是同一台服务器?否则,我无法复制它,我已经尝试过 PowerShell 版本 2.0、4.0 和 5.1。

标签: powershell active-directory


【解决方案1】:

我知道这是个老问题,但它可能是答案。它可能会帮助一些人。 我遇到了同样的问题,它难倒了我 1 小时。最后我使用了 $username = $username.trim() 。所以很明显变量有空间需要修剪。

【讨论】:

    【解决方案2】:

    -Identity 参数接受以下内容:

    • 专有名称
    • 一个 GUID (objectGUID)
    • 安全标识符 (objectSid)
    • SAM 帐户名称 (sAMAccountName)

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

    Get-ADUser -Filter "UserPrincipalName -eq 'John.Doe@contoso.com'"
    

    更多详情请见Get-ADUser

    【讨论】:

    • $username = "John.Doe" Get-ADUser -Identity "$username" 不完全限定 UserPrincipleName
    • John.Doe@contoso.com 是完全限定名称。
    • 如果 Get-ADUser -Identity "John.Doe" 不起作用,则 John.Doe 不是 SamAccountName。所以它应该失败。
    • 我复制并粘贴了错误的错误信息我已经更新了 OP。
    • John.Doe 来自哪里?您只是取名字和姓氏并用点连接吗?
    猜你喜欢
    • 2015-12-17
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    相关资源
    最近更新 更多