1

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:

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:

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.

Any help would be appreciated, thank you :)

RSLAV
  • 53
  • 6
  • Does the `Import-Module` cmdlet not throw any errors? – stackprotector Jun 16 '20 at 09:27
  • no, none at all :) – RSLAV Jun 16 '20 at 14:50
  • I think that the problem you are having relates to DSC script resource executing the script in a scriptblock thrown into an Invoke-Command, you can see how it executes by checking the psm1 at C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration\DSCResources\MSFT_ScriptResource on your machine. You can also check this issue, as it is somewhat same problem: https://stackoverflow.com/questions/14441800/how-to-import-custom-powershell-module-into-the-remote-session – Setorica Jun 18 '20 at 10:00

1 Answers1

0

An important thing to remember is that DSC is not Powershell - you can run Powershell commands or scripts via a Script resource, but any command called within a Script resource isn't aware of its script root directory or path.

If you have your run-myfunction function exposed in a .ps1 file, I've found this sort of syntax to work well, though your run-myfunction code will need to use absolute paths for any other resources or modules it references in order to find them reliably:

Script Run-MyFunction {
    SetScript = {
                & "C:\path\to\script\RunMyFunction.ps1" -ParamName "paramValue"
    }
    ...
}

The standard way to do this would be to create a DSC resource for run-myfunction and call it as part of the DSC configuration for your node.

Adil B
  • 14,635
  • 11
  • 60
  • 78