re-gor
re-gor

Reputation: 1342

How to use function in Where-Object

I want filter a list of objects by property "innerText". But I need to do some preparations. Why furhter code doesn't works? It returns all objects.

function enc[[string]$inp]
{
    return [System.Text.Encoding]::GetEncoding("windows-1251").GetString([System.Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($inp))
}

$req.Links | Where-Object { enc($_.innerText) -eq "my string"} | fl

What I'm doing wrong? Unfortunately I didn't find the necessary article in the Internet. There are a lot of such examples: ($_.Name -eq "name") - and nothing valueable for me.

Upvotes: 1

Views: 2914

Answers (1)

Micky Balladelli
Micky Balladelli

Reputation: 9991

I think the issue is how you defined your function.

function enc
{
    param ([string]$inp)

    return [System.Text.Encoding]::GetEncoding("windows-1251").GetString([System.Text.Encoding]::GetEncoding("ISO-8859-1").GetBytes($inp))
}

$req.Links | Where-Object { (enc $_.innerText) -eq "my string"} | fl

Simple test:

$a = "hello","world"
function enc
{
    param ([string]$inp)
    $inp
}

$a | Where-Object { (enc $_) -eq "hello"}

Upvotes: 5

Related Questions