Quantcast
Channel: desktop – Robin CM's IT Blog
Viewing all articles
Browse latest Browse all 5

PowerShell script to update RDMS server list

$
0
0

Remote Desktop Services in Windows Server 2012 is great in many ways, but it has some serious flaws. One of these is the pretty terrible so-called Remote Desktop Management Server concept. This is actually just some functionality built into Server Manager.

One of the most annoying things is that RDMS will fail to work if a server has been added to your Remote Desktop Deployment but you’ve not added it as a server to be managed via your own personal instance of Server Manager. This is quite a likely situation if you work in a team and/or have an automated server provisioning process. And it sucks. Instead of seeing your RD deployment, you just get a blank page with some text saying:

The following servers in this deployment are not part of the server pool:
SOMENEWRDSERVER.YOUR.FQDN
The servers must be added to the server pool.

No “Add now” button, no “continue anyway”, just a flat refusal to do anything at all.

So, Having recently done some work playing around with XML in PowerShell, and figuring that ServerManager probably stores its config in an XML file (though that wasn’t guaranteed – but luckily is true) I wrote a cmdlet that will update Server Manager to ensure that all the servers in your Remote Desktop deployment are added to Server Manager.

You’ll need to modify the code to specify your Connection Broker(s) (it copes with HA Connection Brokers). Save the code into a .psm1 file and then you can load this in your $profile script by using Import-Module <your-modules.psm1>.

function global:Update-RDMS {
    <#
        .Synopsis
        Ensure all RDS deployment servers are added to the Remote Desktop Management Server (Server Manager)

        .Description
        This cmdlet queries the Connection Broker and updates Server Manager to ensure all the RDS servers are added.
        Server Manager is killed if running, updated and restarted.

        .Example
        Update-RDMS
        Updates the RDMS

        .Notes
        RCM August 2015

        .Link
        http://www.rcmtech.co.uk/
    #>
    Begin{}
    Process{
        Write-Debug "Starting Update-RDMS"

        $ConnectionBrokers = "CBR01.rcmtech.co.uk","CBR02.rcmtech.co.uk"
        $ServerManagerXML = "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\ServerManager\Serverlist.xml"
        Write-Debug "Import RDS cmdlets"
        Import-Module RemoteDesktop
        Write-Debug "Find active Connection Broker"
        $ActiveManagementServer = $null
        foreach($Broker in $ConnectionBrokers){
            $ActiveManagementServer = (Get-ConnectionBrokerHighAvailability -ConnectionBroker $Broker).ActiveManagementServer
            if($ActiveManagementServer -eq $null){
                Write-Host "Unable to contact $Broker" -ForegroundColor Yellow
            } else {
                break
            }
        }
        if($ActiveManagementServer -eq $null){
            Write-Error "Unable to contact any Connection Broker"
        }else{
            if(Get-Process -Name ServerManager -ErrorAction SilentlyContinue){
                Write-Debug "Kill Server Manager"
                # Have to use tskill as stop-process gives an "Access Denied" with ServerManager
                Start-Process -FilePath "$env:systemroot\System32\tskill.exe" -ArgumentList "ServerManager"
            }
            Write-Debug "Get RD servers"
            $RDServers = Get-RDServer -ConnectionBroker $ActiveManagementServer
            Write-Debug "Get Server Manager XML"
            [XML]$SMXML = Get-Content -Path $ServerManagerXML
            foreach($RDServer in $RDServers){
                $Found = $false
                Write-Host ("Checking "+$RDServer.Server+" ") -NoNewline -ForegroundColor Gray
                foreach($Server in $SMXML.ServerList.ServerInfo){
                    if($RDServer.Server -eq $Server.name){
                        $Found = $true
                    }
                }
                if($Found -eq $true){
                    Write-Host "OK" -ForegroundColor Green
                }else{
                    Write-Host "Missing" -ForegroundColor Yellow
                    $NewServer = $SMXML.CreateElement("ServerInfo")
                    $SMXML.ServerList.AppendChild($NewServer) | Out-Null
                    $NewServer.SetAttribute("name",$RDServer.Server)
                    $NewServer.SetAttribute("status","1")
                    $NewServer.SetAttribute("lastUpdateTime",[string](Get-Date -Format s))
                    $NewServer.SetAttribute("locale","en-GB")
                }
            }
            # Remove xmlns attribute on any newly added servers, this is added automatically by PowerShell but causes Server Manager to reject the new server
            $SMXML = $SMXML.OuterXml.Replace(" xmlns=`"`"","")
            Write-Debug "Save XML file"
            $SMXML.Save($ServerManagerXML)
            Write-Debug "Start Server Manager"
            Start-Process -FilePath "$env:systemroot\System32\ServerManager.exe"
        }
    }
    End{}
}


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images