Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScheduledTask: Unable to enter Debug Mode #231

Open
ShawnHardwick opened this issue Jul 9, 2019 · 7 comments
Open

ScheduledTask: Unable to enter Debug Mode #231

ShawnHardwick opened this issue Jul 9, 2019 · 7 comments
Labels
bug The issue is a bug. help wanted The issue is up for grabs for anyone in the community. needs investigation The issue needs to be investigated by the maintainers or/and the community.

Comments

@ShawnHardwick
Copy link
Contributor

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:
dsccommunity/SqlServerDsc#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
Copy link
Member

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?

@PlagueHO PlagueHO added bug The issue is a bug. help wanted The issue is up for grabs for anyone in the community. needs investigation The issue needs to be investigated by the maintainers or/and the community. labels Jul 10, 2019
@johlju
Copy link
Member

johlju commented Jul 10, 2019

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
Copy link
Member

johlju commented Jul 10, 2019

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
Copy link
Contributor Author

@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
Copy link
Member

johlju commented Jul 10, 2019

@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
Copy link
Contributor Author

ShawnHardwick commented Jul 10, 2019

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The issue is a bug. help wanted The issue is up for grabs for anyone in the community. needs investigation The issue needs to be investigated by the maintainers or/and the community.
Projects
None yet
Development

No branches or pull requests

3 participants