Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anton-abyzov/952215eeffbcc7e3a5f01e615d68d09f to your computer and use it in GitHub Desktop.
Save anton-abyzov/952215eeffbcc7e3a5f01e615d68d09f to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
You can use this script to easly transform any XML file using XDT.
To use this script you can just save it locally and execute it. The script
will download it's dependencies automatically.
#>
[cmdletbinding()]
param(
[Parameter(
Mandatory=$true,
Position=0)]
$sourceFile,
[Parameter(
Mandatory=$true,
Position=1)]
$transformFile
)
Write-Host $sourceFile
Write-Host $transformFile
#Apply config transformation
function applyConfigTransformation($src, $xdt, $dst)
{
Add-Type -Path "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.XmlTransform.dll"
try
{
Write-Host 'applyConfigTransformation - Called'
$doc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument
$doc.PreserveWhiteSpace = $true
Write-Host 'applyConfigTransformation - Load Called'
$doc.Load($src)
Write-Host 'applyConfigTransformation - Load completed'
$trn = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdt)
if ($trn.Apply($doc))
{
Write-Host 'applyConfigTransformation - $trn.Apply called'
$doc.Save($dst)
Write-Output "Output file: $dst"
Write-Host 'applyConfigTransformation - $trn.Apply completed'
}
else
{
throw "Transformation terminated with status False"
}
}
catch
{
Write-Output $Error[0].Exception
}
}
applyConfigTransformation $sourceFile $transformFile $sourceFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment