Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<#
**Prototype**
Updates the properties of one or more Sitecore Commerce policies in a policy set or environment json file.
# Patch all policies from another file
Patch-SitecoreCommercePolicySet -Path "path\to\policy.json" -SourcePath "path\to\source.json"
# Patch a specific policy from another file
Patch-SitecoreCommercePolicySet -Path "path\to\policy.json" -SourcePath "path\to\source.json" -PolicyType "Fully.Qualified.Type, With.Assembly.But.Spaces.Are.Ignored"
# Single inline policy
Patch-SitecoreCommercePolicySet -Path "path\to\policy.json" -PolicyType "Fully.Qualified, Type" -Properties @{ "Property1" = "Value1"; "Property2" = "Value2" }
# Multiple inline policies
Patch-SitecoreCommercePolicySet -Path "path\to\policy.json" -SourcePolicies @(
@{
'$type' = "Fully.Qualified.Type, Note.The.SingleQuotes.On.$type",
"Property1" = "Value1";
"Property2" = "Value2";
},
@{
'type' = "Another.Type, Qualified",
"Etc" = "Etc"
}
)
#>
[CmdletBinding(DefaultParameterSetName = 'singleparametersource')]
Param(
[string]$Path,
[Parameter(ParameterSetName='filesource', Mandatory=$true)]
[string]$SourcePath,
[Parameter(ParameterSetName='multipleparametersource')]
[Hashtable[]]$SourcePolicies,
[Parameter(ParameterSetName='filesource', Mandatory=$false)]
[Parameter(ParameterSetName='singleparametersource', Mandatory=$true)]
[string]$PolicyType,
[Parameter(ParameterSetName='singleparametersource', Mandatory=$true)]
[Hashtable]$Properties
)
function NormalizeTypeName([string]$typeName) {
return $typeName -replace " ",""
}
$SourcePolicies = if ($PSCmdlet.ParameterSetName -eq "filesource") {
$sourceJson = Get-Content -Path $Path -Raw | ConvertFrom-Json
if ($PolicyType) {
@($sourceJson.Policies | Where-Object { (NormalizeTypeName $_.'$type') -eq (NormalizeTypeName $PolicyType) })
} else {
@($sourceJson.Policies)
}
} elseif ($PSCmdlet.ParameterSetName -eq "singleparametersource") {
$clonedProperties = $Properties.Clone()
$clonedProperties['$type'] = $PolicyType
@($clonedProperties)
} else {
@($SourcePolicies)
}
$targetJson = Get-Content -Path $Path -Raw | ConvertFrom-Json
$targetJsonPolicies = $targetJson.Policies.'$values'
$SourcePolicies | Foreach-Object {
$sourcePolicy = $_
$targetPolicyIndex = (0..(@($targetJsonPolicies).Count-1)) | Where-Object {
(NormalizeTypeName $targetJson.Policies.'$values'[$_].'$type') -eq (NormalizeTypeName $sourcePolicy.'$type')
}
if ($targetPolicyIndex -eq $null) {
$targetPolicy = @{
'$type' = $sourcePolicy.'$type'
}
$targetJson.Policies.'$values' += @($targetPolicy)
$targetPolicyIndex = @($targetJson.Policies).Count-1
} else {
$targetPolicy = $targetJson.Policies.'$values'[$targetPolicyIndex].psobject.properties | % { $ht = @{} } { $ht[$_.Name] = $_.Value } { $ht }
$targetJson.Policies.'$values'[$targetPolicyIndex] = $targetPolicy
}
$sourcePolicy.Keys | Where-Object { $_ -ne '$type' } | Foreach-Object {
$targetPolicy[$_] = $sourcePolicy[$_]
}
}
$targetJson | ConvertTo-Json -Depth 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment