Forked from markarnott/Add-AspNetCoreEnvVarElems.ps1
Created
February 20, 2018 16:28
-
-
Save maxisam/99380b988c768f4652aa7f6cca454512 to your computer and use it in GitHub Desktop.
A powershell script to inject environmentVariable elements into aspNetCore Element in web.config for ASP.NET Core Module
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
# | |
# == Turn This == | |
# <?xml version="1.0" encoding="utf-8"?> | |
# <configuration> | |
# <system.webServer> | |
# <handlers> | |
# <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> | |
# </handlers> | |
# <aspNetCore processPath=".\MyCoreGizmo.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" /> | |
# </system.webServer> | |
# </configuration> | |
# | |
# == Into This == | |
# | |
# <?xml version="1.0" encoding="utf-8"?> | |
# <configuration> | |
# <system.webServer> | |
# <handlers> | |
# <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" /> | |
# </handlers> | |
# <aspNetCore processPath=".\MyCoreGizmo.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout"> | |
# <environmentVariables> | |
# <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> | |
# </environmentVariables> | |
# </aspNetCore> | |
# </system.webServer> | |
# </configuration> | |
param( | |
[string] $WebConfigPath = ".\web.config", | |
[string] $EnvironmentName = "Development" | |
) | |
$WebConfigXml = [xml](Get-Content $WebConfigPath -Encoding "UTF8") | |
$EVarsParent = $WebConfigXml.CreateElement("environmentVariables") | |
$EVarChild = $WebConfigXml.CreateElement("environmentVariable") | |
$EVarChild.SetAttribute("name", "ASPNETCORE_ENVIRONMENT") | |
$EVarChild.SetAttribute("value", $EnvironmentName) | |
$EVarsParent.AppendChild($EVarChild) | Out-Null | |
$AspNetCoreNode = $WebConfigXml.SelectSingleNode("/configuration/system.webServer/aspNetCore") | |
$AspNetCoreNode.AppendChild($EVarsParent) | Out-Null | |
# How to keep 'pretty print' formatting is a conundrum | |
$PrettyXml = $WebConfigXml.Save([Console]::Out) # This converts encoding='utf-8' to encoding='IBM437' | |
Write-Output $PrettyXml | |
# This loses indentation and line breaks, but preserves the xml encoding | |
Set-Content -Path $WebConfigPath -Value $WebConfigXml.OuterXml -Encoding "UTF8" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment