首页 > 解决方案 > 当在字符串中给出 SAMAccountName 时,Get-ADObject -Identity 找不到对象 - 为什么?

问题描述

这很奇怪:提供带有SAMAccountName,参数Get-ADObject的变量Identity找不到对象,而Filter参数却可以。

为什么会这样?

这是我的代码:

PS C:\Projects> Get-ADObject -Filter { SamAccountName -eq $FreeSam }

DistinguishedName                     Name         ObjectClass  ObjectGUID
-----------------                     ----         -----------  ----------
CN=TP1-sh123456,OU=TP1-Resources ...


PS C:\Projects> Get-ADObject -Identity $FreeSam
Cannot find an object with identity: 'TP1-sh123456' under: 'DC=tstglobal,DC=tst,DC=loc'.
At line:1 char:1
+ Get-ADObject -Identity $FreeSam
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (TP1-sh123456:ADObject) [Get-ADObject], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADObject

标签: powershellactive-directorypowershell-5.0

解决方案


查看Get-ADObject的文档,该Identity参数的唯一有效输入似乎是

  1. 专有名称
  2. 一个 GUID (ObjectGUID)

Get-ADUser将在这种情况下工作:

Get-ADUser -Identity $FreeSam

推荐阅读