Skip to content

Instantly share code, notes, and snippets.

@mpicker0
Created May 30, 2013 18:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mpicker0/5680072 to your computer and use it in GitHub Desktop.
Save mpicker0/5680072 to your computer and use it in GitHub Desktop.
This PowerShell script runs a Web.config transformation. Visual Studio must be installed, and MSBuild.exe in your path. This is useful for one-off testing.
<#
.SYNOPSIS
Create a configuration transformation
.DESCRIPTION
This script runs an ASP.NET configuration transformation, given a source
configuration and transformation file. MSBuild.exe is assumed to be in
the path, and Visual Studio 2012 should be installed. Modify the path to
Microsoft.Web.Publishing.Tasks.dll if a different version of Visual Studio
is installed.
.PARAMETER SourceFile
The source file to use for transformations
.PARAMETER TransformFile
The transformations to apply to the source file
.PARAMETER OutputFile
Where to write the resulting output
.EXAMPLE
Create-WebConfigTransform -SourceFile C:\path\to\project\Web.config -TransformFile C:\path\to\project\Web.Debug.config -OutputFile c:\temp\transformed.xml
.LINK
http://msdn.microsoft.com/en-us/library/dd465326.aspx
#>
param(
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[string]$SourceFile,
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[string]$TransformFile,
[Parameter(Mandatory=$true)]
[string]$OutputFile
)
# set up output filenames
$WorkDir = Join-Path ${env:temp} "work-${PID}"
$SourceWork = Join-Path $WorkDir (Split-Path $SourceFile -Leaf)
$TransformWork = Join-Path $WorkDir (Split-Path $TransformFile -Leaf)
$OutputWork = Join-Path $WorkDir (Split-Path $OutputFile -Leaf)
# create a working directory and copy files into place
New-Item -Path ${WorkDir} -Type Directory
Copy-Item $SourceFile $WorkDir
Copy-Item $TransformFile $WorkDir
# write the project build file
$BuildXml = @"
<Project ToolsVersion="4.0" DefaultTargets="TransformWebConfig" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TransformXml"
AssemblyFile="`$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
<Target Name="TransformWebConfig">
<TransformXml Source="${SourceWork}"
Transform="${TransformWork}"
Destination="${OutputWork}"
StackTrace="true" />
</Target>
</Project>
"@
$BuildXmlWork = Join-Path $WorkDir "build.xml"
$BuildXml | Out-File $BuildXmlWork
# call msbuild
& MSBuild.exe $BuildXmlWork
# copy the output to the desired location
Copy-Item $OutputWork $OutputFile
# clean up
Remove-Item $WorkDir -Recurse -Force
@mpicker0
Copy link
Author

Thanks to Umar Farooq Khawaja for providing an example of the XML on this StackOverflow thread.

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