简体   繁体   English

如何使用powershell更改长JSON值中的url?

[英]How to change url in long JSON value using powershell?

I have a JSON file :我有一个 JSON 文件:

"scripts":  {
                "lint":  "./node_modules/.bin/eslint ./ --config .eslintrc.json",
                "test:ui:start-local-x-run":  "./node_modules/.bin/protractor ./configs/protractor/local_run.conf.js --disableChecks --feurl http://website.com --beurl http://172.34.34.34:3000 --feadminurl http://website.com/ap/users --db name --env STG"

            }

I need to change --feurl --beurl --feadminurl --db --env using powershell.我需要使用 powershell 更改 --feurl --beurl --feadminurl --db --env 。

I've tried to use the function but it changes full value:我尝试使用该函数,但它更改了完整值:

 function Update-JsonParameter($directory, $jsonFile, $property, $subproperty, $value)
{
try
{
write-host "Update $property.$subproperty property in JSON file $directory\$jsonFile"
$jsonFile = "$directory\$jsonFile"
$convertJson = Get-Content -Raw -Path $jsonFile | ConvertFrom-Json
$convertJson.$property.$subproperty = "$value"
$convertJson | ConvertTo-Json | set-content $jsonFile
}
catch
{
write-host "Updating JSON file FAILED!"
throw $Error[0].Exception
}
}

How can I implement it?我该如何实施?

Without a function, you could use a hash table to manage the arguments you want to change.如果没有函数,您可以使用哈希表来管理要更改的参数。 Then just use -replace for each of those arguments.然后只需对每个参数使用-replace You can iterate over a hash table with GetEnumerator() method.您可以使用GetEnumerator()方法遍历哈希表。

$json = @'
{
"scripts":  {
                "lint":  "./node_modules/.bin/eslint ./ --config .eslintrc.json",
                "test:ui:start-local-x-run":  "./node_modules/.bin/protractor ./configs/protractor/local_run.conf.js --disableChecks --feurl http://website.com --beurl http://172.34.34.34:3000 --feadminurl http://website.com/ap/users --db name --env STG"

            }
}
'@
$ArgsToChange = @{ feurl = 'https://newfeurl.com'; beurl = 'https://newbeurl.com'; feadminurl = 'https://newfeadminurl.com'; db = 'newdb'; env = 'newenv'}
$jo = $json | ConvertFrom-Json

foreach ($arg in $ArgsToChange.GetEnumerator()) {
    $jo.scripts.'test:ui:start-local-x-run' = $jo.scripts.'test:ui:start-local-x-run' -replace "(?<=--$($arg.Key) ).*?(?=\s--|$)",$arg.Value
}

$updatedJSON = $jo | ConvertTo-Json

If you always know all of the arguments to pass the target script, then you can still use a hash table to track the arguments, but you can instead build your argument string with its data.如果您始终知道传递目标脚本的所有参数,那么您仍然可以使用哈希表来跟踪参数,但您可以使用其数据构建参数字符串。

$json = @'
{
"scripts":  {
                "lint":  "./node_modules/.bin/eslint ./ --config .eslintrc.json",
                "test:ui:start-local-x-run":  "./node_modules/.bin/protractor ./configs/protractor/local_run.conf.js --disableChecks --feurl http://website.com --beurl http://172.34.34.34:3000 --feadminurl http://website.com/ap/users --db name --env STG"

            }
}
'@

# Hash table of arguments and values (syntax: --argument value)
$ArgsToChange = [ordered]@{ disableChecks = $null; feurl = 'https://newfeurl.com'; beurl = 'https://newbeurl.com'; feadminurl = 'https://newfeadminurl.com'; db = 'newdb'; env = 'newenv'}
$jo = $json | ConvertFrom-Json

# Iterating over the hash table creating string of arguments
# Expected output string of --argument1 value1 --argument2 value2 for example
$ArgString = ($ArgsToChange.GetEnumerator() |% { "--{0} {1}" -f $_.Key,$_.Value }).Trim() -join ' '

# Grab string before the first --argument
$program = ($jo.scripts.'test:ui:start-local-x-run' -split '--')[0]

# Join program string and new argument string
$jo.scripts.'test:ui:start-local-x-run' = $program + $ArgString

$updatedJSON = $jo | ConvertTo-Json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用PowerShell脚本读取odata url并将其另存为json文件 - How to read an odata url and save it as a json file using PowerShell script 如何使用 Websocket 客户端使用 PowerShell V2 打开到 URL 的长期连接? - How to use a Websocket client to open a long-lived connection to a URL, using PowerShell V2? 如何使用Jackson将Int-length的JSON值拆箱为Long? - How to unbox a JSON value of Int-length to Long using Jackson? 使用 PowerShell 如何在具有注释的 json 文件中替换和保存值 - Using PowerShell how to replace and save a value in a json file having comments 如何使用 PowerShell 从 json 响应中获取单个值 - How to get a single value from a json response using PowerShell 如何使用 PowerShell 更新 json 文件中的属性值? - How to update the value of a property in a json file using the PowerShell? 使用 powershell 将 JSON 请求发送到 URL - Post JSON reqeust to URL using powershell 如何使用算术更改 JSON 文件的值? - How to change the value of a JSON file using arithmetics? 如何使用 Powershell 从 URL 加载 JSON? - how to use Powershell to load JSON from a URL? 如何从Powershell中的JSON值获取值 - How to get value from json value in powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM