Author Posts

July 7, 2017 at 9:13 am

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 ?

July 7, 2017 at 9:29 am

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

July 7, 2017 at 11:04 am

Put single quotes around your value.

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

July 7, 2017 at 11:08 am

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

July 8, 2017 at 4:46 pm

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:\>