xml - Find and Replace in PowerShell produces a corrupt/not valid file -


i'm using powershell edit xml file. i'm doing searching string , replacing string within xml. command looks like:

(get-content "c:\my_vm\virtual machines\xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.xml") |  foreach-object {$_ -replace "my_vmname", "my_vmname_0"} | set-content "c:\my_vm\virtual machines\xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.xml" 

where xml file in case virtual machine image imported hyper-v

the find/replace works fine, , can rename .vhdx file import no problem also. using:

rename-item "c:\my_vm\virtual hard disks\my_vmname.vhdx" my_vmname_0.vhdx 

but when try use import-vm using following cmdlet:

import-vm -path "c:\my_vm\virtual machines\xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.xml" -copy -generatenewid 

i error:

"import-vm : virtual machine configuration not read. data might corrupt or not valid."

does know why is? i've come workaround(using different technologies) it's still frustrating. if open text editor , rename manually works fine.

could try one? have issue newlines etc.

foreach ($line in (get-content "c:\my_vm\virtual machines\xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.xml")) {   $line -replace "my_vmname", "my_vmname_0" | out-file new.txt -append } 

more on this: http://powershell.org/wp/2013/10/21/why-get-content-aint-yer-friend/


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -