通过PowerShell和WMI得到的用户名用户的电子邮件地址? [英] Get a user's email address from the username via PowerShell and WMI?

查看:280
本文介绍了通过PowerShell和WMI得到的用户名用户的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户的网络登录名。从PowerShell和WMI是有可能得到该用户的一个有效的电子邮件?请注意,登录名是不是在电子邮件中的名称不同,所以我不能只是结合登录名与电子邮件域。

I have a user's network login name. From PowerShell and WMI is it possible to get a valid email for that user? Note that the login name is different than the name in the email, so I can't just combine the login name with the email domain.

推荐答案

最简单的方法是useActive目录。

当你使用PowerShell的标签,而不是PowerShell的2.0版,您可以使用ADSI。

As you are using PowerShell tag and not PowerShell V2.0 you can use ADSI.

Clear-Host
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://WM2008R2ENT:389/dc=dom,dc=fr","jpb@dom.fr","Pwd")

# Look for a user
$user2Find = "user1"
$Rech = new-object System.DirectoryServices.DirectorySearcher($dn)
$rc = $Rech.filter = "((sAMAccountName=$user2Find))"
$rc = $Rech.SearchScope = "subtree"
$rc = $Rech.PropertiesToLoad.Add("mail");

$theUser = $Rech.FindOne()
if ($theUser -ne $null)
{
  Write-Host $theUser.Properties["mail"]
}

您也可以使用的UserPrincipalName 而不是 sAMAccountName赋的过滤器,的UserPrincipalName 可以使用的为user @ domain 的形式。

You can also use userPrincipalName instead of sAMAccountName in the filter, for userPrincipalName you can use user@domain form.

使用WMI :如果你是绝对要与WMI做到这一点

Using WMI : If you absolutly want to do it with WMI.

$user2Find = "user1"
$query = "SELECT * FROM ds_user where ds_sAMAccountName='$user2find'"
$user = Get-WmiObject -Query $query -Namespace "root\Directory\LDAP"
$user.DS_mail

您可以使用localy在您的服务器或计算机领域内的第二个解决方案,但它更复杂一些,从外域身份验证WMI。

You can use the second solution localy on your server or from a computer inside the domain, but it's a bit more complicated to authenticate to WMI from outside the domain.

中使用PowerShell 2.0

Import-Module activedirectory
$user2Find = "user1"
$user = Get-ADUser $user2Find -Properties mail
$user.mail

这篇关于通过PowerShell和WMI得到的用户名用户的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