首页 文章

PowerShell - 为-Filter使用对象变量

提问于
浏览
0

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

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

我有$ filterType =“名字”

我的测试TSV采用的格式

db stuff Name 1 2 spare40

然后我有这个

$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
}

第一台Get-ADComputer正常工作并输出一个表格 .

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

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

1 回答

  • 2

    Do not use a script block () as the -Filter argument - 它在简单的情况下工作(例如, {$filterType -eq "comp1"} ),但在更复杂的情况下崩溃( {$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
    

    有关背景信息,请参阅我的this answer .

相关问题