Reputation: 51
I have an array of hashtables to process and need the user to select one or more of them for further processing. I can get the GridView to display them correctly however I need to be able to display only 3 or 4 properties which will allow the end user to pick the correct entries and hide the rest from display.
The returned object should be the whole hashtable.
$t = @{}
$t.Name = "Test"
$t.URL = "http://xxx.test"
$t.Type = "UAT"
$RptServers = ,$t
$p = @{}
$p.Name = "Prod-MEL"
$p.URL = "http://xxx.Prod"
$p.Type = "PROD"
$RptServers += $p
$selectedSSRS = $RptServers | ForEach-Object { [PSCustomObject]$_ | Select-Object -Property 'Name','Type' } | Out-GridView -Title "Select Report Server(s)" -PassThru
$selectedSSRS
If the user selects Test the object returned has these properties:
Name Type
---- ----
Test UAT
However I would like to have the whole object with URL, etc
Upvotes: 1
Views: 2371
Reputation: 399
One liners can be elegant but cumbersome to maintain. Still, I'm here nearly four years after the fact to give this to anyone who would come here for the same reasons as me and might find it useful:
$t = [pscustomobject]@{Name = "Test";URL = "http://xxx.test";Type = "UAT"}
$p = [pscustomobject]@{Name = "Prod-MEL";URL = "http://xxx.Prod";Type = "PROD" }
$RptServers = $t,$p
$selectedSSRS = $RptServers | Where Name -in ($RptServers | Select-Object -Property Name,Type | Out-GridView -Title "Select Report Server(s)" -PassThru | Select-Object -ExpandProperty Name)
Note that for it to work, you need to have an array of objects beforehand, not build the pscustomobjects on the fly.
Upvotes: 0
Reputation: 51
So i think that I found a possible solution, however it is not very elegant and would prefer a 'one-liner'
$temp = $RptServers | ForEach-Object { $_ | Select-Object -Property 'Name','Type' } | Out-GridView -Title "Select Report Server(s)" -PassThru
$selectedSSRS = $RptServers | Where-Object { $_.Name -in $temp.Name }
$selectedSSRS
The Where-Object clause will need tweaking in actual code as just Name might not be unique enough to ensure only the correct entries are added to the selected list
Upvotes: 1
Reputation: 1187
Change the last two lines of your script to this three lines:
$selectedSSRS = $RptServers | ForEach-Object { [PSCustomObject]$_ }
$selectedSSRS | Select-Object -Property 'Name','Type' | Out-GridView -Title "Select Report Server(s)"
$selectedSSRS
The Select-Object filter is only effective on the gridview here, while $selectedSSRS has all properties
Upvotes: 0