无法将变量传递给 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/