在 Powershell v2 中使用正则表达式从 json 文件中获取值

Use regex in Powershell v2 to get values from a json file

如何使用 Powershell 中的正则表达式函数访问以下值,并将每个值分配给一个单独的变量?:

id(即获取值:TOKEN_ID)——在token

id(即获取值:TENANT_ID)——在tokentenant

adminURL(即获取值:http://10.100.0.222:35357/v2.0)- serviceCatalog,下的第一个值端点

由于我使用的是 Powershell v2,因此无法使用 ConvertFrom-Json cmdlet。到目前为止,我已尝试使用第三方 PS 脚本将文档转换为 xml 文件,但并不总是正确。我想使用正则表达式,但我不太习惯。

$json =
    "{
        "access": {
            "metadata": {
                "is_admin": 0,
                "roles": [
                    "9fe2ff9ee4384b1894a90878d3e92bab"
                ]
            },
            "serviceCatalog": [
                {
                    "endpoints": [
                        {
                            "adminURL": "http://10.100.0.222:8774/v2/TENANT_ID",
                            "id": "0eb78b6d3f644438aea327d9c57b7b5a",
                            "internalURL": "http://10.100.0.222:8774/v2/TENANT_ID",
                            "publicURL": "http://8.21.28.222:8774/v2/TENANT_ID",
                            "region": "RegionOne"
                        }
                    ],
                    "endpoints_links": [],
                    "name": "nova",
                    "type": "compute"
                },
                {
                    "endpoints": [
                        {
                            "adminURL": "http://10.100.0.222:9696/",
                            "id": "3f4b6015a2f9481481ca03dace8acf32",
                            "internalURL": "http://10.100.0.222:9696/",
                            "publicURL": "http://8.21.28.222:9696/",
                            "region": "RegionOne"
                        }
                    ],
                    "endpoints_links": [],
                    "name": "neutron",
                    "type": "network"
                },
                {
                    "endpoints": [
                        {
                            "adminURL": "http://10.100.0.222:8776/v2/TENANT_ID",
                            "id": "16f6416588f64946bdcdf4a431a8f252",
                            "internalURL": "http://10.100.0.222:8776/v2/TENANT_ID",
                            "publicURL": "http://8.21.28.222:8776/v2/TENANT_ID",
                            "region": "RegionOne"
                        }
                    ],
                    "endpoints_links": [],
                    "name": "cinder_v2",
                    "type": "volumev2"
                },
                {
                    "endpoints": [
                        {
                            "adminURL": "http://10.100.0.222:8779/v1.0/TENANT_ID",
                            "id": "be48765ae31e425cb06036b1ebab694a",
                            "internalURL": "http://10.100.0.222:8779/v1.0/TENANT_ID",
                            "publicURL": "http://8.21.28.222:8779/v1.0/TENANT_ID",
                            "region": "RegionOne"
                        }
                    ],
                    "endpoints_links": [],
                    "name": "trove",
                    "type": "database"
                },
                {
                    "endpoints": [
                        {
                            "adminURL": "http://10.100.0.222:9292",
                            "id": "1adfcb5414304f3596fb81edb2dfb514",
                            "internalURL": "http://10.100.0.222:9292",
                            "publicURL": "http://8.21.28.222:9292",
                            "region": "RegionOne"
                        }
                    ],
                    "endpoints_links": [],
                    "name": "glance",
                    "type": "image"
                },
                {
                    "endpoints": [
                        {
                            "adminURL": "http://10.100.0.222:8777",
                            "id": "350f3b91d73f4b3ab8a061c94ac31fbb",
                            "internalURL": "http://10.100.0.222:8777",
                            "publicURL": "http://8.21.28.222:8777",
                            "region": "RegionOne"
                        }
                    ],
                    "endpoints_links": [],
                    "name": "ceilometer",
                    "type": "metering"
                },
                {
                    "endpoints": [
                        {
                            "adminURL": "http://10.100.0.222:8000/v1/",
                            "id": "2198b0d32a604e75a5cc1e13276a813d",
                            "internalURL": "http://10.100.0.222:8000/v1/",
                            "publicURL": "http://8.21.28.222:8000/v1/",
                            "region": "RegionOne"
                        }
                    ],
                    "endpoints_links": [],
                    "name": "heat-cfn",
                    "type": "cloudformation"
                },
                {
                    "endpoints": [
                        {
                            "adminURL": "http://10.100.0.222:8776/v1/TENANT_ID",
                            "id": "7c193c4683d849ca8e8db493722a4d8c",
                            "internalURL": "http://10.100.0.222:8776/v1/TENANT_ID",
                            "publicURL": "http://8.21.28.222:8776/v1/TENANT_ID",
                            "region": "RegionOne"
                        }
                    ],
                    "endpoints_links": [],
                    "name": "cinder",
                    "type": "volume"
                },
                {
                    "endpoints": [
                        {
                            "adminURL": "http://10.100.0.222:8773/services/Admin",
                            "id": "11fac8254be74d7d906110f0069e5748",
                            "internalURL": "http://10.100.0.222:8773/services/Cloud",
                            "publicURL": "http://8.21.28.222:8773/services/Cloud",
                            "region": "RegionOne"
                        }
                    ],
                    "endpoints_links": [],
                    "name": "nova_ec2",
                    "type": "ec2"
                },
                {
                    "endpoints": [
                        {
                            "adminURL": "http://10.100.0.222:8004/v1/TENANT_ID",
                            "id": "38fa4f9afce34d4ca0f5e0f90fd758dd",
                            "internalURL": "http://10.100.0.222:8004/v1/TENANT_ID",
                            "publicURL": "http://8.21.28.222:8004/v1/TENANT_ID",
                            "region": "RegionOne"
                        }
                    ],
                    "endpoints_links": [],
                    "name": "heat",
                    "type": "orchestration"
                },
                {
                    "endpoints": [
                        {
                            "adminURL": "http://10.100.0.222:35357/v2.0",
                            "id": "256cdf78ecb04051bf0f57ec11070222",
                            "internalURL": "http://10.100.0.222:5000/v2.0",
                            "publicURL": "http://8.21.28.222:5000/v2.0",
                            "region": "RegionOne"
                        }
                    ],
                    "endpoints_links": [],
                    "name": "keystone",
                    "type": "identity"
                }
            ],
            "token": {
                "audit_ids": [
                    "gsjrNoqFSQeuLUo0QeJprQ"
                ],
                "expires": "2014-12-15T15:09:29Z",
                "id": "TOKEN_ID",
                "issued_at": "2014-12-15T14:09:29.794527",
                "tenant": {
                    "description": "Auto created account",
                    "enabled": true,
                    "id": "TENANT_ID",
                    "name": "USERNAME"
                }
            },
            "user": {
                "id": "USER_ID",
                "name": "USERNAME",
                "roles": [
                    {
                        "name": "_member_"
                    }
                ],
                "roles_links": [],
                "username": "USERNAME"
            }
        }
    }"

