2

I'm in trouble with my script.

I'm grabbing paths out of an XML-File, that need to be modified.

$file = [xml](Get-Content "PATH\TO\XML.xml")

$file.here.is.my.attribute  | Format-Table -Wrap -AutoSize | out-file -filepath ".\PATHS.txt" -append 

Powershell is inserting line-breaks in the middle of my path, because it's to long (Just read about buffersize). I tried to save it to an .XML File instead of an .TXT File but that didn't work for me.

Any Idea of a workaround?

File should look like this:

Path1\Path\Path\path  
Path2\Path\Path\path  
Path3\Path\Path\path 

But it looks like:

Path1\Path\Path\  
path  
Path1\Path\Path\  
path  
Path1\Path\Path\  
path

XML-Example:

<?xml version="1.0" encoding="utf-8"?>
<unattend>
  <servicing>
    <package>
      <assemblyIdentity/>
      <source location="HERE\IS\MY\PATH"/>
    </package>
    <package>
      <assemblyIdentity/>
      <source location="HERE\IS\ANOTHER\PATH"/>
    </package>
    <package>
      <assemblyIdentity/>
      <source location="HERE\IS\ANOTHER\PATH"/>
    </package>
  </servicing>
</unattend>
20
  • 3
    and why precisely do you think you need to pipe Format-Table to Out-File? shouldnt it be either-or? Commented Jan 11, 2016 at 12:37
  • I used it to format the line automatically to the right length. But "autosize" is at its maximum Commented Jan 11, 2016 at 12:38
  • I thought it's necessary to pre-format my data... Commented Jan 11, 2016 at 12:40
  • 1
    Are there multiples <package> elements in one <servicing>? Either way I can pull out that one with $xml.unattend.servicing.package.source.location You can get the attributes via dot notation as well. Commented Jan 11, 2016 at 13:08
  • 1
    you might be missing a ".location" at the end of "$file.here.is.my.attribute" -- not accessing the attribute value properly Commented Jan 11, 2016 at 13:10

1 Answer 1

1

First Format-Table is merely for console output. Trying to use for anything other than that will cause you issues. Given your sample XML you can just use dot notation to get all location's

In your example you were trying to output an object array with a location property. Calling the attribute using dot notation removes that layer from the output.

$xmlFile = [xml](Get-Content "PATH\TO\XML.xml")
$xmlFile.unattend.servicing.package.source.location | Set-Content ".\PATHS.txt" 

I don't think you actually needed to be appending to the file in this case. However if you really are building a larger file then you can use Add-Content in place of Set-Content in my example.

Sample Output

HERE\IS\MY\PATH
HERE\IS\ANOTHER\PATH
HERE\IS\ANOTHER\PATH
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I nearly spend hours on that issue. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.