'Configuring Remote Desktop Services remotely via powershell

Trying to manage our Remote Desktop Services installation using PowerShell and we're running into an issue where the commands in the RemoteDeskop module and the RemoteDesktopServices module do not appear to work when being run via Invoke-Command. Basically it appears that these functions do not work when run in a remote session.

The script below gets the following error:

The RD Connection Broker server is not available. Verify that you can connect to the RD Connection Broker server. + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-RDServer + PSComputerName : AWSELABSDevX13.LABSDEV.com

$server = "OUR_SERVER"
$connection_broker = "OUR_SERVER"
$collectionName ="COLLECTION"


$admin_user = "FULLY_QUALIFIED_DOMAIN_USER"
$password = "PASSWORD"

$password_sec = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($admin_user, $password_sec)

$sb =
{
    function Test-IsAdmin {
         ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
     }
    
    if (Test-IsAdmin) {
        ""
        "You are running with Administrator access."
        ""
    } else {
        ""
        "You do not have admin access."
        ""
    }
    whoami /priv
    Import-Module RemoteDesktop
    Import-Module RemoteDesktopServices

    Get-RDServer
}


Invoke-Command -Credential $cred -ComputerName $connection_broker -ScriptBlock $sb


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source