【问题标题】:Getting Undefined DSC resource 'SqlSetup' even when the module is installed即使安装了模块,也获取未定义的 DSC 资源“SqlSetup”
【发布时间】:2021-11-21 10:04:26
【问题描述】:

我正在尝试通过 DSC 安装 SqlServer,但我一直遇到此错误

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'
+         ~~~~~~~~

我的 DSC 配置 sn-p 是这样的

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 

我很肯定 SqlServer 已经安装在远程服务器上。

  1. Get-DscResource -Module SqlServerDsc 返回资源列表
  2. 并且该文件夹存在于 C:\Program Files\WindowsPowerShell\Modules\SqlServerDsc

非常感谢任何反馈/建议。 非常感谢您的宝贵时间。

【问题讨论】:

  • 它可能在那里,但它可能无法使用。 Test-ModuleManifest -Path "C:\Program Files\WindowsPowerShell\Modules\SqlServerDsc\SqlServerDsc.psd1"(或适当的路径)报告什么?
  • @AlwaysLearning 它怎么可能不可用?太奇怪了。我运行了 Test-ModuleManifest,我注意到 ExportedCommand 是空的。 PS C:\Windows\system32> Test-ModuleManifest -Path "C:\Program Files\WindowsPowerShell\Modules\SqlServerDsc\15.2.0\SqlSer verDsc.psd1" ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Manifest 15.2.0 SqlServerDsc
  • Test-ModuleManifest 允许您检查格式错误的模块清单,例如不正确的声明或版本号不匹配,其中任何一个都可能产生“找不到模块 'SqlServerDsc'”错误消息。您也可以尝试使用 -Force 参数重新安装模块,以确保没有丢失文件和/或损坏的文件系统 ACL。
  • 感谢您的回复@AlwaysLearning!我通过在 Azure Devops 模块中添加模块来实现它

标签: sql-server powershell importerror dsc powershell-dsc


【解决方案1】:

看起来这个问题已经得到解答,我不确定我是否关注所有内容,但我通常会创建一个包装函数来复制资源。如下所示,带有用于指定路径、服务器、凭据的标志,然后是帮助程序,例如更改最大信封大小(如果你没有点击这个,你会的)并复制我的证书。下面不是全部,但至少可以确保您的模块可以复制。

{
    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

【讨论】:

    猜你喜欢
    • 2016-09-20
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多