dsccommunity / xRemoteDesktopSessionHost

This module contains DSC resources for the management and configuration of Microsoft Remote Desktop Session Host (RDSH).
MIT License
36 stars 47 forks source link

xRDSessionDeployment: You cannot call a method on a null-valued expression #47

Closed peppekerstens closed 5 years ago

peppekerstens commented 6 years ago

System info (W2016 Azure VM)

$psversiontable

Name                           Value
----                           -----
PSVersion                      5.1.14393.2312
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.2312
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

 [environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      14393  0

Issue

When calling the 'xRDSessionDeployment' in a 'clean machine', it fails with:

You cannot call a method on a null-valued expression.
At line:80 char:5
+     (Get-TargetResource @PSBoundParameters).SessionHost.ToLower() -ie ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

(above info is from loading the resource as function and calling them direct)

The whole thing makes perfect sense, when calling the Get-TargetResource you'll get:

PS C:\Users\devadmin> Get-TargetResource -SessionHost "dev-rdg1.develop.local" -ConnectionBroker "dev-rdb1.develop.local" -WebAccessServer "dev-rdg1.develop.local"

Name                           Value                                                                                                                   
----                           -----                                                                                                                   
WebAccessServer                                                                                                                                        
SessionHost                                                                                                                                            
ConnectionBroker                         

...and indeed; you cannot do a .ToLower() on an empty value...

Looking back in the releases; this code changed since v1.5.0.0 and this bug seems to be there since then.....(ouch?!)

Solution (proposal)

I do not expect this to be fixed as this code already changed/improved in dev branch.. So I also checked that code. This too contains a bug:

PS C:\Users\devadmin> Test-TargetResource -SessionHost "dev-rdg1.develop.local" -ConnectionBroker "dev-rdb1.develop.local" -WebAccessServer "dev-rdg1.develop.local"
Get-Service : Cannot find any service with service name 'RDMS'.
At line:26 char:9
+     if((Get-Service -Name RDMS | Select-Object -ExpandProperty Status ...
+         ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (RDMS:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand

WARNING: Failed to start RDMS service. Error: Cannot find any service with service name 'RDMS'.
False

The easy fix would be:

    # Start service RDMS is needed because otherwise a reboot loop could happen due to
    # the RDMS Service being on Delay-Start by default, and DSC kicks in too quickly after a reboot.
    if((Get-Service -Name RDMS -ErrorAction SilentlyContinue| Select-Object -ExpandProperty Status) -ne 'Running') 
    {
        try
        {
            Start-Service -Name RDMS -ErrorAction Stop
        }
        catch
        {
            Write-Warning "Failed to start RDMS service. Error: $_"
        }
    }

But I am unsure why simpler code of earlier versions has expanded to so elaborate one as above... probably the provide better feedback?

Finally

Kind request; as this resource item is main/core functionality of this DSC module and does not seem to work (you cannot not deploy an RDS environment with v1.5.0.0 or above); please push the next release asap..

johlju commented 6 years ago

Thanks for reporting this! It seems there is not a unit test for when the there is not 'RDMS' service available. Only test seems to be when the service is stopped or running. 🤔

https://github.com/PowerShell/xRemoteDesktopSessionHost/blob/df7c4d96c27adae1b72eaa13e07529c40addb1d9/tests/unit/MSFT_xRDSessionDeployment.tests.ps1#L77-L80

peppekerstens commented 6 years ago

sorry for late reaction, holiday. will make an effort though