Retrieving multiple values from UDSelect

I’m sure I’m messing something up here, but I can’t for the life of me work out what.

I’ve got a UDSelect box with a bunch of computer names and the -multiple switch set.

How do I access the selected values?

Without multiple set, I can just do (Get-UDElement -id “selectbox”).value

With multiple set I seem to be getting a Newtonsoft.Json.Linq.JArray object back.

The documentation I can find says to use (Get-UDElement -id “selectbox”).attributes[“value”] but that returns $null

Whatever attributes I try and hit I can only seem to get type data out of it. I can see under (Get-UDElement -id “selectbox”).children (and a few other places) the same number of ArrayList objects as items selected - but whenever I try and access the values they come back as $null or return TypeData

Tried using GetEnumerator() through the children, which exposes a JToken object for each selected item, but again, values in there are seemingly null or it just returns TypeData.

I’m convinced I must be doing something incredibly basic wrong.

Hello @TheAlbionist I kind of built a specific multi-select component for UD which there is much discussion on here:- New Multi or Single Select Component for UniversalDashboard just throwing this out there as an alternative without any code to work on…I hope this component makes it to your dashboard as seemed to have helped quite a few people so far obtain multi selected values :grin:
more info and function UDInput select drop down with multi-select?

This is another alternative:- Out-UDGridData with selectable rows?

If neither of these suggestions float your boat, then please share the code you got so far…Thanks and keep us posted

Hah - I’m actually using your (wonderful) module already. After installing the v3 module I found this -multiple switch and figured native support must have been added.

If I can’t figure this out, I’ll most certainly be falling back on your solution.

I’m struggling to debug that efficiently now I’m not in charge of the script host running my dashboard with v3, but I’ve found a bit out from setting a bunch of variables then inspecting at wait-debug in VSCode.

$UDElement = Get-UDElement -id “myselectbox”

$UDElement.value.gettype().fullname #comes out as type [newtonsoft.json.linq.jarray] so I’m thinking this might just be a case of me being a bit of a json noob.

$UDElement.value.children().count # Equals the number of selected items.

ForEach($child in $UDElement.value.children()) {

$child.GetEnumerator() # returns type data for JToken
$child.value # returns type data T Value[T](System.Object key)

#have even tried
$child.value.value # returns same T Value[T](System.Object key) type data

}

I’ve bashed away at whatever properties I can find as well as the above examples tbh, but seemingly only return type data or $null - can’t see why it’d return me the right number of no objects but no data, so I assume it must be me failing to expose key/value pairs properly or something.

Oh, it’s maybe relevant I’m doing this from a UDButton -onclick {} scriptblock.

I have a bunch of maintenance schedules which occasionally need weekly exclusions for a random handful of servers so I’ve got a ForEach looping through schedules that creates a UDselect populated with servers matching that schedule.

Below each select is a UDButton which needs to grab the selected values and put them in an invoke-restmethod to exclude the selected servers from the schedule.

It’s all working nicely in 2.9.0 with UDSelector - I just had my head turned when I saw the -multiple switch in the select documentation at https://docs.ironmansoftware.com/dashboard/components/inputs/select (although admittedly it’s not used in any of the examples!)

Hmm - so looks like the same problem is arising when I use UDSelector with v3x - the Get-UDSelectorValue function looks for (Get-UDElement -Id $SelectorId).Attributes.selectedOption which no longer exists.

Drilling down into (Get-UDElement -id $SelectorID).attributes returns the same sort of Newtonsoft.Json.Linq.JArray

(Get-UDElement -Id “DailyDowngridselector”).attributes | ConvertTo-Json returns a blank structure:

[
[
[
“[\r\n []\r\n] [\r\n []\r\n]”,
“[\r\n []\r\n] [\r\n []\r\n]”,
“[\r\n []\r\n] [\r\n []\r\n]”,
“[\r\n []\r\n] [\r\n []\r\n]”
]
],
[
[]
]
]

There were 4 items selected which seems to be reflected in the structure - but no data.

Sorry man what version of Powershell you running? Seen people having issues when running higher than v5…so you had the UDSelector working prior to UD v3? Im happy to post some example code, there is some splashed about on the posts I linked…let me know if you need a demo of how I obtain multiple selected values :grin:

That’s interesting re: the version.

The current dashboard is running on UniversalDashboard 2.9.0 with UDSelector (and a bunch of css to make it display properly with our team’s intranet theme :joy: ) - definitely had it working with Powershell 5 locally while I was building it - though it’s seeing out its life as an Azure Web App and I’m not entirely sure what version of the scripthost runs on there (I’d be surprised if it wasn’t 5+ though - I’ll check it when I’m logged on tomorrow)

With v2.9.0 I was doing this sort of thing to get multi-values from a UDSelector - bit crude probably, but it works. :slight_smile:

$value = Get-UDSelectorValue -SelectorID $selector
If($value -match ",")
{
#Split by , to obtain values
}
Else
{
#Single value selected, use $value
}

The Get-UDSelectorValue function returns nothing under v3 which seems to be because there’s no longer a (Get-UDElement -id $SelectorID).Attributes.selectedItem property.