Question

To the DSC pros, this may seem like a very simple question but I couldn't find any resources on the web for this, or for any of the error messages I've seen. It seems very difficult to dig up any information on DSC so perhaps we can start here.

I am trying to build a Powershell DSC configuration for installing a scheduled task. I have found a sample resource on Steve Murawski's Github page for StackExchange resources, and I have copied the 'StackExchangeResources' tree to my DSC repository.

I imported the StackExchangeModule and attempted to create a very simple configuration using the ScheduledTask resource:

Import-Module StackExchangeResources

Configuration TempCleaner
{
    param($NodeName)

    Node $NodeName
    {
        $filePath = "C:\Tasks\TempCleaner.ps1";

        ScheduledTask
        {
            Name = "Clear Temporary Files"
            FilePath = $filePath
            Daily = $true
            FilePath = ""
            Hours = 4
            Minutes = 0
        }
    }
}

However, when I execute TempCleaner -Node TestNode, it doesn't actually do anything; no MOF files are written and no errors are thrown.

Now, a lot of examples I've seen involve giving a name to the invocation of the resource, something like this:

File TempCleaner
{
    DestinationPath = $filePath
    Contents = $(cat $tempCleanerScript | out-string)
    Checksum = "SHA-512"
}

But when I try to give it a name like so,

ScheduledTask CleanerTask
{
    Name = "Clear Temporary Files"
    FilePath = $filePath
    Daily = $true
    FilePath = ""
    Hours = 4
    Minutes = 0
}

it will throw an exception:

ScheduledTask : No MSFT_ScheduledTask objects found with property 'TaskName' equal to 'CleanerTask'. Verify the value of the property and retry. At C:\Users\Steve\Documents\DevOps\DSC\TempCleaner.ps1:13 char:9 + ScheduledTask CleanerTask + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (CleanerTask:String) [Get-ScheduledTask] , CimJobException + FullyQualifiedErrorId : CmdletizationQuery_NotFound_TaskName,Get-ScheduledTask

When I use the scheduled task resource in conjunction with the file resource as shown above, the file resource is written into the resulting MOF file but no other directives can be seen within.

There must be something I'm missing here. Is there some sort of verbose mode I can enable perhaps? Other logging options that aren't documented? That would be very helpful.

Was it helpful?

Solution

1) To use third party resource, you need to import it using Import-DscResource, not Import-Module.

Import-DscResource -Name StackExchange_ScheduledTask -ModuleName StackExchangeResources

Also, note that it has to be in the Configuration scope

2) Make sure the resource module you are using is deployed to C:\Program Files\WindowsPowerShell\Modules\ Place whole StackExchangeResources folder with it's contents (DSCResources etc.) there.

3) Resource name is mandatory

ScheduledTask task
{
#...
}

here's the configuration with fixes:

Configuration TempCleaner
{
    param($NodeName)

    Import-DscResource -Name StackExchange_ScheduledTask -ModuleName StackExchangeResources
    Node $NodeName
    {
        $filePath = "C:\test\TempCleaner.ps1";

        ScheduledTask task
        {
            Name = "Clear Temporary Files"
            FilePath = $filePath
            Daily = $true
            Hours = 4
            Minutes = 0
        }
    }
}

Hope it helps.

OTHER TIPS

If you are looking for an introduction to DSC, then I would suggest starting at:

Can't add comments yet, so editing my response. I think you may have duplicate keys in our resource.

Import-Module StackExchangeResources

Configuration TempCleaner
{
    param($NodeName)

    Node $NodeName
    {
        $filePath = "C:\Tasks\TempCleaner.ps1";

        ScheduledTask
        {
            Name = "Clear Temporary Files"
            FilePath = $filePath
            Daily = $true
            #FilePath = "" - Need unique keys. Also, FilePath is only a string not string[]
            Hours = 4
            Minutes = 0
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top