1

我正在尝试通过 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 配置片段如下所示

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

非常感谢任何反馈/建议。非常感谢您的参与。

4

1 回答 1

0

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

{
    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
于 2021-11-02T02:49:09.137 回答