0

I am trying to create an RDCMan file (.rdg xml file) using PowerShell. I have started by defining this template

$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>
'

before creating an xml object like so

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

I would then like to define a function for adding a group of servers

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

And call it by running

Add-NewGroup('DCs',$XMLFile)

This would allow me to populate the xml file with all of the OUs in AD. Is anyone able to tell me where I am going wrong?

Thanks

Update: The error I am getting is

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

And I am trying to do what was suggested here https://stackoverflow.com/a/29693625/2165019

TheCube
  • 17
  • 5
  • What kind of output are you receiving? Are you getting errors? It's hard to say where you're going wrong if we don't know what is wrong. – AutomatedOrder Jun 11 '20 at 18:14
  • Hi @AutomatedOrder thanks for having a look at this. I have updated the description for you. – TheCube Jun 12 '20 at 07:59

3 Answers3

0

This error (and second) most likely means that $RDCManFile is null.

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

Try adding $RDCManFile | Out-Host at the top of the Add-NewGroup function to check what's contained in that variable.

Cbsch
  • 1,164
  • 2
  • 10
  • 16
  • Hi Cbsch it looks like the problem was related to me calling the function wrong. Thanks for the help. – TheCube Jun 14 '20 at 16:12
0

You need to change the way you are calling the function.

Because of the comma in between the parameters, PowerShell sees this as one array value, so only parameter $GroupName will receive something.

Furthermore, you should not use brackets around the params, call the function like this:

Add-NewGroup 'DCs' $XMLFile

or send the parameters using their names:

Add-NewGroup -GroupName 'DCs' -RDCManFile $XMLFile
Theo
  • 57,719
  • 8
  • 24
  • 41
0
  1. The property 'Configuration' cannot be found on this object. Verify that the property exists. error at $RDCManFile.Configuration.AppendChild($Child).

If I can accept a .rdg file format and structure as defined in the Script to create a Remote Desktop Connection Manager group from Active Directory Technet article then I'd use

$RDCManFile.RDCMan.file.AppendChild($Child)
  1. Follow the Theo's answer about calling a function.

Pass parameters as positional

Add-NewGroup 'DCs' $XMLFile

or as named

Add-NewGroup -GroupName 'DCs' -RDCManFile $XMLFile
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Hi JosefZ thank you so much for your suggestions! I can confirm that the script is now working for me. – TheCube Jun 14 '20 at 16:10