Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times
Post
by ITEngineer » Wed Oct 23, 2019 10:44 pm
Hi All,
I need some help in modification of 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.
Code: Select all
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?
So any help and suggestion would be greatly appreciated.
Thank you in advance.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Wed Oct 23, 2019 11:03 pm
You cannot combine dissimilar objects. Use a select statement to select only common properties. Many properties will have to be decoded.
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times
Post
by ITEngineer » Thu Oct 24, 2019 10:44 pm
jvierra wrote: ↑ Wed Oct 23, 2019 11:03 pm
You cannot combine dissimilar objects. Use a select statement to select only common properties. Many properties will have to be decoded.
OK, thanks for the pointer, here's the update. But somehow it is still not displaying the result in the Out-Gridview?
I had to type the entry twice, once in the Console prompt, and the second in the Grid-View?
Code: Select all
Try {
Install-Module MSOnline -ErrorAction Stop
Import-Module MSOnline -ErrorAction Stop
$UserCredential = Get-Credential
Connect-MsolService -Credential $UserCredential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
}
Catch { Write-Warning "Unable to load Microsoft Office 365 module because $($Error[0])"; Exit }
## List all properties for the combined output:
$properties = @(
'EmailAddresses'
'EmailAddress'
'UserPrincipalName'
'ProxyAddresses'
)
$UPN = Read-Host -Prompt "Please enter the User Principal Name to search (wildcard accepted)"
If ($UPN) {
$search = $UPN
$MSOLActiveUsers = Get-MsolUser -All
$MSOLDeletedUsers = Get-MsolUser -All -ReturnDeletedUsers
$MSOLGroups = Get-MsolGroup -All
$MSOLContacts = Get-MsolContact -All
$MSOLRecipients =
$MSOLCombinedResults = $MSOLActiveUsers, $MSOLDeletedUsers, $MSOLGroups, $MSOLContacts, $MSOLRecipients |
Where-Object {($_.EmailAddresses -match $search) -or ($_.EmailAddress -match $search) -or ($_.UserPrincipalName -match $search) -or ($_.ProxyAddresses -match $search)} |
ForEach-Object {$_ | Select-Object -Property $properties}
Switch (@($MSOLCombinedResults.Count)) {
0 {Write-Warning "No user account with a SamAccountName matching '$($search)' found!"}
1 {$MSOLCombinedResults}
default {$MSOLCombinedResults | Out-GridView -Title "Please select a user" -OutputMode Single}
}
}
Would you be able to help?
I'm hitting a road block here
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Thu Oct 24, 2019 11:09 pm
I repeat - you cannot combine dissimilar results and two fo the properties are arrays and two may be complex objects that require a computed select.
Try just placing only one result in the grid. Inspect what is returned.
Get-MsolUser -All | Select $properties | Out-GridView
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Thu Oct 24, 2019 11:20 pm
Now try this to see how to aggregate objects but remember that you may get many blank fields.
Code: Select all
$results = Get-MsolUser -All
$results += Get-MsolUser -All -ReturnDeletedUsers
$results += Get-MsolGroup -All
$results += Get-MsolContact -All
$results | select $properties |Out-Gridview
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Thu Oct 24, 2019 11:27 pm
This will get you closer:
Code: Select all
$results = Get-MsolUser -All
$results += Get-MsolUser -All -ReturnDeletedUsers
$results += Get-MsolGroup -All
$results += Get-MsolContact -All
$results |
Where-Object{
$_.EmailAddresses -match $search -or
$_.EmailAddress -match $search -or
$_.UserPrincipalName -match $search -or
$_.ProxyAddresses -match $search
} |
Select-Object $properties |
Out-GridView
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times
Post
by ITEngineer » Fri Oct 25, 2019 12:41 am
jvierra wrote: ↑ Thu Oct 24, 2019 11:27 pm
This will get you closer:
Code: Select all
$results = Get-MsolUser -All
$results += Get-MsolUser -All -ReturnDeletedUsers
$results += Get-MsolGroup -All
$results += Get-MsolContact -All
$results |
Where-Object{
$_.EmailAddresses -match $search -or
$_.EmailAddress -match $search -or
$_.UserPrincipalName -match $search -or
$_.ProxyAddresses -match $search
} |
Select-Object $properties |
Out-GridView
Thanks Mr. Vierra,
yes that does works, so how to expand all the result since the result in the Out-Gridview is all clipped I cannot extend the column length?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times
Post
by jvierra » Fri Oct 25, 2019 1:00 am
You can't. GridView does not work like that. The best you can do is to join the arrays on a line break.
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times
Post
by ITEngineer » Sun Oct 27, 2019 3:34 pm
jvierra wrote: ↑ Fri Oct 25, 2019 1:00 am
You can't. GridView does not work like that. The best you can do is to join the arrays on a line break.
Mr. Vierra,
Thanks for the pointer, now it is throwing some other error:
Code: Select all
## List all properties for the combined output:
$properties = @(
'EmailAddresses'
'EmailAddress'
'UserPrincipalName'
@{n='ProxyAddresses'; e={$_.ProxyAddresses -join ', '}}
)
$UPN = Read-Host -Prompt "Please enter the User Principal Name to search (wildcard accepted)"
If ($UPN) {
$search = $UPN
$results = Get-MsolUser -All
$results += Get-MsolUser -All -ReturnDeletedUsers
$results += Get-MsolGroup -All
$results += Get-MsolContact -All
$results += Get-Recipient -ResultSize Unlimited
$results |
Where-Object{
$_.EmailAddresses -match $search -or
$_.EmailAddress -match $search -or
$_.UserPrincipalName -match $search -or
$_.ProxyAddresses -match $search
} |
Select-Object $properties |
Out-GridView
}
The error is:
Code: Select all
parsing "*kaile*" - Quantifier {x,y} following nothing.
At line:22 char:7
+ $_.EmailAddresses -match $search -or
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
When I tried to search the user:
Kailey
Kailesh
Kailendra
/* IT Engineer */
This topic is 4 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked