I am trying to loop through a specific file directory which holds only xml files, and replace specifically defined nodes in each of these files. My issue right now is the file path is being defined as c:\users\myusername\filepath rather than sticking to the original filepath defined in the script. This happens during my $xml assignment, as the $filepath variable is working properly for other commands in the script. Can I not use Get-Content here? I was under the impression that get-content should work since its editing 1 file at a time.
Current Code:
#updating xml and not including sub-directories
$filepath = "\\server1\SP\ConsensusModeling" #folder with multiple xml files, this is NOT located in my \users\ folder.
Foreach($file in Get-ChildItem $filePath | where {!$_.PSIsContainer}){
$xml = [xml](Get-Content $file)
#updating attributes at each node
$node = $xml.ConsensusModelingConfig #location of internal node tree
$node.APPServer = "server1"
$node.APPDB = "ConsensusModelingAPP"
$node.SSASServer = "server1SSAS"
$node.SSASDB = "ConsensusModelingDB"
$xml.Save($filePath)
}
Example of a file in the folder that I'm trying to edit/result I'm trying to create. These are currently different values and I want to set them to the values below. There are 4 of these. ConsensusModeling.xml:
<ConsensusModelingConfig>
<APPServer>Server1</APPServer>
<APPDB>ConsensusModelingAPP</APPDB>
<SSASServer>Server1SSAS</SSASServer>
<SSASDB>ConsensusModelingDB</SSASDB>
</ConsensusModelingConfig>
Error Message:
Get-Content : Cannot find path 'C:\Users\myusername\mytestfile
At \\scriptlocation.ps1:628 c
+ $xml = [xml](Get-Content $file)
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound:
(C:\Users\Alex.p...eling_Score.xml:String) [G
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
According to the error message, my path is getting assigned to C:\users etc instead of staying the original filepath defined in the $filepath variable. Why is this?
Solution: As per @mklement0's comment, the following code fixed my issues and now all files are having their attributes properly updated.
#updating xml and not including sub-directories
$filepath = "\\server1\SP\ConsensusModeling" #folder with multiple xml files, this is NOT located in my \users\ folder.
Foreach($file in Get-ChildItem $filePath | where {!$_.PSIsContainer}){
$xml = [xml](Get-Content $file.FullName)
#updating attributes at each node
$node = $xml.ConsensusModelingConfig #location of internal node tree
$node.APPServer = "server1"
$node.APPDB = "ConsensusModelingAPP"
$node.SSASServer = "server1SSAS"
$node.SSASDB = "ConsensusModelingDB"
$xml.Save($file.FullName)
}