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.
我有一个用户的网络登录名。从PowerShell和WMI可以获得该用户的有效电子邮件?请注意,登录名与电子邮件中的名称不同,因此我不能只将登录名与电子邮件域相结合。
3 个解决方案
#1
20
The simplest way is to useActive-Directory.
最简单的方法是使用Active-Directory。
As you are using PowerShell tag and not PowerShell V2.0 you can use ADSI.
由于您使用的是PowerShell标记而不是PowerShell V2.0,因此您可以使用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"]
}
You can also use userPrincipalName
instead of sAMAccountName
in the filter, for userPrincipalName
you can use user@domain form.
您还可以在过滤器中使用userPrincipalName而不是sAMAccountName,对于userPrincipalName,您可以使用user @ domain表单。
Using WMI : If you absolutly want to do it with WMI.
使用WMI:如果你绝对想用WMI做这件事。
$user2Find = "user1"
$query = "SELECT * FROM ds_user where ds_sAMAccountName='$user2find'"
$user = Get-WmiObject -Query $query -Namespace "root\Directory\LDAP"
$user.DS_mail
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.
您可以在服务器上或从域内的计算机上使用第二个解决方案,但从域外部对WMI进行身份验证会更复杂一些。
Using PowerShell 2.0
使用PowerShell 2.0
Import-Module activedirectory
$user2Find = "user1"
$user = Get-ADUser $user2Find -Properties mail
$user.mail
#2
7
Here's another possible way (original source):
这是另一种可能的方式(原始来源):
PS> [adsisearcher].FullName
System.DirectoryServices.DirectorySearcher
PS> $searcher = [adsisearcher]"(objectClass=user)"
PS> $searcher
CacheResults : True
ClientTimeout : -00:00:01
PropertyNamesOnly : False
Filter : (objectClass=user)
PageSize : 0
PropertiesToLoad : {}
ReferralChasing : External
SearchScope : Subtree
ServerPageTimeLimit : -00:00:01
ServerTimeLimit : -00:00:01
SizeLimit : 0
SearchRoot :
Sort : System.DirectoryServices.SortOption
Asynchronous : False
Tombstone : False
AttributeScopeQuery :
DerefAlias : Never
SecurityMasks : None
ExtendedDN : None
DirectorySynchronization :
VirtualListView :
Site :
Container :
PS> $searcher = [adsisearcher]"(samaccountname=$env:USERNAME)"
PS> $searcher.FindOne().Properties.mail
#3
0
Not WMI, but this may do the job just as well:
不是WMI,但这也可以做到这一点:
PS> ([adsi]"WinNT://$env:USERDOMAIN/$env:USERNAME,user").Properties["mail"]
#1
20
The simplest way is to useActive-Directory.
最简单的方法是使用Active-Directory。
As you are using PowerShell tag and not PowerShell V2.0 you can use ADSI.
由于您使用的是PowerShell标记而不是PowerShell V2.0,因此您可以使用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"]
}
You can also use userPrincipalName
instead of sAMAccountName
in the filter, for userPrincipalName
you can use user@domain form.
您还可以在过滤器中使用userPrincipalName而不是sAMAccountName,对于userPrincipalName,您可以使用user @ domain表单。
Using WMI : If you absolutly want to do it with WMI.
使用WMI:如果你绝对想用WMI做这件事。
$user2Find = "user1"
$query = "SELECT * FROM ds_user where ds_sAMAccountName='$user2find'"
$user = Get-WmiObject -Query $query -Namespace "root\Directory\LDAP"
$user.DS_mail
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.
您可以在服务器上或从域内的计算机上使用第二个解决方案,但从域外部对WMI进行身份验证会更复杂一些。
Using PowerShell 2.0
使用PowerShell 2.0
Import-Module activedirectory
$user2Find = "user1"
$user = Get-ADUser $user2Find -Properties mail
$user.mail
#2
7
Here's another possible way (original source):
这是另一种可能的方式(原始来源):
PS> [adsisearcher].FullName
System.DirectoryServices.DirectorySearcher
PS> $searcher = [adsisearcher]"(objectClass=user)"
PS> $searcher
CacheResults : True
ClientTimeout : -00:00:01
PropertyNamesOnly : False
Filter : (objectClass=user)
PageSize : 0
PropertiesToLoad : {}
ReferralChasing : External
SearchScope : Subtree
ServerPageTimeLimit : -00:00:01
ServerTimeLimit : -00:00:01
SizeLimit : 0
SearchRoot :
Sort : System.DirectoryServices.SortOption
Asynchronous : False
Tombstone : False
AttributeScopeQuery :
DerefAlias : Never
SecurityMasks : None
ExtendedDN : None
DirectorySynchronization :
VirtualListView :
Site :
Container :
PS> $searcher = [adsisearcher]"(samaccountname=$env:USERNAME)"
PS> $searcher.FindOne().Properties.mail
#3
0
Not WMI, but this may do the job just as well:
不是WMI,但这也可以做到这一点:
PS> ([adsi]"WinNT://$env:USERDOMAIN/$env:USERNAME,user").Properties["mail"]