dsccommunity / ComputerManagementDsc

DSC resources for for configuration of a Windows computer. These DSC resources allow you to perform computer management tasks, such as renaming the computer, joining a domain and scheduling tasks as well as configuring items such as virtual memory, event logs, time zones and power settings.
https://dsccommunity.org
MIT License
306 stars 83 forks source link

ScheduledTask: Unable to enter Debug Mode #231

Open ShawnHardwick opened 5 years ago

ShawnHardwick commented 5 years ago

Details of the scenario you tried and the problem that is occurring

Enabling DSC debug with Enable-DSCDebug -BreakAll and then running the ScheduledTask resource returns an error:

PowerShell DSC resource MSFT_ScheduledTask failed to execute Test-TargetResource functionality with error message: Object reference not set to an instance of an object.

Verbose logs showing the problem

VERBOSE: Perform operation 'Invoke CimMethod' with following parameters, ''methodName' = Resourcetest,'className' = MSFT_DSCLocalConfigurationManager,'namespaceName'
 = root/Microsoft/Windows/DesiredStateConfiguration'.
VERBOSE: An LCM method call arrived from computer REDACTED with user sid SID REDACTED.
WARNING: [REDACTED]:                            [DSCEngine] Warning LCM is in Debug 'ResourceScriptBreakAll' mode.  Resource script processing will be stopped to wait for PowerShell script debugger to attach.
VERBOSE: [REDACTED]:                            [DSCEngine] Importing the module C:\ProgramData\PuppetLabs\puppet\cache\lib\puppet_x\dsc_resources\ComputerManagementDsc\DscResources\MSFT_ScheduledTask\MSFT_ScheduledTask.psm1 in force mode.
DEBUG: [REDACTED]:                            [DSCEngine] 
namespace ScheduledTask
{
    public enum DaysOfWeek
    {
        Sunday = 1,
        Monday = 2,
        Tuesday = 4,
        Wednesday = 8,
        Thursday = 16,
        Friday = 32,
        Saturday = 64
    }
}
VERBOSE: [REDACTED]: LCM:  [ Start  Test     ]  [[ScheduledTask]DirectResourceAccess]
VERBOSE: [REDACTED]:                            [[ScheduledTask]DirectResourceAccess] Importing the module MSFT_ScheduledTask in force mode.
WARNING: [REDACTED]:                            [[ScheduledTask]DirectResourceAccess] Resource is waiting for PowerShell script debugger to attach.  Use the following commands to begin debugging this resource script:
Enter-PSSession -ComputerName REDACTED -Credential <credentials>
Enter-PSHostProcess -Id 10792 -AppDomainName DscPsPluginWkr_AppDomain
Debug-Runspace -Id 2
VERBOSE: [REDACTED]: LCM:  [ End    Test     ]  [[ScheduledTask]DirectResourceAccess] False in 0.5150 seconds.PowerShell DSC resource MSFT_ScheduledTask  failed to execute Test-TargetResource functionality with error message: Object reference not set to an instance of an object. 
    + CategoryInfo          : InvalidOperation: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : ProviderOperationExecutionFailure
    + PSComputerName        : localhost

VERBOSE: Operation 'Invoke CimMethod' complete.
VERBOSE: Time taken for configuration job to complete is 1.603 seconds

Suggested solution to the issue

See the below issue for the same problem in a different DSC module: https://github.com/PowerShell/SqlServerDsc/issues/448

The DSC configuration that is used to reproduce the issue (as detailed as possible)

No configuration to provide

The operating system the target node is running

OsName               : Microsoft Windows Server 2016 Standard
OsOperatingSystemSKU : StandardServerEdition
OsArchitecture       : 64-bit
WindowsBuildLabEx    : 14393.3053.amd64fre.rs1_release_inmarket.190612-1836
OsLanguage           : en-US
OsMuiLanguages       : {en-US}

Version and build of PowerShell the target node is running

