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

时间:2015-07-09 19:08:21

标签: regex json powershell powershell-v2.0

如何使用Powershell中的regex函数访问以下值,并将每个值分配给单个变量?:

id (即获取值:TOKEN_ID) - 在令牌

ID (即获取值:TENANT_ID) - 在令牌下,租户

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

1 个答案:

答案 0 :(得分:3)

如果您在使用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)

这比使用正则表达式更可取。

例如,对于管理员网址,您可以参考:

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

无论如何使用RegEx

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

RegEx细分:

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

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

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

要解决此问题,您可以将其拆分为3种不同的正则表达式匹配。