如何使用 PowerShell 更新 json 文件中 属性 的值?
How to update the value of a property in a json file using the PowerShell?
我被困在我的 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
我被困在我的 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