0

I'm at a loss as to why this isn't working, and what to even search for.

I have a simple function Get-FileInput. It simply calls Import-CSV and checks for a specific column before passing on the data.

I have $filterType = "Name"

My test TSV is in the format

db stuff Name 1 2 spare40

Then I have this

$data = Get-FileInput
foreach ($computer in $data)
{
    Get-ADComputer -Filter {$filterType -eq "comp1"} | Format-Table
    Write-Host "$($computer.$filterType)"
    Get-ADComputer -Filter {$filterType -eq "$($computer.$filterType)"} | Format-Table
}

The first Get-ADComputer works fine and outputs a table.

The Write-Host produces the output comp1 in the terminal.

The second Get-ADComputer runs, but does not output anything.

1 Answer 1

2

Do not use a script block ({...}) as the -Filter argument - it works in simple cases (e.g., {$filterType -eq "comp1"}) but falls apart in more complex ones ({$filterType -eq "$($computer.$filterType)"}).

The -Filter argument is of type [string], and you should construct it as such:

Get-ADComputer -Filter "$filterType -eq '$($computer.$filterType)'" | Format-Table

For background information, see this answer of mine.

Sign up to request clarification or add additional context in comments.

1 Comment

@Spencel: Glad to hear it; my pleasure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.