首页 > 解决方案 > 如何使用 PowerShell 更新 json 文件中的属性值?

问题描述

我被困在我的 PowerShell 脚本中的一个点,我需要将名为 restServiceURL 和 microServiceURL 的属性的部分值从 http 更新为 https。(下面的截图)

json文件

我有以下脚本,但不知何故我无法弄清楚需要添加什么才能将属性的特定部分值(在本例中为 http)从“ http: //VWMAIMPKG16SN/IMatchREST/”替换为“ https: //VWMAIMPKG16SN/IMatchREST/"

我知道 set-content 命令应该能够做到这一点,但是如何在不改变值的另一部分的情况下做到这一点是我所困的地方。

对此的任何建议都会有所帮助。

# Code to get Installation Directory path
$CommonNode=Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\AquagardePI\STeP\Platform\Common
$InstallationDir=$CommonNode.InstallationDir

#Path of Json File
$ConfigPath = $InstallationDir + "Web Client\www\\NextGen\assets\config.json"

#Get Content of the File
$file = Get-Content $ConfigPath -raw | ConvertFrom-Json

#Get the value of Property
$file = $file.restServiceURL

标签: jsonpowershellpowershell-4.0powershell-5.0

解决方案


您可以先获取 JSON 对象,然后简单地替换httphttps您感兴趣的两个属性:

$ConfigPath = $InstallationDir + "Web Client\www\\NextGen\assets\config.json"
$file = Get-Content $ConfigPath -raw | ConvertFrom-Json

$file.microServiceURL = $file.microServiceURL.Replace('http','https')
$file.restServiceURL = $file.restServiceURL.Replace('http','https')

Set-Content -Value ($file | ConvertTo-Json) -Path $ConfigPath

推荐阅读