过滤Invoke-RestMethod结果

zoo*_*nce 2 powershell powershell-2.0

我通过GET从我的应用程序中提取有关提供者的一些细节Invoke-RestMethod.目前它正在返回有关提供商的所有详细信息.我想只返回活动状态设置为True的提供程序代码.

$acctname = 'user1'
$password = 'secret'

$params = @{uri = 'http://localhost:8080/tryout/settings/provider/providerDetails';
                   Method = 'Get';
                   Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($acctname):$($password)"))
           } #end headers hash table
   } #end $params hash table


# This gets all the basic info ok
$var = invoke-restmethod @params

#show the values in the console
echo $var
Run Code Online (Sandbox Code Playgroud)

它目前返回所有这些细节.我需要的只是代码,如果active-true.

id            : 90
name          : Test 1
active        : True
code          : NOT_STATED
system        : False
objectVersion : 2

id            : 91
name          : Test 2
active        : True
code          : MAIN
system        : False
objectVersion : 3

id            : 20372
name          : Test 3
active        : True
code          : NOT_STATED
system        : True
objectVersion : 2

id            : 30382
name          : Test 4
active        : True
code          : OP
system        : False
objectVersion : 1
Run Code Online (Sandbox Code Playgroud)

Mar*_*ndl 6

只需管道 $varWhere-Objectcmdlet并过滤它们:

$var | Where-Object active -eq 'True'
Run Code Online (Sandbox Code Playgroud)

  • 如果字符串包含空格; 报价是必需的.为了一致性; 我会一直提倡他们的使用`:-)` (3认同)