Getting Undefined DSC resource 'SqlSetup' even when the module is installed

892 Views Asked by At

I am trying to install SqlServer via DSC, but I keep hitting this error

Exception calling "NewScriptBlock" with "1" argument(s): "At line:10 char:5
+     Import-DscResource -ModuleName 'SqlServerDsc'
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Could not find the module 'SqlServerDsc'.

At line:438 char:9
+         SqlSetup 'InstallDefaultInstance'
+         ~~~~~~~~

My DSC configuration snippet looks like this

Configuration InstallSoftware
{
param
(
    $ComputerNames
)
Import-DSCResource -ModuleName PSDesiredStateConfiguration
Import-DSCResource -Module SqlServerDsc

Node $ComputerNames
{
    WindowsFeature 'NetFramework45'
    {
        Name   = 'NET-Framework-45-Core'
        Ensure = 'Present'
    }

    SqlSetup 'SqlInstall'
    {
        InstanceName        = 'localhost\mssql2019'
        Features            = 'SQLENGINE'
        Action              = 'Install';
        SourcePath          = 'C:\SQL2019'
        SQLSysAdminAccounts = @('Administrators')
        DependsOn           = '[WindowsFeature]NetFramework45'
    }  
}

InstallSoftware 

I am quite positive that the SqlServer has been installed in the remote server.

  1. Get-DscResource -Module SqlServerDsc returned the list of resources
  2. And the folder exists in C:\Program Files\WindowsPowerShell\Modules\SqlServerDsc

Any feedback/advice is highly appreciated. Thank you so much for your time.

1

There are 1 best solutions below

0
QuilleyPowers On

Looks like this was already answered, and I'm not sure I'm following everything, but I'll typically create a wrapper function for copying resources around. Something like below with flags for specifying path, server, credential, then helper stuff like changing max envelop size (if you haven't hit this, you will) and copying out my cert. Below isn't the whole thing, but it'd at least make sure your module would copy.

{
    Param (
        [Parameter(Mandatory=$true)]
        [string]$localpath,
        [Parameter(Mandatory=$true)]
        [string]$ServerName,
        [Parameter(Mandatory=$true)]
        [PsCredential]$PsAdmin,
        [Parameter(Mandatory=$true)]
        [boolean]$MaxEnvelope,
        [boolean]$CopyDscResources,
        [boolean]$RunDscTest,
        [boolean]$AddEncryptionCert
    )
    #Create Path if it doesnt exist
    if (!(test-path -path $localpath)) {
        New-Item -ItemType directory -Path $localpath
        }
        if ($MaxEnvelope -eq $true)
        {
            $so = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
            $session = New-PSSession -ComputerName $ServerName -Credential $PsAdmin -SessionOption $so 
            Invoke-Command -Session $session -ScriptBlock {
                Set-Item -Path WSMan:\localhost\MaxEnvelopeSizeKb -Value 3072
            }
        }
        
        if ($CopyDscResources -eq $true)
        {  
            $so = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
            $session = New-PSSession -ComputerName $ServerName -Credential $PsAdmin -SessionOption $so 
            
            $paramsDefender = @{
                Path = 'C:\Program Files\WindowsPowerShell\Modules\SqlServerDsc'
                Destination = 'C:\Program Files\WindowsPowerShell\Modules'
                ToSession = $session
            }
            Copy-Item @paramsDefender -Recurse -Force
            
        }

        $Session2 = New-CimSession -ComputerName $ServerName.ToString() -Credential $PsAdmin
        Set-DscLocalConfigurationManager -Path $localpath -CimSession $Session2 -Force
        Start-DSCConfiguration -Path $localpath -Cimsession $Session2 -Wait -Force -ErrorAction Stop -Verbose
        if ($RunDscTest)
        {
            Test-DSCConfiguration -Path $localpath -Cimsession $Session2 -Verbose | Format-Table -AutoSize
        }
       
        
}

#Example run
RunRemoteDsc $localpath 'Some-Server' $PsLocalAdmin $false $true $false