PoshCode Logo PowerShell Code Repository

Set-RDPSetting by Jason Archer 8 years ago
View followups from jesper | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/679"></script>download | new post

Function/script to set settings in a RDP file for Terminal Services. Supports pipeline input and smart conversion of bools.

Uncomment the function code to make a global function.

  1. ########################################################################################################################
  2. # NAME
  3. #     Set-RDPSetting
  4. #
  5. # SYNOPSIS
  6. #     Adds or updates a named property to an existing RDP file.
  7. #
  8. # SYNTAX
  9. #     Edit-RDP [-Path] <string> [-Name] <string> [[-Value] <object>] [-PassThru]
  10. #
  11. # DETAILED DESCRIPTION
  12. #     The Edit-RDP cmdlet can be used to add or update properties in an existing RDP file.  This cmdlet can properly
  13. #     translate boolean values or strings containing boolean or integer values for properties that are integers.
  14. #
  15. # PARAMETERS
  16. #     -Path <string>
  17. #         Specifies the path to the RDP file.
  18. #
  19. #         Required?                    true
  20. #         Position?                    1
  21. #         Default value                
  22. #         Accept pipeline input?       false
  23. #         Accept wildcard characters?  false
  24. #
  25. #     -Name <string>
  26. #         Specifies the name of the property to set.
  27. #
  28. #         Required?                    false
  29. #         Position?                    2
  30. #         Default value                
  31. #         Accept pipeline input?       false
  32. #         Accept wildcard characters?  false
  33. #
  34. #     -Value <Object>
  35. #         Specifies the value to set the property to.
  36. #
  37. #         Required?                    false
  38. #         Position?                    3
  39. #         Default value                
  40. #         Accept pipeline input?       false
  41. #         Accept wildcard characters?  false
  42. #
  43. #     -PassThru <SwitchParameter>
  44. #         Passes thru an object that represents the RDP file to the pipeline.  By default, this cmdlet does
  45. #         not pass any objects through the pipeline.
  46. #
  47. #         Required?                    false
  48. #         Position?                    named
  49. #         Default value                false
  50. #         Accept pipeline input?       false
  51. #         Accept wildcard characters?  false
  52. #
  53. # INPUT TYPE
  54. #    
  55. #
  56. # RETURN TYPE
  57. #     System.IO.FileInfo
  58. #
  59. # NOTES
  60. #
  61. #     -------------------------- EXAMPLE 1 --------------------------
  62. #
  63. #     C:\PS>Set-RDPSetting -Path C:\myserver.rdp -Name "full address" -Value "myotherserver"
  64. #
  65. #
  66. #     This command changes the name of the server to connect to.
  67. #
  68. #
  69. #     -------------------------- EXAMPLE 2 --------------------------
  70. #
  71. #     C:\PS>Set-RDPSetting -Path C:\myserver.rdp -Name "redirectclipboard" -Value $true
  72. #
  73. #
  74. #     This command turns on redirection of the clipboard from the remote computer.
  75. #
  76. #
  77. #     -------------------------- EXAMPLE 3 --------------------------
  78. #
  79. #     C:\PS>Get-RDPSetting -Path C:\template.rdp | Set-RDPSetting -Path C:\myserver.rdp | Start-RDP
  80. #
  81. #
  82. #     This command updates all the settings in "myserver.rdp" with the corresponding values from "template.rdp",
  83. #     and then opens a connection with the resulting file.
  84. #
  85. #
  86.  
  87. #Function global:Set-RDPSetting {
  88.     param(
  89.         [string]$Path = $(throw "A path to a RDP file is required."),
  90.         [string]$Name = "",
  91.         [Object]$Value = "",
  92.         [switch]$Passthru
  93.     )
  94.    
  95.     begin {
  96.         if (Test-Path $path) {
  97.                $content = Get-Content -Path $path
  98.         } else {
  99.             throw "Path does not exist."
  100.         }
  101.        
  102.         $datatypes = @{
  103.             'allow desktop composition' = 'i';
  104.             'allow font smoothing' = 'i';
  105.             'alternate shell' = 's';
  106.             'audiomode' = 'i';
  107.             'authentication level' = 'i';
  108.             'auto connect' = 'i';
  109.             'autoreconnection enabled' = 'i';
  110.             'bitmapcachepersistenable' = 'i';
  111.             'compression' = 'i';
  112.             'connect to console' = 'i';
  113.             'desktopheight' = 'i';
  114.             'desktopwidth' = 'i';
  115.             'disable cursor setting' = 'i';
  116.             'disable full window drag' = 'i';
  117.             'disable menu anims' = 'i';
  118.             'disable themes' = 'i';
  119.             'disable wallpaper' = 'i';
  120.             'displayconnectionbar' = 'i';
  121.             'domain' = 's';
  122.             'drivestoredirect' = 's';
  123.             'full address' = 's';
  124.             'gatewaycredentialssource' = 'i';
  125.             'gatewayhostname' = 's';
  126.             'gatewayprofileusagemethod' = 'i';
  127.             'gatewayusagemethod' = 'i';
  128.             'keyboardhook' = 'i';
  129.             'maximizeshell' = 'i';
  130.             'negotiate security layer' = 'i';
  131.             'password 51' = 'b';
  132.             'prompt for credentials' = 'i';
  133.             'promptcredentialonce' = 'i';
  134.             'port' = 'i';
  135.             'redirectclipboard' = 'i';
  136.             'redirectcomports' = 'i';
  137.             'redirectdrives' = 'i';
  138.             'redirectposdevices' = 'i';
  139.             'redirectprinters' = 'i';
  140.             'redirectsmartcards' = 'i';
  141.             'remoteapplicationmode' = 'i';
  142.             'screen mode id' = 'i';
  143.             'server port' = 'i';
  144.             'session bpp' = 'i';
  145.             'shell working directory' = 's';
  146.             'smart sizing' = 'i';
  147.             'username' = 's';
  148.             'winposstr' = 's'
  149.         }
  150.     }
  151.    
  152.     process {
  153.         if ($_.Name) {
  154.             $tempname = $_.Name
  155.             $tempvalue = $_.Value
  156.             # Convert value
  157.             if ($datatypes[$tempname] -eq 'i') {
  158.                 if (($tempvalue -eq $true) -or ($tempvalue -imatch '^true$')) {
  159.                     $tempvalue = 1
  160.                 } elseif (($tempvalue -eq $false) -or ($tempvalue -imatch '^false$') -or ($tempvalue -eq '')) {
  161.                     $tempvalue = 0
  162.                 } elseif ($tempvalue -match '^[0-9]+$') {
  163.                     $tempvalue = [int]$tempvalue
  164.                 }
  165.             } else {
  166.                 $tempvalue = [string]$tempvalue
  167.             }
  168.            
  169.             # Set or update property
  170.             $found = $false
  171.             $content = $content | ForEach-Object {
  172.                 if ($_ -match "^$tempname") {
  173.                     "${tempname}:$($datatypes[$tempname]):$tempvalue"
  174.                     $found = $true
  175.                 } else {$_}
  176.             }
  177.             if (!$found) {
  178.                 $content += @("${tempname}:$($datatypes[$tempname]):$tempvalue")
  179.             }
  180.         }
  181.     }
  182.    
  183.     end {
  184.         if ($name) {
  185.             # Convert value
  186.             if ($datatypes[$name] -eq 'i') {
  187.                 if (($value -eq $true) -or ($value -imatch '^true$')) {
  188.                     [int]$value = 1
  189.                 } elseif (($value -eq $false) -or ($value -imatch '^false$') -or ($value -eq '')) {
  190.                     [int]$value = 0
  191.                 } elseif ($value -match '^[0-9]+$') {
  192.                     [int]$value = $value
  193.                 }
  194.             } else {
  195.                 [string]$value = $value
  196.             }
  197.            
  198.             # Set or update property
  199.             $found = $false
  200.             $content = $content | ForEach-Object {
  201.                 if ($_ -match "^$name") {
  202.                     "${name}:$($datatypes[$name]):$value"
  203.                     $found = $true
  204.                 } else {$_}
  205.             }
  206.             if (!$found) {
  207.                 $content += @("${name}:$($datatypes[$name]):$value")
  208.             }
  209.         }
  210.        
  211.         $content | Set-Content -Path $path
  212.         if ($passthru) {
  213.             Get-ChildItem -Path $path
  214.         }
  215.     }
  216. #}

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