当在字符串中给出 SAMAccountName 时,Get-ADObject -Identity 找不到对象 - 为什么?

Get-ADObject -Identity doesn't find object when SAMAccountName is given in string - why?

这很奇怪:为变量提供 SAMAccountNameGet-ADObjectIdentity 参数找不到对象,而 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

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

  1. 尊贵的名字
  2. 一个 GUID (ObjectGUID)

Get-ADUser 适用于这种情况:

Get-ADUser -Identity $FreeSam