PoshCode Logo PowerShell Code Repository

New-RDCManFile (modification of post by view diff)
View followups from yguyuygu | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1893"></script>download | new post

Script to create an XML-file for use with Microsoft Remote Desktop Connection Manager
For more details, see the following blog-post: http://blog.powershell.no/2010/06/02/dynamic-remote-desktop-connection-manager-connection-list

  1. ###########################################################################
  2. #
  3. # NAME: New-RDCManFile.ps1
  4. #
  5. # AUTHOR: Jan Egil Ring
  6. # EMAIL: jer@powershell.no
  7. #
  8. # COMMENT: Script to create a XML-file for use with Microsoft Remote Desktop Connection Manager
  9. #          For more details, see the following blog-post: http://blog.powershell.no/2010/06/02/dynamic-remote-desktop-connection-manager-connection-list
  10. #
  11. # You have a royalty-free right to use, modify, reproduce, and
  12. # distribute this script file in any way you find useful, provided that
  13. # you agree that the creator, owner above has no warranty, obligations,
  14. # or liability for such use.
  15. #
  16. # VERSION HISTORY:
  17. # 1.0 02.06.2010 - Initial release
  18. #
  19. ###########################################################################
  20.  
  21. #Importing Microsoft`s PowerShell-module for administering ActiveDirectory
  22. Import-Module ActiveDirectory
  23.  
  24. #Initial variables
  25. $domain = $env:userdomain
  26. $OutputFile = "$home\$domain.rdg"
  27.  
  28. #Create a template XML
  29. $template = @'
  30. <?xml version="1.0" encoding="utf-8"?>
  31. <RDCMan schemaVersion="1">
  32.    <version>2.2</version>
  33.    <file>
  34.        <properties>
  35.            <name></name>
  36.            <expanded>True</expanded>
  37.            <comment />
  38.            <logonCredentials inherit="FromParent" />
  39.            <connectionSettings inherit="FromParent" />
  40.            <gatewaySettings inherit="FromParent" />
  41.            <remoteDesktop inherit="FromParent" />
  42.            <localResources inherit="FromParent" />
  43.            <securitySettings inherit="FromParent" />
  44.            <displaySettings inherit="FromParent" />
  45.        </properties>
  46.        <group>
  47.            <properties>
  48.                <name></name>
  49.                <expanded>True</expanded>
  50.                <comment />
  51.                <logonCredentials inherit="None">
  52.                    <userName></userName>
  53.                    <domain></domain>
  54.                    <password storeAsClearText="False"></password>
  55.                </logonCredentials>
  56.                <connectionSettings inherit="FromParent" />
  57.                <gatewaySettings inherit="None">
  58.                    <userName></userName>
  59.                    <domain></domain>
  60.                    <password storeAsClearText="False" />
  61.                    <enabled>False</enabled>
  62.                    <hostName />
  63.                    <logonMethod>4</logonMethod>
  64.                    <localBypass>False</localBypass>
  65.                    <credSharing>False</credSharing>
  66.                </gatewaySettings>
  67.                <remoteDesktop inherit="FromParent" />
  68.                <localResources inherit="FromParent" />
  69.                <securitySettings inherit="FromParent" />
  70.                <displaySettings inherit="FromParent" />
  71.            </properties>
  72.            <server>
  73.                <name></name>
  74.                <displayName></displayName>
  75.                <comment />
  76.                <logonCredentials inherit="FromParent" />
  77.                <connectionSettings inherit="FromParent" />
  78.                <gatewaySettings inherit="FromParent" />
  79.                <remoteDesktop inherit="FromParent" />
  80.                <localResources inherit="FromParent" />
  81.                <securitySettings inherit="FromParent" />
  82.                <displaySettings inherit="FromParent" />
  83.            </server>
  84.        </group>
  85.    </file>
  86. </RDCMan>
  87. '@
  88.  
  89. #Output template to xml-file
  90. $template | Out-File $home\RDCMan-template.xml -encoding UTF8
  91.  
  92. #Load template into XML object
  93. $xml = New-Object xml
  94. $xml.Load("$home\RDCMan-template.xml")
  95.  
  96. #Set file properties
  97. $file = (@($xml.RDCMan.file.properties)[0]).Clone()
  98. $file.name = $domain
  99. $xml.RDCMan.file.properties | Where-Object { $_.Name -eq "" } | ForEach-Object  { [void]$xml.RDCMan.file.ReplaceChild($file,$_) }
  100.  
  101. #Set group properties
  102. $group = (@($xml.RDCMan.file.group.properties)[0]).Clone()
  103. $group.name = $env:userdomain
  104. $group.logonCredentials.Username = $env:username
  105. $group.logonCredentials.Domain = $domain
  106. $xml.RDCMan.file.group.properties | Where-Object { $_.Name -eq "" } | ForEach-Object  { [void]$xml.RDCMan.file.group.ReplaceChild($group,$_) }
  107.  
  108. #Use template to add servers from Active Directory to xml
  109. $server = (@($xml.RDCMan.file.group.server)[0]).Clone()
  110. Get-ADComputer -LDAPFilter "(operatingsystem=*server*)" | select name,dnshostname |
  111. ForEach-Object {
  112. $server = $server.clone()      
  113. $server.DisplayName = $_.Name  
  114. $server.Name = $_.DNSHostName
  115. $xml.RDCMan.file.group.AppendChild($server) > $null}
  116. #Remove template server
  117. $xml.RDCMan.file.group.server | Where-Object { $_.Name -eq "" } | ForEach-Object  { [void]$xml.RDCMan.file.group.RemoveChild($_) }
  118.  
  119. #Save xml to file
  120. $xml.Save($OutputFile)
  121.  
  122. #Remove template xml-file
  123. Remove-Item $home\RDCMan-template.xml -Force

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me