Insert given variable into existing xml file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bardman
Posts: 10
Joined: 08 Dec 2011 23:52

Insert given variable into existing xml file

#1 Post by bardman » 27 Apr 2013 18:46

Here is what I am trying to accomplish...if this can be accomplished with only batch...for now?

Instead of actually renaming the client here from the netdom statement, I want to INSERT the %strComputer% variable into an xml file or at least replace the text with that same variable. The xml file will be an unattend.xml file used for SysPrepping a Windows 7 client.

This is the section I would like to modify:

<ComputerName>%strComputername%</ComputerName>

In essence, the insert_name statement would be for the new name to be inserted into the xml file vice the netdom.exe command.

The posted code works pretty darn good in my autorenaming process! The steps outlines how the script works.

Please pay attention to the for /f statements and the :insert_name label. Everything else can be disregarded.

::1. Retrieves the MAC address using WMIC
::2. Compares that MAC address to the pdqmac2pcname.txt file for a MAC address match or mismatch.
::3. When the MAC address matches, the name is piped to the netdom statement as the newname variable %strComputername%
::4. then the script will goto insert_name and
::5. If MAC address is found with a computer name, the script goes to the label kill_autologon to force computer to not autologon. This procedure prevents an improperly renamed computer from being joined to the domain under a SysPreped randomly generated name.
::6. If name is not found or MAC Address is not matched, the computer will not join the domain and
::7. computer reboots to login screen so that it can be manually renamed and joined to the domain.


Code: Select all

setlocal enabledelayedexpansion


FOR /F "skip=1 tokens=*" %%I IN ('WMIC Path Win32_NetworkAdapter Where "NetConnectionID='Local Area Connection'" Get MACAddress') DO IF NOT DEFINED MACAddress SET MACAddress=%%I
   FOR /F "usebackq delims=, tokens=1,2" %%J IN (`type C:\rename_computer\pdqmac2pcname.txt`) DO IF %MACAddress% EQU %%J SET strComputername=%%K && GOTO insert_name
)

goto kill_autologon


:insert_name
C:\windows\system32\netdom.exe renamecomputer %COMPUTERNAME% /NewName:%strComputername% /FORCE

goto shutdown


:kill_autologon
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoLogonCount /f

reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 0 /f


:shutdown
shutdown -r -t 0


exit

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Insert given variable into existing xml file

#2 Post by foxidrive » 27 Apr 2013 19:16

If you want to insert text into an XML file then GnuSED is a good tool, or VBS script with regular expressions or a simple text replace.

Here's one tool that may work.

Code: Select all

::Search and replace
@echo off
if "%~3"=="" (
echo.Search and replace
echo Syntax:
echo %0 "filein.txt" "fileout.ext" "regex" "replace_text" [first]
echo.
echo. if [first] is present only the first occurrence is changed
goto :EOF
)
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
 >_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True           
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs

bardman
Posts: 10
Joined: 08 Dec 2011 23:52

Re: Insert given variable into existing xml file

#3 Post by bardman » 27 Apr 2013 21:40

Thanks foxidrive for the quick response. However, I am not even sure what language that is? That script is daunting for me so I know our other admins will be even more lost. I should have stated--I am not a programmer and only dabble in batch scripting primarily for creating batch startup scripts. Can't batch do regular expressions?

foxidrive wrote:If you want to insert text into an XML file then GnuSED is a good tool, or VBS script with regular expressions or a simple text replace.

Here's one tool that may work.

Code: Select all

::Search and replace
@echo off
if "%~3"=="" (
echo.Search and replace
echo Syntax:
echo %0 "filein.txt" "fileout.ext" "regex" "replace_text" [first]
echo.
echo. if [first] is present only the first occurrence is changed
goto :EOF
)
if "%~5"=="" (set global=true) else (set global=false)
set s=regex.replace(wscript.stdin.readall,"%~4")
 >_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=%global%
>>_.vbs echo regEx.IgnoreCase=True           
>>_.vbs echo regex.pattern="%~3"
>>_.vbs echo wscript.stdOut.write %s%
cscript /nologo _.vbs <"%~1" >"%~2"
del _.vbs

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Insert given variable into existing xml file

#4 Post by Endoro » 27 Apr 2013 22:22

Mostly this work with xml files can be done in batch, but also mostly it is much more difficult. So I would recommend to try it in vbs.

bardman
Posts: 10
Joined: 08 Dec 2011 23:52

Re: Insert given variable into existing xml file

#5 Post by bardman » 28 Apr 2013 01:00

Hi Endoro,

I know that VBS and PowerShell and mostly any other language can accomplish this much much better. However, I nor any other of our admins knows much more than batch for general scripting.

I promise to learn VBS and PowerShell within the next few months! But now is not the time cause our upgrade is next month.

I got an answer though from another site http://superuser.com/questions/588630/insert-given-variable-from-batch-into-existing-xml-file


Endoro wrote:Mostly this work with xml files can be done in batch, but also mostly it is much more difficult. So I would recommend to try it in vbs.

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Insert given variable into existing xml file

#6 Post by Endoro » 28 Apr 2013 01:22

Funny answer there, split the xml.

Btw. in delayed expansion the for loop removes !^
But you don't need delayed expansion in ypur script.
Remove the closing parenthesis before " goto kill_autologon".

bardman
Posts: 10
Joined: 08 Dec 2011 23:52

Re: Insert given variable into existing xml file

#7 Post by bardman » 28 Apr 2013 14:08

Endoro wrote:Funny answer there, split the xml.

Btw. in delayed expansion the for loop removes !^
But you don't need delayed expansion in your script.
Remove the closing parenthesis before " goto kill_autologon".



Yeah it is, but that split works perfectly.
I will probably remove the delayed expansion before final deployment after a few more tests.
Thanks for catching that rogue parenthesis. I am shocked any of my tests haven't thrown any errors--unless I uploaded a different test script. I will verify at work tomorrow.

Post Reply