簡體   English   中英

Get-ADUser -Identity

[英]Get-ADUser -Identity

無法將變量傳遞給 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參數接受以下內容:

  • 一個獨特的名字
  • 一個 GUID (objectGUID)
  • 安全標識符 (objectSid)
  • SAM 帳戶名稱 (sAMAccountName)

如果要基於其他屬性進行搜索,則需要使用-Filter開關。 例如,要根據 UserPrincipalName 查找用戶,您可以執行以下操作:

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

有關更多詳細信息,請參閱Get-ADUser

我知道這是一個老問題,但它可能是答案。 它可能會幫助一些人。 我遇到了同樣的問題,這讓我難住了 1 小時。 最后我使用了 $username = $username.trim() 。 所以顯然變量有需要修剪的空間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM