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.

Spencel
  • 80
  • 1
  • 9

1 Answers1

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775