Skip to content

Instantly share code, notes, and snippets.

@Axeltherabbit
Last active December 11, 2024 15:34
Show Gist options
  • Save Axeltherabbit/cae3c1fda3fa611f01ab4621c5917113 to your computer and use it in GitHub Desktop.
Save Axeltherabbit/cae3c1fda3fa611f01ab4621c5917113 to your computer and use it in GitHub Desktop.
ps1 script to switch asp.net webconfig profile locally, it's useful if you want to try another profile without publishing
if (-not $($args[0])) {
Write-Host "The profile variable is null or empty."
}
$xml = Resolve-Path "../web.config";
$xdtPath = "../web." + $($args[0]) + ".config"
$xdt = Resolve-Path $xdtPath;
Add-Type -LiteralPath "../Microsoft.Web.XmlTransform.dll"
$xmldoc = New-Object Microsoft.Web.XmlTransform.XmlTransformableDocument;
$xmldoc.PreserveWhitespace = $true
$xmldoc.Load($xml);
$transf = New-Object Microsoft.Web.XmlTransform.XmlTransformation($xdt);
if ($transf.Apply($xmldoc) -eq $false)
{
throw "Transformation failed."
}
$xmldoc.Save($xml);
@Axeltherabbit
Copy link
Author

This is the pre-build hook that can be used:

powershell.exe -ExecutionPolicy Bypass -File ../switchWebConfig.ps1 "$(Configuration)"

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