Last active
July 10, 2018 11:53
-
-
Save inammathe/0a4ac4f9c3d6dfc3b16328cca3cb0920 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Uses XML transformation on a specified XML file | |
.DESCRIPTION | |
Performs XML configuration changes to a given XML file via XML-Document-Transform (XDT) commands specified within a seperate XML file. | |
.EXAMPLE | |
PS C:\> Use-XMLTransform -XML 'D:\myApp\app.config' -XDT 'D:\myApp\app.release.config' | |
Transforms configuration within app.config using the XDT commands found in app.release.config | |
.INPUTS | |
String literal paths to XML/XDT docs and optionally XmlTransform.dll | |
.OUTPUTS | |
boolean - true if successful. false if unsuccessful | |
.NOTES | |
Web.config Transformation Syntax - https://msdn.microsoft.com/en-us/library/dd465326(v=vs.110).aspx | |
Microsoft ASP.NET and Web Tools - https://marketplace.visualstudio.com/items?itemName=JacquesEloff.MicrosoftASPNETandWebTools-9689 | |
#> | |
function Update-XmlByTransform { | |
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Low")] | |
param( | |
# Path to the xml file being transformed | |
[Alias('XML', 'Path')] | |
[Parameter(Position = 0, Mandatory, ValueFromPipeline)] | |
[ValidateScript( | |
{ Test-Path $_ -PathType 'Leaf' } | |
)] | |
[string] | |
$FilePath, | |
# Path to the transform file | |
[Alias('XDT', 'TransformFile', 'TransformPath')] | |
[Parameter(Position = 1, Mandatory)] | |
[ValidateScript( | |
{ Test-Path $_ -PathType 'Leaf' } | |
)] | |
[string] | |
$XmlTransformFile, | |
# Path to the XML transform assembly. Comes with Microsoft ASP.NET and Web Tools https://marketplace.visualstudio.com/items?itemName=JacquesEloff.MicrosoftASPNETandWebTools-9689 | |
[Alias('DllPath')] | |
[Parameter(Position = 2)] | |
[ValidateScript( | |
{ Test-Path $_ -PathType 'Leaf' } | |
)] | |
[string] | |
$XmlTransformDllPath = 'C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.XmlTransform.dll' | |
) | |
begin { | |
$result = $false | |
Write-Verbose "Adding XmlTransformableDocument type from DLL at '$XmlTransformDllPath'" | |
Add-Type -LiteralPath $XmlTransformDllPath | |
Write-Verbose "Loading Xml Transform Document from '$XmlTransformFile'." | |
try { | |
$TransformDocument = New-Object Microsoft.Web.XmlTransform.XmlTransformation($XmlTransformFile) | |
} | |
catch { | |
Write-Output $result | |
Write-Error "Transformation file load failed. The error encountered was: $($_.Exception)" -ErrorAction Stop | |
} | |
} | |
process { | |
Write-Verbose "Loading Xml File from '$FilePath'." | |
$XmlDocument = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument | |
$XmlDocument.PreserveWhitespace = $true | |
try { | |
$XmlDocument.Load($FilePath) | |
} | |
catch { | |
Write-Output $result | |
Write-Error "XML file load failed. The error encountered was: $($_.Exception)" -ErrorAction Stop | |
} | |
try { | |
Write-Verbose 'Applying transform.' | |
if ($PSCmdlet.ShouldProcess($FilePath, "XML Transform using '$XmlTransformFile'")) { | |
$result = $TransformDocument.Apply($XmlDocument) | |
if ($result) { | |
Write-Verbose 'Saving changes' | |
$XmlDocument.Save($FilePath) | |
} | |
} | |
} | |
catch { | |
Write-Error "Transformation failed. The error encountered was: $($_.Exception)" | |
if ($_.Exception.InnerException) { | |
Write-Error $_.Exception.InnerException | |
} | |
} | |
finally | |
{ | |
Write-Output $result | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment