Reputation: 375
I'm using PowerShell to edit an XML file. What I'm doing is searching for a string and replacing that string within the XML. The command for this 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 the XML file in my case is a Virtual Machine image to be imported into Hyper-V
The find/replace works fine, and I can rename the .vhdx file for the import no problem also. I do this using:
Rename-Item "C:\My_VM\Virtual Hard Disks\My_VMName.vhdx" My_VMName_0.vhdx
But when I try to use Import-VM using the following cmdlet:
Import-VM -Path "C:\My_VM\Virtual Machines\xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.XML" -Copy -GenerateNewID
I get the error:
"Import-VM : The virtual machine configuration could not be read. The data might be corrupt or not valid."
Does anyone know why this is? I've already come up with a workaround(using different technologies) but it's still frustrating. If I open a text editor and rename everything manually it works fine.
Upvotes: 0
Views: 1342
Reputation: 555
I had this exact same problem, and that's how I solved it:
Problem
When I tried to use PowerShell to edit a Citrix Receiver *.ica file. If I opened the file with Notepad, replaced the text I wanted, then save it, Receiver will launch the session. However, if I used PowerShell using Get-Content and then Out-File, the file looked normal, but Receiver wouldn't read it and reported a general error.
Solution
The solution turned out to be that, by default, Out-File writes in Unicode format. I needed to change the encoding format to ASCII for it to be in the format required by Receiver.
Upvotes: 2
Reputation: 101
Could you try this one? You probably have an issue with 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/
Upvotes: 1