如何使用 powershell 从 json 获取值

How to Get the values from json using powershell

伙计们,这是我的 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

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

我在我原来的 JSON 文件上使用了这个函数 Get-Content -Raw -Path E:\shell\PowershellReleasePipelines.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 内容,您可以执行以下难看的代码:

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

使用format-list而不是format-table查看变量的所有子属性。由于属性不同,format-table 不会显示所有属性。 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}
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