Editing XML with PowerShell and "file format" error
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.
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" could mean in this HyperV error message and how I might be able to use my code to automate this change in the XML and be able to use it to import the VM config?
XML Before
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<DECLARATIONS>
<DECLGROUP>
<VALUE.OBJECT><INSTANCE CLASSNAME="Msvm_VirtualSystemExportSettingData"><PROPERTY NAME="Caption" TYPE="string"><VALUE>Virtual System Export Setting Data</VALUE></PROPERTY><PROPERTY NAME="CopySnapshotConfiguration" TYPE="uint8"><VALUE>0</VALUE></PROPERTY><PROPERTY NAME="CopyVmRuntimeInformation" TYPE="boolean"><VALUE>FALSE</VALUE></PROPERTY><PROPERTY NAME="CopyVmStorage" TYPE="boolean"><VALUE>FALSE</VALUE></PROPERTY><PROPERTY NAME="CreateVmExportSubdirectory" TYPE="boolean"><VALUE>TRUE</VALUE></PROPERTY><PROPERTY NAME="Description" TYPE="string"><VALUE>Microsoft Virtual System Export Setting Data</VALUE></PROPERTY><PROPERTY NAME="ElementName" TYPE="string"><VALUE>Microsoft Virtual System Export Setting Data</VALUE></PROPERTY><PROPERTY NAME="InstanceID" TYPE="string"><VALUE>Microsoft:A1F914F2-F38E-48A6-B1EE-58B84ECEAC0C</VALUE></PROPERTY><PROPERTY NAME="SnapshotVirtualSystem" TYPE="string"></PROPERTY></INSTANCE>
</VALUE.OBJECT>
XML After
开发者_开发技巧<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<DECLARATIONS>
<DECLGROUP>
<VALUE.OBJECT>
<INSTANCE CLASSNAME="Msvm_VirtualSystemExportSettingData">
<PROPERTY NAME="Caption" TYPE="string">
<VALUE>Virtual System Export Setting Data</VALUE>
</PROPERTY>
<PROPERTY NAME="CopySnapshotConfiguration" TYPE="uint8">
<VALUE>0</VALUE>
</PROPERTY>
<PROPERTY NAME="CopyVmRuntimeInformation" TYPE="boolean">
<VALUE>FALSE</VALUE>
</PROPERTY>
<PROPERTY NAME="CopyVmStorage" TYPE="boolean">
<VALUE>TRUE</VALUE>
</PROPERTY>
<PROPERTY NAME="CreateVmExportSubdirectory" TYPE="boolean">
<VALUE>TRUE</VALUE>
</PROPERTY>
<PROPERTY NAME="Description" TYPE="string">
<VALUE>Microsoft Virtual System Export Setting Data</VALUE>
</PROPERTY>
<PROPERTY NAME="ElementName" TYPE="string">
<VALUE>Microsoft Virtual System Export Setting Data</VALUE>
</PROPERTY>
<PROPERTY NAME="InstanceID" TYPE="string">
<VALUE>Microsoft:A1F914F2-F38E-48A6-B1EE-58B84ECEAC0C</VALUE>
</PROPERTY>
<PROPERTY NAME="SnapshotVirtualSystem" TYPE="string">
</PROPERTY>
</INSTANCE>
</VALUE.OBJECT>
NOTE: This is a cross post from https://serverfault.com/questions/231186/code-to-update-hyperv-export-file. I believe this is actually more of a coding/dev problem rather than an IT Pro question.
Instantiating an XmlTextWriter and setting the Formatting property to "None" should accomplish what you are looking for. You could try something like this and modify to your specific needs.
function NoFormat-XML ([xml]$xml)
{
$StringWriter = New-Object System.IO.StringWriter
$XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter
$xmlWriter.Formatting = "None"
$xml.WriteContentTo($XmlWriter)
$XmlWriter.Flush()
$StringWriter.Flush()
Write-Output $StringWriter.ToString()
}
精彩评论