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.
This topic is 3 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
rslaav
Posts: 25
Last visit: Sat Oct 01, 2022 6:11 am
Has voted: 2 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 25
Last visit: Sat Oct 01, 2022 6:11 am
Has voted: 2 times

Re: Calling a function from within a dsc configuration issue

Post by rslaav »

but i can call a cmdlet no?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 25
Last visit: Sat Oct 01, 2022 6:11 am
Has voted: 2 times

Re: Calling a function from within a dsc configuration issue

Post by rslaav »

that's lame :)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 25
Last visit: Sat Oct 01, 2022 6:11 am
Has voted: 2 times

Re: Calling a function from within a dsc configuration issue

Post by rslaav »

thank you :)
This topic is 3 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked