Passing a variable as a where clause

I have the following code
$sqlfilter | out-file c:\temp\filter.txt
$myexcel | Where-object{$.product -eq “Windows 10 for 32-bit Systems” -or $.product -eq “Windows Server 2016” -and $_.Sev -eq “Critical”} | Export-Csv $outfile

$myexcel | Where-object{$sqlfilter} | Export-Csv $outfile

when I have the filter hard coded it works fine. But when I use the variable $sqlfiler it does not filter anything

The contents of the $sqlfilter are
$.product -eq “Windows 10 for 32-bit Systems” -or $.product -eq “Windows Server 2016” -and $_.Sev -eq “Critical”
when I look at both the contents of the $sqlfilter and the hard coded one I can see a difference.

Any ideas ?

I have tried some tests from the console and the results are below :-
PS M:> $filepath = $csvtofilter
$myexcel = Import-CSV $filepath -header Product,kb,sev

PS M:> $sqlfilter
$.product -eq “Windows 10 for 32-bit Systems” -or $.product -eq “Windows Serve
r 2016” -and $_.Sev -eq “Critical”

PS M:> $myexcel | where-object {$.product -eq “Windows 10 for 32-bit Systems” -or $.product -eq “Windows Serve
r 2016” -and $_.Sev -eq “Critical”
}

Product kb sev


Windows 10 for 32-bit Systems 4022727 (Security Update) Critical
Windows 10 for 32-bit Systems 4022727 (Security Update) Critical
Windows 10 for 32-bit Systems 4022727 (Security Update) Critical
Windows 10 for 32-bit Systems 4022727 (Security Update) Critical
Windows 10 for 32-bit Systems 4022727 (Security Update) Critical
Windows 10 for 32-bit Systems 4022727 (Security Update) Critical
Windows 10 for 32-bit Systems 4022727 (Security Update) Critical

PS M:> $myexcel | where-object $sqlfilter

PS M:>

If I put $myexcel | where-object {$sqlfilter}

It does not filter and returns the whole content of $myexcel

Put single quotes around your value.

$sqlfilter = '$_.product -eq "Windows 10 for 32-bit Systems"|"Windows Server 2016" -AND $_.Sev -eq "Critical"'

I have managed to sole this but putting the $sqlfilter into a script block

$whereblock = [scriptblock]::Create($sqlfilter)
$myexcel | Where-object $whereblock | Export-Csv $outfile

Hmm, this example works for me:

PS C:\> $a = { $_ -match 'hi' }
PS C:\> $a.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     ScriptBlock                              System.Object


PS C:\> echo hi | where $a
hi
PS C:\> echo h | where $a
PS C:\>