Calling a function from within a dsc configuration issue

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
rslaav
Posts: 14
Joined: Mon Jun 08, 2020 2:01 pm

Calling a function from within a dsc configuration issue

Post by rslaav »

I have an issue with my dsc config, on windows 10. I had to define a script block as I couldn't install WindowsIIS with dsc considering it wants a server sku. The following code is an example of a way to bypass it (sorta), but I can't call my modules or functions from the script block for some reason. Have a look:

Code: Select all

Configuration update-settings
{
    $hostname = $env:COMPUTERNAME

    Node $hostname
    {
        Script whatever
        {
            GetScript = { return @{'Result' = 'something'} }
            TestScript = { return $false }

            # problem is here:
            SetScript = { run-myfunction -args something }
        }
    }
}
I have a psm1 file elsewhere, and even if I do a Import-Module C:\MyFolder\PSModules\run-myfunctions.psm1 -force in my dsc, it still gives me the following errors:

Code: Select all

PowerShell DSC resource MSFT_ScriptResource  failed to execute Set-TargetResource functionality with error message: The term 'run-myfunction' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a 
path was included, verify that the path is correct and try again.
    + CategoryInfo          : InvalidOperation: (:) [], CimException
    + FullyQualifiedErrorId : ProviderOperationExecutionFailure
    + PSComputerName        : DESKTOPofMe

The SendConfigurationApply function did not succeed.
    + CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : MI RESULT 1
    + PSComputerName        : DESKTOPofMe
Just an fyi, I did run and export my powershell modules correctly, and can access them on the commandline with run-myfunction or run-myfunction2, etc.

Are we essentially unable to refer to any external functions from the script blocks in a dsc? if so, how would I do this without having to copy and paste a bunch of repeated code?

jvierra
Posts: 14452
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Calling a function from within a dsc configuration issue

Post by jvierra »

You cannot refer to external functions o modules in a DSC. A DSC is run under the system ConfigMgr process and not in any user session.

rslaav
Posts: 14
Joined: Mon Jun 08, 2020 2:01 pm

Re: Calling a function from within a dsc configuration issue

Post by rslaav »

but i can call a cmdlet no?

jvierra
Posts: 14452
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Calling a function from within a dsc configuration issue

Post by jvierra »

You can call a CmdLet that is PS core. Any added functions will not be available unless you include them in teh DSC code.

Review the full documentation on DSC to see examples of how to work with external code.

rslaav
Posts: 14
Joined: Mon Jun 08, 2020 2:01 pm

Re: Calling a function from within a dsc configuration issue

Post by rslaav »

that's lame :)

jvierra
Posts: 14452
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Calling a function from within a dsc configuration issue

Post by jvierra »

Some CmdLets won't work in DC o you will have to test.

jvierra
Posts: 14452
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Calling a function from within a dsc configuration issue

Post by jvierra »

Perhaps the best way to point you is to tell you that DSC is not PowerShell. It is WMI/CIM classes and instances that are used to create, enforce or remove a configuration. DSC is not PowerShell. PS just supplies basic syntax for generating MOF files that DSC and ConfigMgr use to do their magic. Also DSC is what we call a "declarative" language. It is designed to define an end state. It is not a procedural syntax.

MS has a number of excellent documents that will help you to understand what DSC is and how it is designed to work. A good understanding of configuration management is required as a starting point. What CM is, why we use it and how we use it are a first step.

https://docs.microsoft.com/en-us/powers ... wershell-7

This document will get you started and answer most of your questions.

https://docs.microsoft.com/en-us/powers ... wershell-7

rslaav
Posts: 14
Joined: Mon Jun 08, 2020 2:01 pm

Re: Calling a function from within a dsc configuration issue

Post by rslaav »

thank you :)

Anayazius
Posts: 6
Joined: Tue Jun 23, 2020 5:04 am

Re: Calling a function from within a dsc configuration issue

Post by Anayazius »

jvierra wrote:
Tue Jun 16, 2020 12:38 pm
Perhaps the best way to point you is to tell you that DSC is not PowerShell. It is WMI/CIM classes and instances that are used to create, enforce or remove a configuration. DSC is not PowerShell. PS just supplies basic syntax for generating MOF files that DSC and ConfigMgr use to do their magic. Also DSC is what we call a "declarative" language. It is designed to define an end state. It is not a procedural syntax.

MS has a number of excellent documents that will help you to understand what DSC is and how it is designed to work. A good understanding of configuration management is required as a starting point. What CM is, why we use it and how we use it are a first step.

https://edubirdie.com/do-my-math-homework

This document will get you started and answer most of your questions.

https://docs.microsoft.com/en-us/powers ... wershell-7
Thank you so much for sharing. It's very helpful to me.

Post Reply