gpt4 book ai didi

json - 使用 PowerShell 更新 JSON 文件

转载 作者:行者123 更新时间:2023-12-03 23:31:51 24 4
gpt4 key购买 nike

我目前正在尝试使用 VSTS 设置一个持续集成系统,但遇到了一些障碍。作为发布过程的一部分,我需要根据环境更新 JSON 文件中的特定对象值。在 VSTS 环境中,我似乎可以使用的唯一工具是 PowerShell。

我已经做了很多研究,但一直无法弄清楚这究竟是如何做到的。我在 Stack Overflow“How do I update JSON file using PowerShell”上找到了这个问题和答案,但是执行答案中提供的脚本会显着改变 JSON 文件的结构,并添加了很多看起来像 PowerShell 元数据的内容。

理想情况下,我想采用一个现有的 JSON 文件,该文件被部署并更新下面示例 JSON 中 connectionString 属性的值。

{
"policies": {
"Framework.DataContext": {
"connectionString": "Server=ServerName;Database=DateBaseName;Integrated Security=sspi;"
}
}
}

有没有人对如何实现这一点有任何建议?到目前为止,我已尝试运行以下脚本,但它抛出“在此对象上找不到属性 'connectionString'。验证该属性是否存在并且可以设置。”异常(exception)。我已经验证对象遍历是正确的并且 connectionString 属性存在。
$pathToJson = "D:\Path\To\JSON\file.json"
$a = Get-Content $pathToJson | ConvertFrom-Json
$a.policies.'Framework.DataContext'.connectionString = "Server=ServerName;Database=DateBaseName;Integrated Security=sspi;"
$a | ConvertTo-Json | set-content $pathToJson

