Reputation: 1141
I need to modify the below script, which is partially working.
It basically queries the Office 365 Users, Contact, Groups and Deleted users, and then match it with the user input, then display it on Out-GridView when found.
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 }
}
}
The issue with the above script is the result is always meaningless long Gridview?
Upvotes: 0
Views: 1376
Reputation: 5227
NOTE: your script is missing connecting to Exchange Online, which is required for Get-Recipient
but I assume you did this, but just forgot to add to your question.
First of all, you filter $MSOLCombinedResults
in this line:
$MSOLCombinedResults | Where-Object (...)
But then you pipe to Out-GridView
unfiltered array:
$MSOLCombinedResults | Out-GridView (...)
What you forgot is to save filtered array and then operate on it. PowerShell doesn't do that automatically, so you should go with something like:
$filteredResults = $MSOLCombinedResults | Where-Object { $_.emailaddresses -match $search -or $_.emailaddress -match $search -or $_.userprincipalname -eq $search -or $_.proxyaddresses -match $search }
Switch ($filteredResults .Count) {
# do something
}
Another thing is that $MSOLCombinedResults
contains multiple types of object, so you should select desired properties before outputting it (for performance reasons it'd be nice to select them even earlier).
The data you'd be interested in is as follows:
Get-MsolUser
: UserPrincipalName (string), ProxyAddresses (array)Get-MsolGroup
: EmailAddress (string), ProxyAddresses (array)Get-MsolContact
: EmailAddress (string), ProxyAddresses (array, optional as I don't see this value set usually)Get-Recipient
: EmailAddresses (array, notice plural), ExternalEmailAddress (string in SMTP:xxx
format), PrimarySmtpAddress (string)You have to modify your filter accordingly and then output desired results using Select-Object
to Out-GridView
. Take a look at calculated properties as you might want to rename/convert some properties for easier filtering.
Upvotes: 1