如何在 PowerShell Core 中获取当前登录用户的 Active Directory 电子邮件地址?

问题描述 投票:0回答:2

我在这里看到了其他问题/答案,展示了如何在 Windows PowerShell 中执行此操作,它们可以在 Windows PowerShell 中工作,但不能在 PowerShell 6+(即构建在 .NET Core 之上的 PowerShell Core)中工作。

如何在 PowerShell Core 中获取已加入域的当前用户的 Active Directory 电子邮件地址?

如果它们没有加入 AD 域,那么它做什么并不重要(抛出错误,返回空字符串,等等)。

谢谢!

我在这里查看了示例:如何在powershell中获取登录用户的电子邮件,但是“

[adsi]
”类型不可用。

我考虑过

"$env:username@$env:userdnsdomain"
,但这给出了错误的结果。

谢谢!

注意:这是在 Windows 中(特别是 Windows 10)。我需要用户的电子邮件地址才能使用 ADAL 生成身份验证令牌。在 .NET 中它可以在没有用户电子邮件地址的情况下完成此操作,但在 .NET Core 中则不行。

例如,我有一个登录名是

m8345
的用户。他的电子邮件地址可以是“
[email protected]
”或“
[email protected]
”或“
[email protected]
”或“
[email protected]
”等,即登录名之间不一定有任何关联(即“%username%” ") 和电子邮件地址...但是 .NET 可以解析它,但 .NET core 不能。

.net-core powershell-core
2个回答
2
投票

经过一番折腾,我终于明白了:

([ADSI]"LDAP://<SID=$([Security.Principal.WindowsIdentity]::GetCurrent().User.Value)>").UserPrincipalName

0
投票

另一种方法使用 ADSystemInfo COM 对象 检索用户的 DistinguishedName,然后检索“mail”属性:

([ADSI]"LDAP://$([System.__ComObject].InvokeMember("UserName", [System.Reflection.BindingFlags]::GetProperty, $null, (New-Object -ComObject "ADSystemInfo"), $null))").mail
© www.soinside.com 2019 - 2024. All rights reserved.