从PowerShell PS自定义对象读取Json文件

时间:2019-07-06 17:09:53

标签: json powershell

我有一个文件,可以通过JsonConvert转换为Json

我能够读取和迭代文件。但是在读书的时候我被困在其中一个地方。我只想读取时来自json文件的值,例如我只想要“ cd_hello1”

“ ClientId”:{“ variableName”:“ cd_hello1”}

下面是我的json文件

{
"AllowedHosts": "*",
"AllExpirationInMinutes": 10,
"AzureAd": {
    "Authority": "https://sampleURL",
    "CallbackPath": "/signin-oidc",
    "ClientId": {
        "variableName": "cd_hello1"
    },
    "ClientSecret": {
        "variableName": "cd_hello2"
    },
    "Domain": "hello.onmicrosoft.com",
    "Instance": "https://login.microsoftonline.com",
    "TenantId": "242-243-243243-234"
},
"AzureKeyVault": {
    "VaultName": {
        "variableName": "cd_hello2"
    },
    "ClientCertName": {
        "variableName": "cd_hello3"
    },
    "ServerCertThumbprint": {
        "variableName": "cd_hello4"
    }
},
"EC_APIM_Vault_Key": {
    "variableName": "cd_hello4"
},
"HttpCacheExpireInMinutes": 120,
"InstrumentationEngine_EXTENSION_VERSION": "disabled",
"Logging": {
    "LogLevel": {
        "Default": "Warning"
    }
}

}

 $newAppSettings = @{}





  #Reading all values from Host.settings.Json File
    $JsonDataObject =  (Get-Content -Path $HostSettingFilePath) -replace '^\s*//.*' | Out-String  | ConvertFrom-Json


    #Adding new values
    foreach ($property in $JsonDataObject.PSObject.Properties) 
      { 


        foreach($value in $property.Value)
        {


            if($value.GetType() -eq [System.Management.Automation.PSCustomObject])
            {

                foreach($subvalue in $value.PSObject.Properties)
                {


                        if($subvalue -like '*variableName*')
                        {

                             $x = $subvalue.Value

                             $newAppSettings[$property.Name] = (get-item env:$x).Value # Example : $cd_XYZ

                             $subvalue | Get-Member -MemberType NoteProperty | Select -ExpandProperty Value


                        }
                        else
                        {

                            $newAppSettings[$property.Name] = [string]$subvalue.Value
                        }

                }
            }
            else
            {

                $newAppSettings[$property.Name] = [string]$property.Value
            }
        }


    }          

0 个答案:

没有答案