【问题标题】:How to Get the values from json using powershell如何使用powershell从json中获取值
【发布时间】:2020-08-10 02:53:07
【问题描述】:

伙计们,这是我的 JSON 文件,我想创建一个 PowerShell 脚本,它会给我类似的结果 我使用了 Get-Content 等方法,但 JSON 解析存在一些问题。请在下面找到我详细解释的我的要求。

MyLocalMachineHome
LocalMachine = Sahil_LocalMachine
Second_MyLocalMachine = Sahil_MylocalMachine
Second_MyLocalMachine = ""

Staging
Second_Staging = Sahil;_Secconf
Staging = Sahil_Staging
third_staging = stsajiii

如果我只想获取“暂存”变量,我还想拥有一项功能。

我在原始 JSON 文件中使用了这个函数 Get-Content -Raw -Path E:\shell\Powershell\1ReleasePipelines.json | ConvertFrom-Json | select -ExpandProperty 变量,但不知何故,我从这个方法中获得的存储字符串存在某种限制。

{
  "environments": [
    {
      "id": 3,
      "name": "MyLocalMachineHome",
      "variableGroups": [],
      "variables": {
        "LocalMachine": {
          "value": "Sahil_LocalMachine"
        },
        "Second_MyLocalMachine": {
          "value": "Sahil_MylocalMachine"
        },
        "thirf_mylocal": {
          "value": ""
        }
      }
    },
    {
      "id": 7,
      "name": "Staging",
      "variableGroups": [],
      "variables": {
        "Second_Staging": {
          "value": "Sahil;_Secconf"
        },
        "Staging": {
          "value": "Sahil_Staging"
        },
        "third_staging": {
          "value": "stsajiii"
        }
      }
    }
  ]
}

【问题讨论】:

  • json 解析存在一些问题什么问题?你试过什么?什么没用?您收到了哪些错误消息?
  • 问题实际上是我的 json 文件比我提供的文件大得多,当我使用上面写的 get-content 方法时,我试图将文件放入字符串变量,如 $a=get-content ... bla bla bla 当我检查值时在变量中它只显示 localmachine 的值而不是 staging 的值,所以在这种情况下我无法使用循环问题

标签: json powershell azure-devops powershell-2.0 azure-powershell


【解决方案1】:

如果我们假设 $json 包含您的 JSON 内容,您可以执行以下丑陋的代码:

$environment = 'staging'
$j = $json | ConvertFrom-Json
($j.environments | where name -eq $environment).variables | Foreach-Object {
    $CurrentObject = $_
    $CurrentObject | Get-Member -MemberType NoteProperty |
        Select-Object -Expand Name | Foreach-Object {
            $CurrentObject.$_.value
        }
}

看来您的问题是您不知道 JSON 中将包含哪些变量。所以你不能轻易使用Select-Object variable$object.variable。您需要一种动态的方法。


如果您提前了解变量,事情就会变得简单。您可以将变量名称存储在数组中并循环遍历它们。

$variables = 'Second_Staging','Staging','third_staging'
$environment = 'staging'
$j = $json | ConvertFrom-Json
$jsonVars = ($j.environments | where name -eq $environment).variables
$variables | Foreach-Object {
    $jsonVars.$_.value
}

【讨论】:

【解决方案2】:

使用格式列表而不是格式表查看变量的所有子属性。由于属性不同,格式表不会显示所有属性。 json中有很多草率的对象构造。

$a = get-content file.json


$a.environments.variables | format-table

LocalMachine                Second_MyLocalMachine         thirf_mylocal
------------                ---------------------         -------------
@{value=Sahil_LocalMachine} @{value=Sahil_MylocalMachine} @{value=}


$a.environments.variables | format-list

LocalMachine          : @{value=Sahil_LocalMachine}
Second_MyLocalMachine : @{value=Sahil_MylocalMachine}
thirf_mylocal         : @{value=}

Second_Staging : @{value=Sahil;_Secconf}
Staging        : @{value=Sahil_Staging}
third_staging  : @{value=stsajiii}

获取暂存变量?

$a.environments | where name -eq staging | foreach variables

Second_Staging          Staging                third_staging
--------------          -------                -------------
@{value=Sahil;_Secconf} @{value=Sahil_Staging} @{value=stsajiii}

【讨论】:

  • 我添加了什么shell来获取暂存变量而不是本地机器
  • 这个? $a.environments.variables.staging
  • 这个? $a.environments | where name -eq staging | select variables我完全不明白你的问题。
【解决方案3】:
cls

start-transcript -path 'C:\E\Devops\PowerShell_Chapters\ABC.txt'

write-output "**********Variables of Release************"

get-content -raw -path 'C:\E\Devops\PowerShell_Chapters\Release.json'| Convertfrom-Json | Select -ExpandProperty variables 

$json = get-content -raw -path 'C:\E\Devops\PowerShell_Chapters\Release.json'| Convertfrom-Json | Select -ExpandProperty environments

$EnvirnomentsVariables = get-content -raw -path 'C:\E\Devops\PowerShell_Chapters\Release.json'| Convertfrom-Json | Select -ExpandProperty environments |Select -ExpandProperty name 

$ReleaseVariable = get-content -raw -path 'C:\E\Devops\PowerShell_Chapters\Release.json'| Convertfrom-Json | Select -ExpandProperty environments |Select -ExpandProperty variables 

$i = 0 

foreach($a in $EnvirnomentsVariables)
{

    $ABC_Staging = $EnvirnomentsVariables[$i]
     #write-output $ABC_Staging
    if( $ABC_Staging -match "ABC Staging")
    {
        write-output "****************Variables of " $EnvirnomentsVariables[$i]*************"
        #add-content 'C:\E\Devops\PowerShell_Chapters\ABC.txt' $EnvirnomentsVariables[$i] 
        # Set-content  -path 'C:\E\Devops\PowerShell_Chapters\Sahil.json'| ConvertTo-Json  | select $EnvirnomentsVariables[$i] 
        write-output $ReleaseVariable[$i]
        #   add-content 'C:\E\Devops\PowerShell_Chapters\ABC.txt' $ReleaseVariable[$i] 
        # Set-content  -path 'C:\E\Devops\PowerShell_Chapters\Sahil.json'| ConvertTo-Json | select  $ReleaseVariable[$i]
    }
    $i = $i + 1   
}
stop-transcript 

【讨论】:

  • ----我得到的OutPut-- **********发布的变量************ 变量绑定:@{value=false } AbcPort : @{value=100} ****************ABC Staging 的变量**************** Connection : @{value =*****} 环境:@{value=ABC Staging} AbcPort:@{value=100 ********************** Windows PowerShell 脚本结束结束时间:20200427130839 仍然要更改最终输出,但这对我来说有点用
  • 感谢您在这里分享您的解决方案,请您接受您的解决方案作为答案吗?因此,对于遇到相同问题的其他成员轻松找到解决方案将有所帮助。
猜你喜欢
  • 2021-12-20
  • 2020-10-17
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 2021-03-04
  • 2012-08-26
  • 1970-01-01
相关资源
最近更新 更多