为什么“在哪里”不能像我期望的那样选择某些结果呢?

时间:2017-04-05 17:10:08

标签: powershell powershell-v4.0

我正在运行以下PowerShell,我的问题是为什么第二个命令没有返回任何结果?另外,我应该使用where还是Where-Object?见下面的截图。

Write-Host "This displays all..." -ForegroundColor Green
Get-Command -Module "Microsoft.TeamFoundation.PowerShell"

Write-Host "This displays nothing..." -ForegroundColor Yellow
Get-Command -Module "Microsoft.TeamFoundation.PowerShell" | Where-Object ($_.Name -like '*Tfs*')

enter image description here

1 个答案:

答案 0 :(得分:0)

Where-Object需要脚本块或比较语句(3.0+)。

在您的情况下,用大括号替换括号将起作用:

Get-Command -Module "Microsoft.TeamFoundation.PowerShell" |
    Where-Object {$_.Name -like '*Tfs*'}

或者您可以使用比较声明:

Get-Command -Module "Microsoft.TeamFoundation.PowerShell" |
    Where-Object -Property Name -like '*Tfs*'