Using Invoke-Command using Credentials to run Get-RDRemoteApp fails

Benjamin Peikes 16 Reputation points
2022-04-10T03:28:18.867+00:00

I have a script which I can run locally on our ConnectionBroker, though the session has to be elevated, which manages our RDRemoteApp collections. We're trying to automate processes and remote the requirement of having someone actually log onto the ConnectionBroker to run scripts manually. To do so, we are trying to use Invoke-Command to run the script remotely.
Even though we pass credentials for a user that is in domain administrator group, the following script gives us an error when run remotely:

$server = "ConnectionBrokerName"
$collectionName ="OurCollection"

$password = ConvertTo-SecureString "password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("DOMAIN\domain_admin", $password)

$sb =
{
Import-Module RemoteDesktop
Import-Module RemoteDesktopServices
Get-RDRemoteApp -CollectionName $Using:$collectionName
}

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

This will return an error like:
A Remote Desktop Services deployment does not exist on ConnectionBrokerName.DOMAIN.COM. This operation can be performed after creating a deployment. For information about creating a
deployment, run "Get-Help New-RDVirtualDesktopDeployment" or "Get-Help New-RDSessionDeployment".

  • CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
  • FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Get-RDRemoteApp
  • PSComputerName : ConnectionBrokerName
Remote Desktop
Remote Desktop
A Microsoft app that connects remotely to computers and to virtual apps and desktops.
4,381 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,449 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,671 Reputation points
    2022-04-10T14:59:05.253+00:00

    The value of $collectionName needs to be passed as a parameter to the script block.

    Try using (no pun intended) something like this:

    $sb =
    {
        Import-Module RemoteDesktop
        Import-Module RemoteDesktopServices
        Get-RDRemoteApp -CollectionName $USING:collectionName
    }
    

  2. Rich Matheisen 45,671 Reputation points
    2022-04-28T21:28:15.653+00:00

    Are you use a high-availabilty setup?

    (Get-RDConnectionBrokerHighAvailability -ConnectionBroker).ActiveManagementServer
    

    Does this work? And, if it does, do you get the name of the server you expect?