file.json 的全部内容如下
{
"log": {
"level": 0,
"file": "c:\\temp\\simport.log",
"formats": {
"error": null,
"start": null,
"requestBegin": null,
"requestWork": "",
"requestError": null,
"requestEnd": null,
"stop": null
},
"eventLog": {
"name": "Application"
}
},

"diagnostic": {
"stackTrace": false
},

"api": {
"simport": true
},

"roles": {
"0": "Anonymous",
"1": "Administrator",
"2": "Participant",
"3": "Facilitator"
},

"pathType": {
"area": 1,
"region": 2,
"session": 3,
"team": 4
},

"scenarios": {
"default": {
"default": true,
"initState": "Init",
"rounds": [
{
"name": "round1",
"displayName": "R1",
"beginTime": 1,
"endTime": 3
},
{
"name": "round2",
"displayName": "R2",
"beginTime": 4,
"endTime": 6
},
{
"name": "round3",
"displayName": "R3",
"beginTime": 7,
"endTime": 9
},
{
"name": "round4",
"displayName": "R4",
"beginTime": 10,
"endTime": 12
}
]
}
},

"simportQueries": {
"package": "bin/trc.simport3.zip"
},

"customQueries": {
"package": "app/config/custom-queries.zip",
"parameters": {
}
},

"audit": {
"Path.Create": true,
"Path.Delete": true,
"Team.Create": true,
"Team.Update": true,
"Team.Delete": true,
"SimportData.SaveValues": true
},

"tasks": {
"task1": {
"state": "",
"required": "",
"completed": "C:Task1Status:+0"
}
},

"feedback": {
"welcome": {
"text": {
"": "en-us",
"en-us": "Welcome"
}
}
},

"contentCategories": {
"demo1": {
"round": 1
}
},

"policies": {
"Simport.Web.Module": {
"fileMask": ".aspx,.asmx",
"deny": {
"statusCode": 404,
"statusDescription": "Not found",
"location": [
"/{0,1}app/config/(.*\\.json)$",
"/{0,1}app/config/(.*\\.xml)$",
"/{0,1}app/config/(.*\\.zip)$",
"/{0,1}app/config/(.*\\.xlsx)$"
]
},
"formDataContentType": [ "application/x-www-form-urlencoded" ]
},

"Framework.DataContext": {
"connectionString": "Server=(local);Database=Simport3;Integrated Security=sspi;",
"commandTimeout": 30
},

"Simport.Security": {
"passwordEncryption": "",
"passwordSalt": "",
"passwordPolicy": {
"disabled": true,
"min": 8,
"max": 100,
"rules": [
{ "id": "digit", "pattern": "\\d+", "flags": "i" },
{ "id": "letter", "pattern": "\\w+", "flags": "i" },
{ "id": "upper", "pattern": "[A-Z]+" },
{ "id": "lower", "pattern": "[a-z]+" },
{ "id": "special", "pattern": "[\\!#@\\$_~]+", "flags": "i" },
{ "id": "prohibited", "pattern": "[\\\\/'\"\\?\\^&\\+\\-\\*\\%\\:;,\\.]+", "flags": "gi", "match": false }
]
}
},

"Simport.PackageDefinition": {
"path": "~/app/config/manifest.xml"
},

"Security.SignIn": {
"result": {
"default": "u,p,p.props,t"
},
"claims": [
[ "userId", "firstName", "lastName" ]
]
},

"Security.GetContext": {
"result": {
"default": "u,p,p.props,pr,t"
}
},

"Security.ChangePassword": {
"allowedRoles": [ 1, 2, 3 ]
},

"Security.ResetPassword": {
"allowedRoles": [ 1, 2 ]
},

"Security.Register": {
"allowedRoles": [ 0 ],
"!pathType-0": 4,
"!roleId-0": 2
},

"Path.Create": {
"allowedRoles": [ 1, 2, 3 ]
},

"Path.Select": {
"allowedRoles": [ 1, 2, 3 ],
"result-1": {
}
},

"Path.Delete": {
"allowedRoles": [ 1, 2 ]
},

"User.Select": {
"allowedRoles": [ 1, 2, 3 ],
"result": {
"select": [ "id", "pathid", "roleid", "name", "email", "login", "props" ],
"restrict": [ "password" ]
},
"result-1": {
"select": "*",
"group": true
}
},

"User.Create": {
"allowedRoles": [ 1, 2 ],
"result": {
"select": [ "id", "pathid", "roleid", "name", "email", "login", "props" ],
"restrict": [ "password" ]
},
"result-1": {
"select": "*",
"group": true
}
},

"User.Update": {
"allowedRoles": [ 1, 2, 3 ],
"result": {
"select": [ "id", "pathid", "roleid", "name", "email", "login", "props" ],
"restrict": [ "password" ]
}
},

"User.Delete": {
"allowedRoles": [ 1, 2 ],
"result": {
"restrict": [ "password" ]
}
},

"Session.Select": {
"allowedRoles": [ 1, 2, 3 ],
"enforcePathLevel": true,
"result": {
"default": [ "name", "beginDate", "endDate" ],
"restrict": [ "password" ]
},
"result-1": {
"default": [ "name", "beginDate", "endDate" ],
"treeAllowed": true,
"treeDefault": false
}
},

"Session.Create": {
"allowedRoles": [ 1, 2 ],
"enforcePathLevel": true
},

"Session.Update": {
"allowedRoles": [ 1, 2 ],
"enforcePathLevel": true,
"update-restictions": [ "password" ],
"update-restictions-1": [ ],
"result": {
"restrict": [ "password" ]
}
},

"Session.Delete": {
"allowedRoles": [ 1, 2 ],
"result": {
"restrict": [ "password" ]
}
},

"Team.Select": {
"allowedRoles": [ 1, 2, 3 ],
"enforcePathLevel": false,
"enforcePathLevel-1": true,
"result-1": {
"treeAllowed": true,
"treeDefault": false
}
},

"Team.Create": {
"allowedRoles": [ 1, 2, 3 ],
"enforcePathLevel": true,
"enforcePathLevel-1": false,
"allowMultiple": false,
"allowMultiple-1": true,
"result": {
},
"overrides": {
"roleID": 3
}
},

"Team.Reset": {
"allowedRoles": [ 1, 2, 3 ]
},

"Team.Delete": {
"allowedRoles": [ 1, 2, 3 ],
"deleteMultiple": true,
"result": {
"default": "t"
}
},

"Team.TransitionTo": {
"allowedRoles": [ 1, 2, 3 ],
"inboundRules": {
"Round1Init": {
"allowedRoles": [ ]
}
},
"outboundRules": {
"Round1Wait": {
"allowedRoles": [ 1, 2, 3 ]
}
}
},

"Team.TakeControl": {
"allowedRoles": [ 1, 2, 3, 4 ],
"select-1": {
"select": "*",
"restrict": [ "ctrl.userID", "ctrl.loginName" ]
}
},

"Team.ReleaseControl": {
"allowedRoles": [ 1, 2, 3, 4 ],
"select-1": {
"select": "*",
"restrict": [ "ctrl.userID", "ctrl.loginName" ]
}
},

"Team.GetStatus": {
"allowedRoles": [ 1, 2, 3 ],
"result": {
"default": "p,t,pr"
}
},

"Data.Select": {
"allowedRoles": [ 1, 2, 3 ]
},

"Data.Update": {
"allowedRoles": [ 1, 2, 3 ],
"audit": {
"g": false,
"i": true,
"o": true,
"s": true
}
},

"Data.ExecuteQuery": {
"allowedRoles": [ 0, 1, 2, 3 ],
"allowed-0": [ "login4\\areas", "login4\\regions", "login4\\sessions", "login4\\teams" ],
"restrict-3": [ "prohibitedQueryNameHere" ]
},

"Document.Select": {
"defaultTextEncoding": "utf-16"
},

"Document.Create": {
"~allowFileExt": [ ],
"denyFileExt": [ ".exe", ".com", ".cmd", ".bat", ".ps1" ],
"~allowContentType": [ ],
"denyContentType": [ "application/x-msdownload" ],
"maxContentLength": 0,
"defaultTextEncoding": "utf-16"
},

"Document.Update": {
"allowedRoles": [ 1, 2, 3 ]
},

"Document.Delete": {
},

"Document.Download": {
}
}
}

最佳答案

您的 json缺少开始和结束的大括号:

{
"policies": {
"Framework.DataContext": {
"connectionString": "Server=ServerName;Database=DateBaseName;Integrated Security=sspi;"
}
}
}

现在您可以像这样更新文件:
$pathToJson = "F:\Path\To\JSON\file.json"
$a = Get-Content $pathToJson | ConvertFrom-Json
$a.policies.'Framework.DataContext'.connectionString = "Server=ServerName;Database=DateBaseName;Integrated Security=sspi2;"
$a | ConvertTo-Json | set-content $pathToJson

你也可以使用一些 Select-Object获取属性:
$connectionString = $a | select -expand policies | select -expand Framework.DataContext 
$connectionString.connectionString = 'Test'

关于json - 使用 PowerShell 更新 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37551366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com