Skip to content

Instantly share code, notes, and snippets.

@turowicz
Last active March 17, 2016 16:34
Show Gist options
  • Save turowicz/579c77c020c860ebb299 to your computer and use it in GitHub Desktop.
Save turowicz/579c77c020c860ebb299 to your computer and use it in GitHub Desktop.
Replace JSON variables based on another json
Param(
[Parameter(Mandatory=$True)]
[string]$configFile,
[Parameter(Mandatory=$True)]
[string]$path
)
# read configuration file
$config = ""
foreach($file in Get-ChildItem $path -filter $configFile)
{
$config = Get-Content $file.FullName | ConvertFrom-Json
}
if($config -ne "")
{
Write-Host
Write-Host 'Configuration:'
Write-Host $config
Write-Host
foreach ($key in $config.psobject.properties.name)
{
# for each entity
if($key -ne "`$schema")
{
Write-Host 'Configuring: '$key
foreach($file in Get-ChildItem $path -filter "*.json")
{
$target = Get-Content $file.FullName | ConvertFrom-Json
# find matching definition
if($target.name -eq $key)
{
Write-Host
Write-Host "Transforming file " $file.Name
# replace values
foreach($property in $config.$key)
{
Write-Host "Setting: " $property.name
Write-Host "Value: " $property.value
$expression = $property.name -replace "[`$]", "`$target"
Invoke-Expression ($expression + " = `"" + $property.value + "`"")
}
# write result to disk
$target | ConvertTo-Json -depth 999 | Out-File $file.FullName
Write-Host
}
}
}
}
}
else
{
Write-Host "Configuration file not found: " $configFile
}
@turowicz
Copy link
Author

Useful for data factories

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment