Need script to ask for user input and end up modifying xml file.

I have an xml file I use to pass to my powershell script to created folders, shares and my IIS website.  However I have to manually go in an edit my xml input file.  I wanted to create a script/batch file to ask the user for input and then modify the xml file.

This is what I have so far:

xml file:

<?xml version="1.0"?>
<item>
            
            <application_env>test</application_env>
            <application_name>app-test</application_name>
            <application_fqdn>app-test.company.com</application_fqdn>
            <framework_version>40</framework_version>
            <managedPipelineMode>Integrated</managedPipelineMode>
            <apppoolidentity>yes</apppoolidentity>
            <application_id>app\svc-test</application_id>
            <application_pw>#@$$#</application_pw>
            <application_developers>App-Web-DEV</application_developers>
   

</item>

Open in new window


here is my batch file:

@echo off
setlocal EnableDelayedExpansion

set /p Application_env=App Env:
set /p Application_name=App Name:
set /p Application_FQDN=App FQDN:
set /p Framework_ver=Framework version:
set managedPipelineMode=Integrated
set apppoolidentity=yes
set /p application_id=AppPool ID:
set /p application_pw=AppPool PW:
set /p application_developers=App Developers:

(for /F "delims=" %%a in (input.xml) do (
   set "line=%%a"
   set "newLine=!line:application_env>=!"
   if "!newLine!" neq "!line!" (
      set "newLine=<application_env>%application_env%</application_env>"
   )
   echo !newLine!
   set "newLine=!line:application_name>=!"
   if "!newLine!" neq "!line!" (
      set "newLine=<application_name>%application_name%</application_name>"
   )
)) > newFile.xml

Open in new window


It will change the first line of the XML file but nothing else.  I think my logic/syntax may be wrong with the !newline.  

Any help would be greatly appriciated
LVL 6
Chad FranksSenior System EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Since you're passing it to a Powershell script anyway, why not create the queries and the xml file in PS as well, and treat it as xml?
This will ask for the values, and use the default value from the xml if no input was given.
$ExportXml = "C:\Temp\newfile.xml"
[xml]$xml = @'
<?xml version="1.0"?>
<item>
	<application_env>Default Application Environment</application_env>
	<application_name>Default Application</application_name>
	<application_fqdn>app-test.company.com</application_fqdn>
	<framework_version>40</framework_version>
	<managedPipelineMode>Integrated</managedPipelineMode>
	<apppoolidentity>yes</apppoolidentity>
	<application_id>app\svc-test</application_id>
	<application_pw>#@$$#</application_pw>
	<application_developers>App-Web-DEV</application_developers>
</item>
'@

$XmlHash = [ordered]@{}
$XmlHash["application_env"] =			Read-Host "App Env ['$($xml.SelectSingleNode("/item/application_env").InnerText)']"
$XmlHash["application_name"] =			Read-Host "App Name ['$($xml.SelectSingleNode("/item/application_name").InnerText)']"
$XmlHash["application_fqdn"] =			Read-Host "App FQDN ['$($xml.SelectSingleNode("/item/application_fqdn").InnerText)']"
$XmlHash["framework_version"] =			Read-Host "Framework version ['$($xml.SelectSingleNode("/item/framework_version").InnerText)']"
$XmlHash["managedPipelineMode"] =		Read-Host "Managed Pipeline mode ['$($xml.SelectSingleNode("/item/managedPipelineMode").InnerText)']"
$XmlHash["apppoolidentity"] =			Read-Host "AppPool Identity ['$($xml.SelectSingleNode("/item/apppoolidentity").InnerText)']"
$XmlHash["application_id"] =			Read-Host "AppPool ID ['$($xml.SelectSingleNode("/item/application_id").InnerText)']"
$XmlHash["application_pw"] =			Read-Host "AppPool PW ['$($xml.SelectSingleNode("/item/application_pw").InnerText)']"
$XmlHash["application_developers"] =	Read-Host "App Developers ['$($xml.SelectSingleNode("/item/application_developers").InnerText)']"
ForEach ($Key In $XmlHash.Keys) {
	If (-Not [string]::IsNullOrEmpty($XmlHash[$Key])) {
		($xml.SelectSingleNode("/item/$($Key)")).InnerText = $XmlHash[$Key]
	}
}
$xml.Save($ExportXml)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
oBdACommented:
Come to think of it, here's v2.0. Everything's now configured in the xml template; the additional attributes will be removed in the xml generated.
$ExportXml = "C:\Temp\newfile.xml"
[xml]$xml = @'
<?xml version="1.0"?>
<item>
	<application_env query="yes" prompt="App Env">Default Application Environment</application_env>
	<application_name query="yes" prompt="App Name">Default Application</application_name>
	<application_fqdn query="yes" prompt="App FQDN">app-test.company.com</application_fqdn>
	<framework_version query="yes" prompt="Framework version">40</framework_version>
	<managedPipelineMode query="no" prompt="Managed Pipeline mode">Integrated</managedPipelineMode>
	<apppoolidentity query="no" prompt="AppPool Identity">yes</apppoolidentity>
	<application_id query="yes" prompt="AppPool ID">app\svc-test</application_id>
	<application_pw query="yes" prompt="AppPool PW">#@$$#</application_pw>
	<application_developers query="yes" prompt="App Developers">App-Web-DEV</application_developers>
</item>
'@
ForEach ($Node In $xml.SelectNodes("/item/*")) {
	If ($Node.GetAttribute("query") -eq "yes") {
		$Response = Read-Host "$($Node.GetAttribute('prompt')) ['$($Node.InnerText)']"
		If (-Not [string]::IsNullOrEmpty($Response.Trim())) {
			$Node.InnerText = $Response.Trim()
		}
	}
	$Node.RemoveAllAttributes()
}
$xml.Save($ExportXml)

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.