Passing a variable as a where clause

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of js js 2 weeks, 6 days ago.

  • Author
    Posts
  • #74416
    Profile photo of Simon B
    Simon B
    Participant

    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 ?

  • #74422
    Profile photo of Simon B
    Simon B
    Participant

    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

  • #74437
    Profile photo of random commandline
    random commandline
    Participant

    Put single quotes around your value.

    $sqlfilter = '$_.product -eq "Windows 10 for 32-bit Systems"|"Windows Server 2016" -AND $_.Sev -eq "Critical"'
    
  • #74438
    Profile photo of Simon B
    Simon B
    Participant

    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

  • #74489
    Profile photo of js
    js
    Participant

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

You must be logged in to reply to this topic.