PowerShell 根据特定通配符搜索列出 UPN
PowerShell to list of UPNs based on specific Wildcard search
我需要修改下面的脚本,它部分有效。
主要是查询Office 365 Users, Contact, Groups and Deleted users,然后匹配用户输入,找到后显示在Out-GridView上。
Try {
Install-Module MSOnline -ErrorAction Stop
Import-Module MSOnline -ErrorAction Stop
$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential
}
Catch { Write-Warning "Unable to load Microsoft Office 365 module because $($Error[0])"; Exit }
$UPN = Read-Host -Prompt "Please enter the User Principal Name to search (wildcard accepted)"
If ($UPN) {
$UPN = $search
$MSOLActiveUsers = Get-MsolUser -All
$MSOLDeletedUsers = Get-MsolUser -All -ReturnDeletedUsers
$MSOLGroups = Get-MsolGroup -All
$MSOLContacts = Get-MsolContact -All
$MSOLRecipients = Get-Recipient -ResultSize Unlimited
$MSOLCombinedResults = $MSOLActiveUsers + $MSOLDeletedUsers + $MSOLGroups + $MSOLContacts + $MSOLRecipients
$MSOLCombinedResults | Where-Object { $_.emailaddresses -match $search -or $_.emailaddress -match $search -or $_.userprincipalname -eq $search -or $_.proxyaddresses -match $search }
Switch ($MSOLCombinedResults.Count) {
0 { Write-Warning "No user account with a SamAccountName matching '$($UPN)' found!" }
1 { $MSOLCombinedResults }
default { $MSOLCombinedResults | Out-GridView -Title "Please select a user" -OutputMode Single }
}
}
上述脚本的问题是结果总是没有意义的长 Gridview?
注意:您的脚本缺少连接到 Exchange Online,这是 Get-Recipient
所必需的,但我假设您这样做了,只是忘了添加到您的问题中.
首先,您在这一行过滤 $MSOLCombinedResults
:
$MSOLCombinedResults | Where-Object (...)
但随后您通过管道传输到 Out-GridView
未过滤的数组:
$MSOLCombinedResults | Out-GridView (...)
你忘记的是保存过滤后的数组,然后对其进行操作。 PowerShell 不会自动执行此操作,因此您应该使用以下内容:
$filteredResults = $MSOLCombinedResults | Where-Object { $_.emailaddresses -match $search -or $_.emailaddress -match $search -or $_.userprincipalname -eq $search -or $_.proxyaddresses -match $search }
Switch ($filteredResults .Count) {
# do something
}
另一件事是 $MSOLCombinedResults
包含多种类型的对象,因此您应该 select 在输出它之前需要的属性(出于性能原因,最好 select 它们甚至早些时候)。
您感兴趣的数据如下:
- 来自
Get-MsolUser
:UserPrincipalName(字符串)、ProxyAddresses(数组)
- 发件人
Get-MsolGroup
:EmailAddress(字符串)、ProxyAddresses(数组)
- 来自
Get-MsolContact
:EmailAddress(字符串)、ProxyAddresses(数组,可选,因为我通常看不到这个值集)
- 来自
Get-Recipient
:EmailAddresses(数组,注意复数),ExternalEmailAddress(SMTP:xxx
格式的字符串),PrimarySmtpAddress(字符串)
您必须相应地修改过滤器,然后使用 Select-Object
到 Out-GridView
输出所需的结果。看一下 calculated properties,因为您可能想要 rename/convert 一些属性以便于过滤。
我需要修改下面的脚本,它部分有效。
主要是查询Office 365 Users, Contact, Groups and Deleted users,然后匹配用户输入,找到后显示在Out-GridView上。
Try {
Install-Module MSOnline -ErrorAction Stop
Import-Module MSOnline -ErrorAction Stop
$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential
}
Catch { Write-Warning "Unable to load Microsoft Office 365 module because $($Error[0])"; Exit }
$UPN = Read-Host -Prompt "Please enter the User Principal Name to search (wildcard accepted)"
If ($UPN) {
$UPN = $search
$MSOLActiveUsers = Get-MsolUser -All
$MSOLDeletedUsers = Get-MsolUser -All -ReturnDeletedUsers
$MSOLGroups = Get-MsolGroup -All
$MSOLContacts = Get-MsolContact -All
$MSOLRecipients = Get-Recipient -ResultSize Unlimited
$MSOLCombinedResults = $MSOLActiveUsers + $MSOLDeletedUsers + $MSOLGroups + $MSOLContacts + $MSOLRecipients
$MSOLCombinedResults | Where-Object { $_.emailaddresses -match $search -or $_.emailaddress -match $search -or $_.userprincipalname -eq $search -or $_.proxyaddresses -match $search }
Switch ($MSOLCombinedResults.Count) {
0 { Write-Warning "No user account with a SamAccountName matching '$($UPN)' found!" }
1 { $MSOLCombinedResults }
default { $MSOLCombinedResults | Out-GridView -Title "Please select a user" -OutputMode Single }
}
}
上述脚本的问题是结果总是没有意义的长 Gridview?
注意:您的脚本缺少连接到 Exchange Online,这是 Get-Recipient
所必需的,但我假设您这样做了,只是忘了添加到您的问题中.
首先,您在这一行过滤 $MSOLCombinedResults
:
$MSOLCombinedResults | Where-Object (...)
但随后您通过管道传输到 Out-GridView
未过滤的数组:
$MSOLCombinedResults | Out-GridView (...)
你忘记的是保存过滤后的数组,然后对其进行操作。 PowerShell 不会自动执行此操作,因此您应该使用以下内容:
$filteredResults = $MSOLCombinedResults | Where-Object { $_.emailaddresses -match $search -or $_.emailaddress -match $search -or $_.userprincipalname -eq $search -or $_.proxyaddresses -match $search }
Switch ($filteredResults .Count) {
# do something
}
另一件事是 $MSOLCombinedResults
包含多种类型的对象,因此您应该 select 在输出它之前需要的属性(出于性能原因,最好 select 它们甚至早些时候)。
您感兴趣的数据如下:
- 来自
Get-MsolUser
:UserPrincipalName(字符串)、ProxyAddresses(数组) - 发件人
Get-MsolGroup
:EmailAddress(字符串)、ProxyAddresses(数组) - 来自
Get-MsolContact
:EmailAddress(字符串)、ProxyAddresses(数组,可选,因为我通常看不到这个值集) - 来自
Get-Recipient
:EmailAddresses(数组,注意复数),ExternalEmailAddress(SMTP:xxx
格式的字符串),PrimarySmtpAddress(字符串)
您必须相应地修改过滤器,然后使用 Select-Object
到 Out-GridView
输出所需的结果。看一下 calculated properties,因为您可能想要 rename/convert 一些属性以便于过滤。