Link to home
Start Free TrialLog in
Avatar of Chad Franks
Chad Franks

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oBdA
oBdA

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