Last active
March 7, 2018 16:21
-
-
Save foxbit19/862f20c4a9c461774172128b5af403e4 to your computer and use it in GitHub Desktop.
A powershell script to generate web.config from a template
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
clear | |
$template = ".\web.config.template" | |
$confFile = "web.config" | |
"`n***********************" | |
"* Configuration script *" | |
"*************************" | |
function askConfirm(){ | |
$value = Read-Host "Do you want to continue? [Y/n]" | |
return (($value.Length -eq 0) -Or ($value.ToString().ToLower() -eq "y")) | |
} | |
$webConfig = New-Object System.Xml.XmlDocument | |
$webConfig.Load($template) | |
# Hashtable of appkey to replace | |
$appKeys = @{ | |
0="key0"; | |
1="key1"; | |
2="key2"; | |
} | |
# Hashtable of default values to use if no stdin is specified | |
$defaults = @{ | |
0=""; | |
1="default"; | |
2=""; | |
} | |
# Hashtable of messages to show before stdin read | |
$messages = @{ | |
0="Enter the value for key 1"; | |
1="Enter the value for key 2 ["+$defaults[1]+"]"; | |
2="Enter the value for key 3"; | |
} | |
"`n* App key configuration *" | |
"* You will be asked some questions to configure your environment." | |
if (askConfirm) { | |
for ($i = 0; $i -lt $appKeys.Count; $i++) { | |
# read user input | |
$userInput = Read-Host $messages[$i] | |
# if user input is empty, the defaults value will be used | |
if(($userInput.Trim().Length -eq 0) -And ($defaults[$i].Length -gt 0)) { | |
"Using default value '" + $defaults[$i] + "'" | |
$userInput = $defaults[$i] | |
} | |
# Generates new web.config | |
$webConfig.SelectSingleNode('configuration/appSettings/add[@key="' + $appKeys[$i] + '"]').Value = $userInput.ToString() | |
} | |
"`nYour App key configuration was correctly built!" | |
} | |
"`nWrite new configuration file" | |
# Save the file as a web.config | |
$webConfig.Save($confFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment