【问题标题】:How to update the value of a property in a json file using the PowerShell?如何使用 PowerShell 更新 json 文件中的属性值?
【发布时间】:2020-10-16 02:17:51
【问题描述】:

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


    【解决方案1】:

    您可以先获取 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
    

    【讨论】:

      猜你喜欢
      • 2015-02-19
      • 1970-01-01
      • 2021-09-03
      • 2016-06-22
      • 2020-09-08
      • 2010-10-02
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      相关资源
      最近更新 更多