'Calling a Function from a Script block in a DSC configuration
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 :)
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Adil B |