-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-patch.psm1
49 lines (32 loc) · 1.1 KB
/
json-patch.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Function JsonPatch{
[CmdletBinding(SupportsShouldProcess = $true)]
Param
(
[Parameter(Mandatory = $true)]
[Alias('f')][String]$FilePath,
[Parameter(Mandatory = $true)]
[Alias('p')][String]$PropertyName,
[Parameter(Mandatory = $true)]
[Alias('v')][String]$Value,
[Parameter(Mandatory = $false)]
[Alias('l')][bool]$Log
)
If ($PSCmdlet.ShouldProcess("Patching Json file...")) {
Write-Host "Patching Start."
# $p = $PWD.Path;
# $ScriptDir = Split-Path -parent $MyInvocation.MyCommand.Path
# $targetFilePath = $(Join-Path -Path $ScriptDir -ChildPath $FileName);
$json = Get-Content -Path $FilePath | Out-String
$object = ConvertFrom-Json $json
if ($Log -eq $true) {
Write-Host $json
}
$object.$PropertyName = $Value;
$json = ConvertTo-Json $object -Compress
if ($Log -eq $true) {
Write-Host $json
}
Set-Content -Path $FilePath -Value $json
Write-Host "Patching Done."
}
}