在PowerShell中将xml对象添加到xml文件

魔方

我正在尝试使用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

约瑟夫·Z
  1. 在此对象上找不到属性“配置”。验证该属性存在。发生错误$RDCManFile.Configuration.AppendChild($Child)

如果我可以接受脚本中.rdg定义文件格式和结构以从Active Directory Technet文章中创建“远程桌面连接管理器”组,则可以使用

$RDCManFile.RDCMan.file.AppendChild($Child)
  1. 遵循Theo关于调用函数的回答

将参数作为位置传递

Add-NewGroup 'DCs' $XMLFile

或命名

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章