使用PowerShell编辑XML和“文件格式”错误 [英] Editing XML with PowerShell and "file format" error

查看:155
本文介绍了使用PowerShell编辑XML和“文件格式”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Codeplex的HyperV模块从2008R2 Hyper-V服务器导出仅配置。为了在另一台HyperV服务器上导入配置,我需要在EXP文件中编辑CopyVMStorage的值。此文件是一个XML文件。我在PowerShell中编写了以下代码来为我做更新。变量$ existing是现有的exp文件。

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)

此代码对XML进行了正确的更改。但是,当我在Hyper-V服务器上导入文件时,会收到一条错误,表示文件格式不正确。我想知道文件的编码是不正确的还是有其他的。如果我在wordpad中手动编辑文件,那么它会导入而没有问题。

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.

我注意到使用PowerShell更新的文件出来格式化,而原始文件是xml,所有文件都没有空白组合在一起。

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.

关于什么文件格式可能意味着这个HyperV错误消息的任何想法,以及我如何可以使用我的代码自动化这个XML中的这个变化,并能够使用它来导入VM配置?

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之前

<?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

<?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>

注意:这是一个来自 https://serverfault.com/questions/231186/code-to-update-hyperv-export-file 。我相信这实际上是一个编码/开发问题,而不是一个IT专业版的问题。

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.

推荐答案

实例化一个XmlTextWriter并设置将属性格式化为无可以完成您要查找的内容。您可以尝试这样的东西,并根据您的具体需求进行修改。

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

这篇关于使用PowerShell编辑XML和“文件格式”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