Powershell-在哪里包含多个JSON数组键

时间:2018-09-29 09:21:28

标签: arrays json powershell

我找不到任何解决方案,为什么我在Powershell中的where子句不能给我期望的输出。

我有一个JSON文件,如下所示:

[
  {
    "name": "server1",
    "tag": ["App", "Mid-Tier", "User"],
    "domain": "domainname",
    "alias": ["Alias3"],
    "ip": null,
    "tcp": 8080,
    "active": false,
    "apps": ["ARS", "Tomcat"],
    "patch": null,
    "hotfix": null
  },
  {
    "name": "server2",
    "tag": ["Reporting"],
    "domain": "domainname",
    "alias": ["Alias3"],
    "ip": null,
    "tcp": 8080,
    "active": false,
    "apps": ["ARS", "Tomcat"],
    "patch": null,
    "hotfix": null
  },
  {
    "name": "server3",
    "tag": ["DB"],
    "domain": "domainname",
    "alias": ["Alias1", "Alias2", "Alias3", "Alias4"],
    "ip": null,
    "tcp": 8080,
    "active": true,
    "apps": ["SQL"],
    "patch": null,
    "hotfix": null
  },
      {
    "name": "server5",
    "tag": ["Reporting"],
    "domain": "domainname",
    "alias": ["Alias1"],
    "ip": null,
    "tcp": 8080,
    "active": false,
    "apps": ["ARS", "Tomcat"],
    "patch": null,
    "hotfix": null
  }
]

编辑/更新
好的,我必须为您提供更多详细信息,我将尝试完成/理解的事情:

我有3个文件:
-具有上述数据的JSON文件
-存储所有函数和变量的脚本
-主脚本

这是我的函数的样子:

function opl_GetHostList {
[CmdletBinding()]
param (
    [parameter (
        mandatory=$false,
        HelpMessage = "Enter alias name.")]
        [string[]]
        $Aliasname,
    [parameter (
        mandatory=$false,
        HelpMessage = "Enter tag name.")]
        [string[]]
        $Tagname,
    [parameter (
        mandatory=$false,
        HelpMessage = "Enter app name.")]
        [string[]]
        $Appname,
    [parameter (
        mandatory=$false,
        HelpMessage = "Enter Active State: Either false or true. Default = true")]
        [bool]
        $Active = $true,
        [parameter (
        mandatory=$false,
        HelpMessage = "Choose selection Object.")]
        [string[]]
        $Select
)


$WhereArray = @()

# evaluate these with contains comparison
if ($Aliasname) {$WhereArray += '$Aliasname -contains $_.alias'}
#if ($Tagname) {$WhereArray += '$Tagname -contains $_.tag'}
#if ($Appname) {$WhereArray += '$Appname -contains $_.apps'}

# Build the where array into a string by joining each statement with -and
$WhereString = $WhereArray -Join " -and "
# Create the scriptblock with your final string            
$WhereBlock = [scriptblock]::Create($WhereString)

$opl_Hostlist | Where-Object {($WhereBlock.invoke()) -and $Active -eq $_.active} | Select-Object $Select

}

现在是我不了解的部分。
如果我在main.ps1中使用以下命令:

$opl_Hostlist | Where-Object {$_.alias -contains "alias3"} | Select-Object name

输出:

name
----
server1
server2
server3

但是,如果我将其与我的功能一起使用:

opl_GetHostList -Aliasname alias3 -Select name

我得到这个输出:

name
----
server1
server2

那么,当我用函数调用数据时,为什么第二个方法没有得到“ server3”?

非常感谢您的任何帮助。

1 个答案:

答案 0 :(得分:0)

根据您发布的JSON,您将收到ConvertFrom-Json : Invalid array passed in, extra trailing ','. (1018)错误。这是因为最后一项后面的逗号引起了。

我建议使用-in而不是-match操作,因为Alias属性是list。在比较中,左操作数是一个元素,右操作数是一个列表,因此您必须使用-in,否则使用-contains

if ($Aliasname) {
    $Aliasname | ForEach-Object{ $WhereArray += "$_ -in `$_.alias"}
}
$WhereArray -Join '-or'