如果您在装有 PowerShell 2.0 的计算机上使用 .NET 3.5 或更高版本,you can use a JSON serializer(来自链接的答案):

[System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$json = "{a:1,b:2,c:{nested:true}}"
$ser = New-Object System.Web.Script.Serialization.JavaScriptSerializer
$obj = $ser.DeserializeObject($json)

这比使用正则表达式更好。

以管理员URL为例,您可以参考:

$obj.access.serviceCatalog[0].endpoints[0].adminURL

无论如何都使用正则表达式

if ($json -match '(?s)"serviceCatalog".+?"endpoints".+?"adminURL"[^"]+"(?<adminUrl>[^"]+)".+?"token".+?"id"[^"]+"(?<tokenID>[^"]+)".+?"tenant".+?"id"[^"]+"(?<tenantID>[^"]+)') {
    $Matches['adminURL']
    $Matches['tokenID']
    $Matches['tenantID']
}

正则表达式细分:

  • (?s) 告诉正则表达式引擎 . 匹配任何内容,包括换行符(默认情况下它不会)。
  • 当然,所有 "whatever" 部分都完全匹配。
  • .+? 匹配 1 个或多个任意字符(包括换行符,因为我们使用 s),? 使它成为 non-greedy.
  • [^"]+ 这匹配 1 个或多个不是双引号的字符。
  • () 是一个捕获组。通过使用 (?<name>) 我们可以稍后通过名称而不是数字来引用该组,这很漂亮。

所以基本的想法是寻找文字,然后到达我们可以捕获所需值的地步。在 PowerShell 中进行 -regex 运算符匹配后,$Matches 变量会填充匹配项、组等

请注意,这取决于值在发布 JSON 中的顺序。如果它们的顺序不同,它将失败。

要解决这个问题,您可以将其分成 3 个不同的正则表达式匹配项。