json - 如何使用 PowerShell 更新 json 文件中的属性值?
问题描述
我被困在我的 PowerShell 脚本中的一个点,我需要将名为 restServiceURL 和 microServiceURL 的属性的部分值从 http 更新为 https。(下面的截图)
我有以下脚本,但不知何故我无法弄清楚需要添加什么才能将属性的特定部分值(在本例中为 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
解决方案
您可以先获取 JSON 对象,然后简单地替换http
为https
您感兴趣的两个属性:
$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
推荐阅读
- html - 如何在更改屏幕宽度的同时保持响应式背景图像高度?
- php - 标记日历 php mysql
- c# - 有没有办法在通过服务器端处理呈现的 Jquery 数据表中指定用户权限?
- ruby - 如何在 ruby 中添加或获取 {github.sha} 到黄瓜 url
- javascript - 请帮我用通用代码替换特定的部分域
- regex - 熊猫精确str匹配功能?
- django - Django Channels ValueError:找不到路径的路由
- linux-kernel - 如何使 Ubuntu 服务器仅通过 Wi-Fi 启动(ubuntu-20.04.2-server)?
- html - 如何保持恒定的图像高度?卡在弹性盒子里?
- php - 缺少更新:媒体 Drupal 9