none
Powershell Script to change Allow New Connections from True to False RRS feed

  • Question

  • Good morning all,

     I was hoping someone may be able to help me, I have two host servers in a collection (RDSH01 and RDSH02)

     Every day I have to manually Allow New Connections on one and disable new connections for the other, is anyone aware of a Powershell Script which could do this for me?

    Many thanks,

     

    Tuesday, August 22, 2017 7:56 AM

Answers

  • Hi,

    To change allow new connections you would use Set-RDSessionHost, similar to below, in an admin powershell prompt:

     
    Import-Module RemoteDesktop
    Set-RDSessionHost -SessionHost "rdsh01.domain.com" -NewConnectionAllowed No -ConnectionBroker "broker.domain.com"
    Set-RDSessionHost -SessionHost "rdsh02.domain.com" -NewConnectionAllowed Yes -ConnectionBroker "broker.domain.com"
     


    If you wanted to you could read the current values using Get-RDSessionHost and then set them to opposite.

    -TP


    Tuesday, August 22, 2017 9:28 PM
    Moderator

All replies

  • Hi,

    Please check whether these links below are useful:

    Change logon

    https://technet.microsoft.com/en-us/library/cc753586(v=ws.11).aspx

    Remote Desktop Services “Drain mode” PowerShell script

    https://fixingitpro.com/2014/12/10/remote-desktop-services-drain-mode-powershell-script/

    Remote Desktop Session Hosts and Drain Mode Script

    http://purerds.org/remote-desktop-scripts/remote-desktop-session-hosts-drain-mode-script/

    Please Note: Since web sites above are not hosted by Microsoft, links may change without notice. Microsoft does not guarantee the accuracy of this information.

    Best Regards,

    Amy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, August 22, 2017 10:21 AM
    Moderator
  • Hi,

    To change allow new connections you would use Set-RDSessionHost, similar to below, in an admin powershell prompt:

     
    Import-Module RemoteDesktop
    Set-RDSessionHost -SessionHost "rdsh01.domain.com" -NewConnectionAllowed No -ConnectionBroker "broker.domain.com"
    Set-RDSessionHost -SessionHost "rdsh02.domain.com" -NewConnectionAllowed Yes -ConnectionBroker "broker.domain.com"
     


    If you wanted to you could read the current values using Get-RDSessionHost and then set them to opposite.

    -TP


    Tuesday, August 22, 2017 9:28 PM
    Moderator
  • Thanks TP, that looks promising.

    I have given it a try but unfortunately I ran into a problem. When I entered Set-RDSessionHost -SessionHost "rdsh01.hospice.local" I received a prompt to change the value for NewConnectionAllowed (please see the screenshot below)

    Initially I entered $True but this produced an error which said "Specify one of the following enumerator names and try again: Yes, NotUntilReboot, No" which seems strange considering on Server Manager the values are either True or False...

    I tried again but entered Yes instead of $True when prompted and it seemed to process the command. However when I went to Server Manager Allow New Connections for RDSH01 were still set to False.

    So at the moment it's a bit of a headscratcher...! Any help is greatly appreciated.

    Wednesday, August 23, 2017 8:32 AM
  • Thanks Amy, I will take a look at these and get back to you shortly.
    Wednesday, August 23, 2017 8:33 AM
  • Hi,

    I edited commands above and fixed my mistake.

    In order to see the change in Server Manager, you need to completely close and re-open Server Manager.  If you want to see the change immediately you can use Get-RDSessionHost cmdlet, like this:

    Get-RDSessionHost -CollectionName "YourCollectionName"

    -TP

    Wednesday, August 23, 2017 9:09 AM
    Moderator
  • Thanks that's worked a treat. All working now!

    • Proposed as answer by vergilis Saturday, April 6, 2019 4:29 AM
    • Unproposed as answer by TP []MVP, Moderator Saturday, April 6, 2019 4:38 AM
    Wednesday, August 23, 2017 11:22 AM
  • Hi,

    i cant seem to be able to run this command on any other computer except locally on the current active broker...

    i tired with domain admin + run as admin (both ISE and Shell), i keep getting this error:

    Set-RDSessionHost -SessionHost "VTS16-TEST.domain.my" -NewConnectionAllowed No -ConnectionBroker "Broker02.domain.my"
    New-PSSession : [localhost] Connecting to remote server localhost failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the se
    rvice on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destin
    ation is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting 
    Help topic.
    At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\RemoteDesktop\SessionDesktopCollection.psm1:217 char:32
    + ... owSession = New-PSSession -ConfigurationName Microsoft.Windows.Server ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
        + FullyQualifiedErrorId : CannotConnect,PSSessionOpenFailed
    Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
    At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\RemoteDesktop\SessionDesktopCollection.psm1:219 char:37
    +             Invoke-Command -Session $workflowSession -ArgumentList @( ...
    +                                     ~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

    any help would be great :)

    thanks,

    Sean

    Sunday, September 24, 2017 6:51 AM