$next is illegal in that context. PowerShell won’t evaluate that as code in the way you’re thinking, no. You’d have to put your entire comparison into a variable and then use Invoke-Expression - which is also vulnerable to a number of different injection attacks if any of your data is coming from external sources.
OK thanks. All of this is run internally so that should not be a problem. Could you give me a example of what you mean by chance? I have a better time understanding it if I see an example.
To use Don’s earlier suggestion. If you would use a listbox enabled for multi-selection in your GUI instead of checkboxes for the years. It would be very easy to implement the filter without using Invoke-Expression which can become dangerous if used incorrectly.
Example code:
# Dummy source data converted into an array of objects
$tracks = @'
Name,Year
Space Oddity,1969
Heroes,1977
Under Pressure,1982
Lazarus,2016
'@ | ConvertFrom-Csv
Add-Type -AssemblyName System.Windows.Forms
$listbox = New-Object -TypeName System.Windows.Forms.ListBox
$listbox.SelectionMode = [System.Windows.Forms.SelectionMode]::MultiSimple
# Add some items to our listbox
$tracks.Year | ForEach-Object {
[void]($listbox.Items.Add($_))
}
# Select a couple of items because this example doesn't have a UI
$listbox.SetSelected(0, $true)
$listbox.SetSelected(3, $true)
# Actual code to filter the tracks
$tracks | Where-Object { $_.Year -in $listbox.SelectedItems }
Thanks for all your help, reading the the docs for Invoke-Expression does seem to be exactly what I need but it does not seem to be reading $tracks.
$express = "$tracks | where { $_.Year -like '1986' }"
Invoke-Expression $express
Invoke-Expression : At line:1 char:8
+ | where { .Year -like '1986' }
+ ~
An empty pipe element is not allowed.
At line:2 char:1
+ Invoke-Expression $express
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
+ FullyQualifiedErrorId : EmptyPipeElement,Microsoft.PowerShell.Commands.InvokeExpressionCommand
Do I have to pass $tracks somehow?
Viewing the $express variable just shows:
$express
| where { .Year -like '1986' }
Viewing $tracks does show content so I am not sure what I am doing wrong.
EDIT: Thanks Daniel Krebs, I think I was typing as the same time as you were responding. I will try that out but would still like to know what I did wrong above just so I know
The dollar-signs in Don’s Invoke-Expression example need to be escaped with a backtick to avoid early variable expansion into the $express string.
# Just some example data
$tracks = @'
Name,Year
Space Oddity,1969
Heroes,1977
Under Pressure,1982
Lazarus,2016
'@ | ConvertFrom-Csv
$express = "`$tracks | where { `$_.Year -like '1969' -or `$_.Year -like '2016' }"
Invoke-Expression $express
I got it working both ways just to make sure I understood both methods. Thanks to both of you for all the help!!
I went with the Listbox method which you are right is so much easier, I over complicated it a bit I think…lol
I do have another related question though. I want to be able to pick from a list of Genres and Years (2 separate Listboxes) and have it output whatever matches. I was able to get it to populate the list of Genres automatically but wanted to use a range for the years so I just put them in manually. What I have below works just fine but only if you select at least one entry from each of the 2 Listboxes. I don’t know how to tell it to ignore that part of the statement if nothing is selected:
LOL…yeah I am still working on it and its going great. I am using this to learn more as I am still very new to PS.
So basically I would create a list of $songs from the Genre selections then use that filtered list to list the year(s) that match in that filter list, right? That makes sense.
I was thinking I had to have it done all at once.