本文介绍了Powershell - 在

问题描述

我正在使用 PowerShell 加载一个

I am using PowerShell to load an

这是

    <?

我使用这个 Powershell 命令:

I use this Powershell command:

[

但我不确定如何将新记录添加到计算机和用户节点.

But I am not sure how I can add new records to the Computer and User nodes.

例如我想将此添加到计算机:

For example I would like to add this to Computer:

 <PolicyMap xsi:type="OptionalPolicyMap">
          <Name>Test Policy</Name>
          <CspUri>./Device/Vendor/MSFT/Policy/Config/Test</CspUri>
          <CspName>Policy</CspName>
          <Version>0000</Version>
        </PolicyMap>

有人知道我需要做什么才能将这个

Does anyone know what I would need to do to get this block of

谢谢.

推荐答案

[ 转换结构是一个 实例.

What the [ cast constructs is a System. instance.

您可以使用其方法来操作 PowerShell 对该 DOM 的改编 来轻松钻取直到感兴趣的父元素.

You can use its methods to manipulate the

在您的情况下,命名空间的参与使得新元素的插入具有挑战性.

In your case, the involvement of namespaces makes the insertion of the new elements challenging.

# Read the file into an 

注意:如果您不介意再次解析 System. 实例,保存到打印精美的文件就像:

Note: If you don't mind the extra overhead of parsing the System. instance, saving to a pretty-printed file is as easy as:

# Inefficient, but convenient way to save an [


重新 :

请注意,通过设置 [ 实例的 属性$true 在调用 .Load() 之前:

Note that you can opt to preserve insignificant whitespace (as used in pretty-printing) on loading an [ instance's .PreserveWhitepace property to $true before calling .Load():

  • 如果输入文件一开始是漂亮打印的并且您没有进行结构更改.Save() 将保留漂亮打印的格式文件.

  • If the input file was pretty-printed to begin with and you make no structural changes, .Save() will preserve the pretty-printed format of the document.

如果您确实进行了结构更改,则保留漂亮打印格式的唯一方法是手动匹配预期的格式空白以使其适合与现有的漂亮印刷.

If you do make structural changes, the only way to preserve the pretty-printed format is to manually match the expected formatting whitespace so that it fits in with the existing pretty-printing.

总的来说,更强大的方法是:

  • 加载时忽略无关紧要的空格,[ 默认情况下会这样做(也就是说,任何原始的漂亮打印都会丢失).
  • 如果需要,在保存时显式地打印漂亮(不幸的是,[ 并不容易 - 与 [System.).
  • Ignore insignificant whitespace on loading, which [ does by default (that is, any original pretty-printing is lost).
  • Explicitly pretty-print on saving, if needed (which [ doesn't make easy, unfortunately - unlike [System.).

Tomalak 建议将关于此快捷方式的警告表述如下:

除非文件损坏并且 失败,否则您应该永远不要使用 Get-Content 来读取

原因是诸如 [ 之类的语句(或者,没有类型约束变量,$) 可能会误解文件的字符编码:

The reason is that a statement such as [ (or, without type-constraining the variable, $) may misinterpret the file's character encoding:

  • encoding 属性,见下文)的情况下,应该假定为 UTF-8.

  • The default character encoding for encoding attribute in the

    • 但是,在 Windows PowerShellGet-Content 假定 ANSI 编码(系统的活动 ANSI 代码页由系统区域设置(语言用于非 Unicode 程序)) 在没有 BOM 的情况下.幸运的是,PowerShell (Core) v6+ 现在始终默认为(无 BOM)UTF-8.
    • However, in Windows PowerShell Get-Content assumes ANSI encoding (the system's active ANSI code page as determined by the system locale (language for non-Unicode programs)) in the absence of a BOM. Fortunately, PowerShell (Core) v6+ now consistently defaults to (BOM-less) UTF-8.

    虽然您可以在 Windows PowerShell 中使用 -Encoding ut8 解决这个问题,但这还不够,因为 作为文档内容的一部分,即通过 encoding 属性;例如:

    While you can address this problem with -Encoding ut8 in Windows PowerShell, this is not sufficient, because an as part of the document's content, namely via the encoding attribute; e.g.:
    <?

    • 由于 Get-Content 仅根据 BOM 的缺失/存在来决定使用哪种编码,它不是遵守声明的编码.

    • Since Get-Content decides what encoding to use solely based on the absence / presence of a BOM, it does not honor the declared encoding.

    相比之下,[ (System.) 类型的 方法 确实如此,这是为什么它是唯一可靠读取 .

    By contrast, the [ (System.) type's .Load() method does, which is why it is the only robust way to read .

    同样,您应该使用 方法,用于将 [ 实例正确保存到文件中.

    Similarly, you should use the .Save() method for properly saving an [ instance to a file.

    不幸的是,与 Get-Content 快捷方式相比,这种健壮的方法是 (a) 更冗长,更不流畅(您需要显式构造一个 [ 实例,然后在其上调用 .Load() 和 (b) treacherous(由于 .NETAPI 通常看到与 PowerShell 不同的工作目录,相对文件路径故障):

    It is unfortunate that the robust method, compared to the Get-Content shortcut is (a) more verbose and less fluid (you need to construct an [ instance explicitly and then call .Load() on it) and (b) treacherous (due to .NET APIs typically seeing a different working dir. than PowerShell, relative file paths malfunction):

    # The ROBUST WAY to read an 

    你可以缩短成这个成语,但它仍然很别扭:

    You can shorten to this idiom, but it is still awkward:

    ($


    未来的潜在改进:

    • 如果 Get-Content 本身被设为可识别

    • If Get-Content itself were to be made

      • Tomalak has suggested this in GitHub issue #14508.

      或者,可以引入新的语法糖来支持转换System.IO.FileInfo 实例到[,如Get-Item返回Get-ChildItem:

      Alternatively, new syntactic sugar could be introduced to support casting a System.IO.FileInfo instance to [, such as returned by Get-Item and Get-ChildItem:

      # WISHFUL THINKING - cast a FileInfo instance to [

    • 这篇关于Powershell - 在

08-29 02:02