我試圖用單個值更新JSON文件。該文件已更新,但正在更新一些額外的數據,我不想或不需要。使用PowerShell更新JSON文件
下面是來自PowerShell腳本的一個片段:
Id.txt包含一個字符串 「16963e76」
$outPath = "C:\Program Files (x86)\Go Agent\pipelines\mypipeline\Application\Application\Id.txt"
$imageid = Get-Content $outPath
$filejson = Get-Content -Raw -Path $file.FullName -Encoding UTF8 | ConvertFrom-Json
$filejson.parameters.Image = $imageid
$filejson | ConvertTo-Json | Out-File $filepath -Encoding utf8 -Force
這是我希望我的JSON的樣子:
{
"name": "perf-a",
"cft_file":"cft/cft.json",
"parameters": {
"AppEnvironmentType": "perf",
"Image": "16963e76"
},
"tags": {
"Owner": "[email protected]",
"CostCenter": "12345"
}
}
以下是更新後我的JSON的實際外觀:
{
"name": "perf-a",
"cft_file": "cft/cft.json",
"parameters": {
"AppEnvironmentType": "perf",
"Image": {
"value": "16963e76",
"PSPath": "C:\\Program Files (x86)\\Go Agent\\pipelines\\mypipeline\\Application\\Application\\Id.txt",
"PSParentPath": "C:\\Program Files (x86)\\Go Agent\\pipelines\\mypipeline\\Application\\Application",
"PSChildName": "Id.txt",
"PSDrive": "C",
"PSProvider": "Microsoft.PowerShell.Core\\FileSystem",
"ReadCount": 1
}
},
"tags": {
"Owner": "[email protected]",
"CostCenter": "12345"
}
}
是什麼導致了這個問題,我該如何阻止它發生?
感謝,
朗達
行,所以當您導入從id.txt PowerShell中的字符串是從環境中添加幾個noteproperties,通常這些並不重要,可以忽略。然而ConvertTo-JSON從對象中提取這些音符屬性並將它們插入到你的json中。 通過使用tostring方法,我的解決方案通過改變什麼是一個字符串到另一個字符串,但這次沒有註釋屬性,所以它的工作原理。 –