簡體   English   中英

如何使用 PowerShell 更新 json 文件中的屬性值?

[英]How to update the value of a property in a json file using the PowerShell?

我被困在我的 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

您可以先獲取 JSON object,然后只需將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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM