1

I am attempting to automate a full Remote Desktop Session deployment but running into a problem adding the gateway via powershell. I can add each server with the exception of the gateway. Which produces the error:

Unable to create a Remote Desktop resource authorization policy on <computer name>. The error is
Object reference not set to an instance of an object.. Please check the eventlog on RD Gateway server for more info.

I see nothing in any of the logs that would help me troubleshoot this.

If I go to the server manager and add it via this UI: enter image description here

Everything works as expected. Are there an additional steps required to make this work?

Here's the script that's currently running (EC2, userdata):

<powershell>
try {
    $SecretAD = "SuperSecretSauceSuzzySaid"
    $SecretObj = (Get-SECSecretValue -SecretId $SecretAD)
}catch {
    Write-Output 'Failed to get Secret'
    return
}
$sys = Get-WmiObject -Class Win32_ComputerSystem

if ( -Not ($sys.PartOfDomain))
{
    Write-Output "Not A domain member yet. Joining..."
    [PSCustomObject]$Secret = ($SecretObj.SecretString  | ConvertFrom-Json)
    $password = $Secret.password | ConvertTo-SecureString -asPlainText -Force
    $username = $Secret.username
    $credential = New-Object System.Management.Automation.PSCredential($username, $password)
    Write-Output "Starting Domain Join and Restarting"
    Add-Computer -DomainName "6565.fake.com" -Credential $credential -Restart -Force
    Write-Output "Initiating restart..."
}


Try {
    $process = (Get-NetAdapterBinding -InterfaceAlias "Ethernet" -ComponentID ms_tcpip6).Enabled
    If ($process.toString() -ne "False"){
        Get-NetAdapterBinding –InterfaceAlias "Ethernet"
        Disable-NetAdapterBinding –InterfaceAlias "Ethernet" –ComponentID ms_tcpip6
        Write-Output "Disabled ipv6"
        Import-Module RemoteDesktop
        Add-WindowsFeature –Name RDS-RD-Server –IncludeAllSubFeature
        Write-Output "Restarting..."
        Restart-Computer -Force
    }
    Start-Sleep 200
    $installed = (Get-WindowsFeature RDS-RD-Server).Installed

    If ($installed.toString() -eq "True"){
        $broker = (Get-WindowsFeature RDS-Connection-Broker).Installed
        If ($broker.toString() -eq "False"){
            $ErrorActionPreference = 'SilentlyContinue'
            Write-Output "Installing the new session deployment"
            New-RDSessionDeployment -ConnectionBroker "$env:COMPUTERNAME.6565.fake.com" -SessionHost "$env:COMPUTERNAME.6565.fake.com"
            Write-Output "Restarting..."
            Restart-Computer -Force
        }
    }
    Start-Sleep -Seconds 100
    New-RDSessionCollection  -CollectionName QuickSessionCollection -SessionHost "$env:COMPUTERNAME.6565.fake.com"  -CollectionDescription "POR Stuff"  -ConnectionBroker "$env:COMPUTERNAME.6565.fake.com"
    Write-Output "Installing Gateway with managnent tools"
    Add-WindowsFeature -Name RDS-Gateway -IncludeManagementTools -ComputerName $env:COMPUTERNAME
    Start-Sleep -Seconds 100
    Add-RDServer -Server "$env:COMPUTERNAME.6565.fake.com" -Role "RDS-GATEWAY" -ConnectionBroker "$env:COMPUTERNAME.6565.fake.com" -GatewayExternalFqdn "6565.fake.com"
    Add-RDServer -Server "$env:COMPUTERNAME.6565.fake.com" -Role "RDS-LICENSING" -ConnectionBroker "$env:COMPUTERNAME.6565.fake.com"
} Catch [System.Exception] {
    Write-Output "Failed"
    Write-Output "Failed to install RD Gateway components $_"
    Exit 0
}
</powershell>
<persist>true</persist>
Kisaragi
  • 103
  • 1
  • 1
  • 8

1 Answers1

0

I would guess that no computer exists with name of your gateway. I guess that after $sys = Get-WmiObject -Class Win32_ComputerSystem you get a null $sys.

Try to debug your script with tools like powershell ide.

Henry Aloni
  • 147
  • 8