I am quite new to PowerShell. Currently, I'm struggling with the following setup. There are several XML files contained in this folder structure:
Root
|_2020-XX-XX
|_file1.xml
|_file2.xml
|_2020-yy-yy
|_file1.xml
|_file2.xml
Now, I want to create a script that merges parts of each folder's xml files into one.
The result should look something like this:
|_2020-XX-XX
|_file1.xml
|_file2.xml
|_newfile2020XXXX.xml
|_2020-yy-yy
|_file1.xml
|_file2.xml
|_newfile2020YYYY.xml
So far, my script looks like this:
$date=Get-Date -Format "yyyyMMdd"
#Get the date for naming the output-file
$xmlFilePath = Get-ChildItem "\\Server\Rootfolder" -Recurse -Force | Sort-Object LastWriteTime
#Get and sort the xml-Files by date
$finalXml = "<root>"
foreach ($file in $_.xmlFilePath) {
[xml]$xml = Get-Content $file
$finalXml += $xml.root.InnerXml
}
$finalXml += "</root>"
#XML Object closer
([xml]$finalXml).Save("$pwd\newfile$date.xml")
While the debugger does not produce any error, the created file is
- neither saved in the correct directory ("$pwd" does not seem to be the correct variable)
- empty except for the
<root></root>nodes.
I can't quite figure out what to do, to troubleshoot this and would be grateful for any advice on this.
Thanks in advance for any hint!
$_.xmlFilePath->$xmlFilePath