Editing XML with PowerShell and the "File Format" Error

I am using Codeplex's HyperV module to export "config only" from the 2008R2 Hyper-V server. To import the configuration to another HyperV server, I need to edit the CopyVMStorage value in the EXP file. This file is an XML file. I wrote the following code in PowerShell to do an update for me. The $ existing variable is an existing ex 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 right changes to the XML. However, when I go to import the file to the Hyper-V server, I get an "invalid file format" error message. I am wondering if the file encoding is incorrect or if something else is happening. If I manually edit the file in the text box, it imports without problems.

I noticed that a file updated using PowerShell exits the formatted file, while the raw file is xml, all grouped together without spaces.

Any ideas on what the “file format” might mean in this HyperV error message and how can I use my code to automate this change in XML and be able to use it to import the virtual machine configuration?

XML Up

<?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 the cross point from https://serverfault.com/questions/231186/code-to-update-hyperv-export-file . I believe this is more of a / dev coding issue, not an IT Pro issue.

+3
source share
1 answer

XmlTextWriter "" , . - .

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()
}
0

Source: https://habr.com/ru/post/1792583/


All Articles