Get value from New-UDSelect

Hi,

I am trying the get the result from the select box and then get the value to be processed by my sql query next. However I just cannot get the value out? I have read the document on New-UDSelect and tried a few on the forum , still not working.

Here is my code:

New-UDSelect -Option {
New-UDSelectOption -Name ‘Server Patchlist’ -Value “patchserver”
New-UDSelectOption -Name ‘Internal Workspace’ -Value “internalpatch”
New-UDSelectOption -Name ‘External Workspace’ -Value “externalpatch”
New-UDSelectOption -Name ‘Server Baseline’ -Value “serverbaseline”
New-UDSelectOption -Name ‘Workstation Baseline’ -Value “workstationbaseline”
} -Id ‘select’ -DefaultValue “patchserver” -OnChange {$Session:test = $EventData}

Method 1:
$Session:test = (Get-UDElement -Id ‘select’)[“value”]

Method 2:
$Element = Get-UDElement -Id ‘select’

if ($Element.Value)
{
    New-UDTypography -Text $Element.Value
}

else 
{
    New-UDTypography -Text $Element.DefaultValue
}

Method 3:
$test = ([string]$eventdata ).Trim( ‘"’ )

Method 4:
$test = (Get-UDElement -Id ‘select’)[“value”]

New-UDTypography -Text $Session:test

Thanks so much !

Hello @qy2009 I cannot answer your direct question, however I can show you something I contributed a while back Ironman Software Marketplace - UniversalDashboard.UDSelector which shows you how to do this…If you only want a single selection then you can use Ironman Software Marketplace - UniversalDashboard.UDSingleSelector I do need to get round to updating this component building it directly for PSU…but this should work for you, so kind of answering your question on how I would and have gone about doing this exact thing in the past…I hope this helps :smile:

Hello @qy2009

If this in a form, have you tried querying $Eventdata variable to see if it is returning anything?

Hi, how do you give the new value to your sql query ?

@psDevUK I love your UniversalDashboard.UDSingleSelector, the UI looks soooo nice :slight_smile:
I have read the document, looks like it is for UDSelector? Also, is there a way to set the default selection?

I have tried the UDSingleSelctor, however it still didn’t work ;(
I need the value to be set inside the SQL query.

Here is my code:

Import-Module dbatools

$sqlserver = ‘xxx’

$database = ‘xxx’

$siteserver = ‘xxx’

$sitecode = ‘xxx’

New-UDSingleSelector -Id “stuff” -options {

@{ value = "patchserver"; label = "Server Patchlist" },

@{ value = "internalpatch"; label = "Internal Workspace" },

@{ value = "externalpatch"; label = "External Workspace" },

@{ value = "serverbaseline"; label = "Server Baseline" }

@{ value = "workstationbaseline"; label = "Workstation Baseline" }

}

$selection = (Get-UDElement -id “stuff”).Attributes.selectedOption

$query = "

SELECT xxx

FROM dbo.vSMS_SUMDeploymentStatusPerAsset

WHERE (AssignmentName = N'$selection’) AND (LastComplianceMessageDesc = N’Non-compliant’)

"

$data = Invoke-DbaQuery -SqlInstance $sqlserver -Database $database -Query $query | ForEach-Object {

@{ 

    DeviceName = $_.DeviceName 

    LastComplianceMessageDesc = $_.LastComplianceMessageDesc

    LastComplianceMessageTime = $_.LastComplianceMessageTime

    LastEnforcementMessageTime = $_.LastEnforcementMessageTime

    LastMessage = $_.LastMessage

}

}

$columns = @(

New-UDTableColumn -Property DeviceName -Title Name

New-UDTableColumn -Property LastComplianceMessageDesc -Title Compliance 

New-UDTableColumn -Property LastComplianceMessageTime -Title ReportTimeatus 

New-UDTableColumn -Property LastEnforcementMessageTime -Title LastMessageTime 

New-UDTableColumn -Property LastMessage -Title LastMessage 

)

#New-UDStyle -Style '

#    font-size: 11px;

#    .MuiTableCell-root {

#    padding: 0px;

#}' -Content {

    New-UDTable -Data $data -Columns $columns

#}

@Mike27 I tried with $Eventdata, but it got nothing in return

@sandy37 I am using this to get the value inside my sql query:

WHERE (AssignmentName = N '$selection ’) AND (LastComplianceMessageDesc = N’Non-compliant’)

The format is a bit messy in this editor. it is ` , and then ’ to get the final result looks like N’value’

Hello @qy2009 yes you can PRE-SELECT stuff

New-UDSelector -Id "stuff" -Selected {
                    @{ value = "push"; label = "Push" },
                    @{ value = "pull"; label = "Pull" }
                } -options {
                    @{ value = "push"; label = "Push" },
                    @{ value = "pull"; label = "Pull" },
                    @{ value = "jump"; label = "Jump" },
                    @{ value = "throw"; label = "Throw" }
                    @{ value = "kick"; label = "Kick" }
                    @{ value = "punch"; label = "Punch" }
                }

Notice the -Selected parameter :slight_smile:
Source GitHub - psDevUK/UD-Selector

I can put an example together on getting the value…there is an easier way to do the SQL stuff…This drop down list can handle thousands of results from SQL…Need to find a script I was using…there is a lot of info on this thread on the forum:-

Boom…so glad I document stuff…please see the SQL sample in this README

Hi @psDevUK
I was using the New-UDSingleSelector, and that one doesn’t have “-Selected” parameter?

also, how can I get the value out? I was using
$selection = (Get-UDElement -id “stuff”).Attributes.selectedOption
and
$selection = (Get-UDElement -id “stuff”).Attributes.selectedOption | ConvertTo-Json | ConvertFrom-Json

New-UDTypography -Text $selection

but it returned nothing?

thanks so much :slight_smile:

I understand there was issues using this and PS7. So first question is are you using powershell 7?
Just want to verify that from using the above code, you did give the -id paramater the name "stuff"
The other week I finished my job…so I need to get PSU install on the wifes laptop as she got x64 and I am still on x86
I got a good few weeks till I start my next job, so I think it would make sense for me to rebuild both these components, include the additional -selected parameter for the single selection…I am also then hoping the light dark mode will work with it…
I know people do have this working in PSU, but I only ran it in UD…will attempt on Monday to get this up and running on the wifes laptop…so I can send an official answer…
You did try the function yeah?

function Get-UDSelectorValue {
param($SelectorId)
$value = (((Get-UDElement -Id $SelectorId).Attributes.selectedOption | Select-Object Root -ErrorAction SilentlyContinue) | ConvertTo-Json -Depth 2 | ConvertFrom-Json | Select-Object -ExpandProperty Root -ErrorAction SilentlyContinue) | Select-Object -First 1
if (!$value) {
$val = ((Get-UDElement -Id $SelectorId).Attributes.selectedOption | ConvertTo-Json -Depth 1 | ConvertFrom-Json | Select-Object -ExpandProperty SyncRoot) | Select-Object -ExpandProperty SyncRoot
$length = $val.length
$i = 0
Do {
if (($i % 2) -eq 0) {
$value += "'$($val[$i])'" + ","
}
$i++
}
While ($i -le $length)
$value = $value.TrimEnd(",")
}
return $value
}

This is documented in the above link I sent. This should work…

Hi @psDevUK ,

I am using Powershell 5.1 on win 2016 x64 system.
I have added this function, but still get nothing?

$test = Get-UDSelectorValue  -SelectorId 'stuff'
New-UDTypography -Text $test

Is that the correct way to use this function?

thanks so much :slight_smile:

Hi @qy2009 I have dealt with people running into issues whilst trying to use this component, but they seem to come through in the end, and get it working…I am seeing some slight syntax differences from how you are trying to read the value to how I have it documented in the link in the above post to my github page. I will attempt to get PSU running on my wifes laptop tomorrow and post a full on solution, if the demo files in the documentation are not working, as that was written for UD not PSU, so some slight adjustments might be required…I cannot promise this though as I also got four daughters to entertain tomorrow as well :smile:
Not sure if the code I got in this post will help?

@psDevUK I realized that we were talking different things, I was testing with the UD-SingleSelector the whole time, and you were talking about the UD-Selector. I am really sorry about that. My mistake :slight_smile:

I found these two codes can get the value out from the selection:

$UDElement = Get-UDElement -id "stuff"
$Value=$UDElement.Attributes.selectedOption.value

Maybe you can update the document on UD-SingleSelector in future ? Then less people will be confused. Also, it will be really appreciated if you can add the default value parameter for UD-SingleSelector.

Enjoy your time with your family please! Family always come first :slight_smile:
At least I can start working on the next few things now.

Really really appreciate all the help, @psDevUK !

1 Like