PowerShell-为-Filter使用对象变量

PowerShell-为-Filter使用对象变量,powershell,powershell-5.0,Powershell,Powershell 5.0,我不知道为什么这不起作用,甚至不知道该搜索什么 我有一个简单的函数Get FileInput。它只需调用Import CSV并在传递数据之前检查特定列 我有$filterType=“Name” 我的测试TSV的格式为 db stuff Name 1 2备用40 那么我有这个 $data = Get-FileInput foreach ($computer in $data) { Get-ADComputer -Filter {$filterType -eq "comp1"} | Form

我不知道为什么这不起作用,甚至不知道该搜索什么

我有一个简单的函数Get FileInput。它只需调用Import CSV并在传递数据之前检查特定列

我有$filterType=“Name”

我的测试TSV的格式为

db stuff Name
1 2备用40

那么我有这个

$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
}
第一台计算机工作正常并输出一个表

写入主机在终端中产生输出comp1

第二个Get ADComputer运行,但不输出任何内容。

不要使用脚本块(
{…}
)作为
-Filter
参数
-它在简单情况下工作(例如,
{$filterType-eq“comp1”}
),但在更复杂的情况下会分离(
{$filterType-eq”$($computer.$filterType)}

-Filter
参数的类型为
[string]
,您应该这样构造它:

Get-ADComputer -Filter "$filterType -eq '$($computer.$filterType)'" | Format-Table
有关背景信息,请参阅我的。

不要使用脚本块(
{…}
)作为
-Filter
参数
-它在简单情况下工作(例如,
{$filterType-eq“comp1”}
),但在更复杂的情况下会崩溃(
{$filterType-eq“$($computer.$filterType)}

-Filter
参数的类型为
[string]
,您应该这样构造它:

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

有关背景信息,请参阅我的。

@Spencel:很高兴听到这个消息;我的荣幸。@Spencel:很高兴听到这个消息;我的荣幸。