PSVersion                      5.1.14393.3053                                                                                                                       
PSEdition                      Desktop                                                                                                                              
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                              
BuildVersion                   10.0.14393.3053                                                                                                                      
CLRVersion                     4.0.30319.42000                                                                                                                      
WSManStackVersion              3.0                                                                                                                                  
PSRemotingProtocolVersion      2.3                                                                                                                                  
SerializationVersion           1.1.0.1   

Version of the DSC module that was used ('dev' if using current dev branch)

6.4.0.0

PlagueHO commented 5 years ago

Thanks for raising this @ShawnHardwick.

Are you able to share the DSC Config you're using (with specifics redacted)? @johlju - do you recall what the exact cause of this problem was over in SqlServerDsc?

johlju commented 5 years ago

In the SqlServerDsc case it was a parameter that set a default value from another parameter

param
(
    [Parameter(Mandatory = $true)]
    [System.String]
    $InstanceName

    [Parameter()]
    [System.String]
    $FailoverClusterGroupName = "SQL Server ($InstanceName)"
)

It failed on $FailoverClusterGroupName = "SQL Server ($InstanceName)".

johlju commented 5 years ago

Maybe it fails on this?

https://github.com/PowerShell/ComputerManagementDsc/blob/0b394479d03f2594476b123c3762b7ebee03334b/DSCResources/MSFT_ScheduledTask/MSFT_ScheduledTask.psm1#L787

The solution was to set the default value as the first thing in the code, instead of setting it in the param block.

ShawnHardwick commented 5 years ago

@PlagueHO

I actually use Puppet to call DSC so I'll provide what Puppet calls and what I call directly to debug the code.

$invokeParams = @{
Name = 'ScheduledTask'
Method = 'test'
Property = @{
taskname = 'OneDrive Mapper'
actionexecutable = 'C:\OneDrive_Mapper\OneDrive_Mapper.exe'
scheduletype = 'AtLogOn'
enable = $true
delay = '00:00:30'
}

$result = Invoke-DscResource @invokeParams
johlju commented 5 years ago

@ShawnHardwick to just rule this out. In these locations can you remove the = [System.DateTime]::Today from all three locations in the resource code on your node.

https://github.com/PowerShell/ComputerManagementDsc/blob/0b394479d03f2594476b123c3762b7ebee03334b/DSCResources/MSFT_ScheduledTask/MSFT_ScheduledTask.psm1#L262

https://github.com/PowerShell/ComputerManagementDsc/blob/0b394479d03f2594476b123c3762b7ebee03334b/DSCResources/MSFT_ScheduledTask/MSFT_ScheduledTask.psm1#L787

https://github.com/PowerShell/ComputerManagementDsc/blob/0b394479d03f2594476b123c3762b7ebee03334b/DSCResources/MSFT_ScheduledTask/MSFT_ScheduledTask.psm1#L1637

Than in the start of each function Get-TargetResource, Test-TargetResource and Set-TargetResource please add these rows. Alternative to adding the below rows, you could add the StartTime parameter with a valid value to the properties that you call Puppet with.

if (-not $PSBoundParameters.ContainsKey('StartTime'))
{
    $StartTime = [System.DateTime]::Today
}

This is the only thing I can see that is close to the problem in SqlServerDsc, it would be nice to rule it out.

ShawnHardwick commented 5 years ago

That appears to have fixed it. I can use Enable-DSCDebug -BreakAll to open a debugger in DSC now with the ScheduledTask resource.

It's interesting to know now that the DSC debug fails if the default value for a parameter needs to be derived from a function or another parameter.

Just curious, if this has been broken for this resource, how do other people step through DSC code?

PlagueHO commented 5 years ago

Thanks @johlju - great info. I'll look at incorporating changes into the resource over the weekend.

@ShawnHardwick , in answer to your question, I very rarely use DSC debugging, generally relying on unit tests/integration tests. I try to use a red-green refactor (TDD) approach when writing resources. This doesn't eliminate the need for debugging, but does make it a rare enough occurrence.

I'm not completely sure why the conditions @johlju identified would have caused problems debugging DSC, but I'll look into it a bit more. Might also be something we can test for as well.