简体   繁体   中英

Powershell Set-Content seems to corrupt my SSIS .dtsx files

I have build a simple powershell script which replaces the xml path configuration in dtsx files using get-content / set-content.

(Get-Content $file.PSPath) | ForEach-Object {$_ -replace $searchtext , $replacetext} | Set-Content $file.PSPath

When i use it and try to run the package from sql server agent job i get an error saying:

"Unable to load the package as XML because of package does not have a valid XML format."

If i edit the file myself with sublime and not use the script it is ok.

The replace is happening ok, i used kdiff to compare the files and there are no differences.

Any ideas?

Set-Content defaults to ASCII encoding. I wonder if the file was UTF8 with a BOM. Try using Set-Content $file.PSPath -Enc Utf8 . Or maybe it was Unicode, in that case use -Encoding Unicode .

FWIW these types of problems is why I added Edit-File to the PowerShell Community Extensions . With it you can edit the file like so:

Edit-File $file.PSPath $searchText $replacementText

And it handles preserving the file's original encoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM