【问题标题】:Add xml object to xml file in PowerShell将 xml 对象添加到 PowerShell 中的 xml 文件
【发布时间】:2020-06-13 04:20:14
【问题描述】:

我正在尝试使用 PowerShell 创建一个 RDCMan 文件(.rdg xml 文件)。我从定义这个模板开始

$newFileTemplate = '<?xml version="1.0" encoding="utf-8"?>
<RDCMan programVersion="2.7" schemaVersion="3">
    <file>       
    <properties>
        <expanded>False</expanded>
        <name>Office Servers</name>
    </properties>    
    <displaySettings inherit="None">
        <liveThumbnailUpdates>True</liveThumbnailUpdates>
        <allowThumbnailSessionInteraction>False</allowThumbnailSessionInteraction>
        <showDisconnectedThumbnails>True</showDisconnectedThumbnails>
        <thumbnailScale>1</thumbnailScale>
        <smartSizeDockedWindows>True</smartSizeDockedWindows>
        <smartSizeUndockedWindows>False</smartSizeUndockedWindows>
    </displaySettings>
    </file>
</RDCMan>
'

在创建这样的 xml 对象之前

$File = 'D:\Test.rdg'
Set-Content $File $newFileTemplate
[XML]$XMLFile = [XML](Get-Content $File)

然后我想定义一个添加一组服务器的函数

# This function adds a new group element
Function Add-NewGroup($GroupName,$RDCManFile) {
    [xml]$GroupXML = @"
    <group>
      <properties>
        <expanded>False</expanded>
        <name>$GroupName</name>
      </properties>
    </group>
"@
    $Child = $RDCManFile.ImportNode($GroupXML.group, $true)
    $RDCManFile.Configuration.AppendChild($Child)
}

并通过运行调用它

Add-NewGroup('DCs',$XMLFile)

这将允许我使用 AD 中的所有 OU 填充 xml 文件。 谁能告诉我哪里出错了?

谢谢

更新:我得到的错误是

You cannot call a method on a null-valued expression.
At D:\Users\user\Desktop\Projects\RDCMan\Create-RDG.ps1:179 char:5
+     $Child = $RDCManFile.ImportNode($GroupXML.group, $true)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At D:\Users\user\Desktop\Projects\RDCMan\Create-RDG.ps1:180 char:5
+     $RDCManFile.Configuration.AppendChild($Child)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

我正在尝试做这里建议的事情https://stackoverflow.com/a/29693625/2165019

【问题讨论】:

  • 你收到什么样的输出?你有错误吗?如果我们不知道哪里出了问题,就很难说你哪里出了问题。
  • 嗨@AutomatedOrder 感谢您查看此内容。我已经为你更新了描述。

标签: powershell


【解决方案1】:
  1. 在此对象上找不到属性“配置”。验证该属性是否存在。$RDCManFile.Configuration.AppendChild($Child) 处出错。

如果我可以接受Script to create a Remote Desktop Connection Manager group from Active Directory Technet 文章中定义的.rdg 文件格式和结构,那么我会使用

$RDCManFile.RDCMan.file.AppendChild($Child)
  1. 关注Theo's answer关于调用函数。

将参数作为位置传递

Add-NewGroup 'DCs' $XMLFile

或命名

Add-NewGroup -GroupName 'DCs' -RDCManFile $XMLFile

【讨论】:

  • 嗨 JosefZ 非常感谢您的建议!我可以确认该脚本现在正在为我工​​作。
【解决方案2】:

这个错误(和第二个)很可能意味着$RDCManFile 为空。

You cannot call a method on a null-valued expression.
At D:\Users\user\Desktop\Projects\RDCMan\Create-RDG.ps1:179 char:5
+     $Child = $RDCManFile.ImportNode($GroupXML.group, $true)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

尝试在Add-NewGroup 函数的顶部添加$RDCManFile | Out-Host 以检查该变量中包含的内容。

【讨论】:

  • 嗨,Cbsch,看来问题与我调用函数错误有关。感谢您的帮助。
【解决方案3】:

您需要更改调用函数的方式。

由于参数之间的逗号,PowerShell 将其视为一个数组值,因此只有参数 $GroupName 会收到一些东西。

此外,你不应该在参数周围使用括号,这样调用函数:

Add-NewGroup 'DCs' $XMLFile

或使用参数名称发送参数:

Add-NewGroup -GroupName 'DCs' -RDCManFile $XMLFile

【讨论】:

    猜你喜欢
    • 2021-04-22
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2016-01-23
    相关资源
    最近更新 更多