Code to update HyperV Export file

Posted by Andy Schneider on Server Fault See other posts from Server Fault or by Andy Schneider
Published on 2011-02-04T15:17:49Z Indexed on 2011/02/04 15:27 UTC
Read the original article Hit count: 303

Filed under:
|
|
|

I am using the HyperV Module from Codeplex to do a "config only" export from a 2008R2 Hyper-V server. In order to import the configuration on another HyperV server, I need to edit the value of CopyVMStorage in the EXP file. This file is an XML file. I wrote the following code in PowerShell to do the update for me. The variable $existing is the existing exp file.

$xml = [xml](get-content $existing)
$xpath = '//PROPERTY[@NAME ="CopyVmStorage"]'
foreach ($node in $xml.SelectNodes($xpath))
    {$node.Value = 'TRUE'}
$xml.Save($existing)

This code makes the correct changes to the XML. However, when I go to import the file on the Hyper-V server, I get an error that says the file format is incorrect. I am wondering if the encoding of the file is incorrect or if there is something else going on. If I edit the file manually in wordpad, it imports without an issue. The filename is a GUID with a .exp extension, and it appears that the file name is too long for notepad to open. Notepad throws an error trying to open the file, which is why I went with WordPad.

I have noticed that the file that is updated with PowerShell comes out formatted whereas the raw file is xml all bunched together with no whitespace.

Any ideas on what "file format" means in this HyperV error message and how I might be able to use my code to automate this change in the XML without changing the file format?

© Server Fault or respective owner

Related posts about hyper-v

Related posts about powershell