0

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)

            }
Alex
  • 79
  • 12
  • When using PSv3+ I suggest to change to: `Foreach($file in (Get-ChildItem "$filePath\*.xml" -File)){` –  Dec 14 '18 at 19:48
  • `$file` stringifies to the _mere filename_, not the full path, due to the specifics of the `Get-ChildItem` command used - see the linked post for details. Using `$file.FullName` is a simple workaround. – mklement0 Dec 14 '18 at 20:33

1 Answers1

0

Are you running the script from c:\users...?

Here is something is you can do :

$filepath = "\\server1\SP\ConsensusModeling"
Get-ChildItem $filePath | % {

        $XML = import-clixml $_.FullName #this will get you the childitem full path.
        #
        #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)


}
Hamza
  • 465
  • 3
  • 7
  • Hmm, I might be. I am running the script by typing .(space) "\\scriptpath" and then calling the function, which probably means I am running it from c:\users.. I tried your code but got error: Element 'Objs' with namespace name 'http://schemas.microsoft.com/powershell/2004/04' was not found. Am googling it now – Alex Dec 14 '18 at 20:22
  • 1
    @Hamza: Using `$_.FullName` to force use of the full path is a good tip, but note that the `Import-CliXml` cmdlet is _not_ a general-purpose XML-parsing cmdlet, so this won't work. – mklement0 Dec 14 '18 at 23:18
  • I didn't know about that. Thanks. – Hamza Dec 27 '18 at 21:55