DSC 推送模式 - 复制 DSC 资源的最佳方式

DSC Push mode - best way to copy DSC resources

我正在研究 DSC,想知道将 DSC 资源复制到目标主机的最佳方法是什么?

当我尝试将我的配置推送到目标主机时,它抱怨缺少 DSC 资源。

The PowerShell DSC resource xWebAdministration does not exist at the PowerShell module path nor is it registered as a WMI DSC resource.
    + CategoryInfo          : InvalidOperation: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : DscResourceNotFound
    + PSComputerName        : server1.appman.net

确保资源可用的最简单方法是设置一个基于文件共享的存储库以用于下拉模块。此博客应该可以帮助您 http://nanalakshmanan.com/blog/Push-Config-Pull-Module/

创建将安装模块的 DSC 配置,模块可以从网络共享中获取,或者更多可能可以从某些存储库中检出它们,例如 git 但当然如果他们可以访问它。推或拉什么更适合你。

在PS模块路径中找不到模块时出现错误。
使用以下行从 PSGallery 存储库 Install-Module -Name xWebAdministration

安装 xWebAdministration powershell 模块

然后点击"Yes to All"弹出窗口,模块安装完成
要交叉检查模块是否已安装,请键入 $env:PSModulePath 在 powershell 控制台中并在 PS 模块路径

中找到 xWebAdministration 文件夹

我尝试使用 DSC 安装 PS 个模块。它需要 3 个单独的配置:

Configuration InitialConfiguration
{
    Import-DscResource -ModuleName 'PSDesiredStateConfiguration'

    Node MyServer
    {
        Script InstallModule
        {
            SetScript = { Install-Module PackageManagement -MinimumVersion 1.1.7 -Force }
            TestScript = { $version = (Get-Module PackageManagement -ListAvailable).Version; $version.Major -ge 1 -and $version.Minor -ge 1 }
            GetScript = { Get-Module PackageManagement -ListAvailable }
        }
    }
}

Configuration ModulesConfiguration
{
    Import-DscResource -ModuleName 'PackageManagement' -ModuleVersion 1.1.7.0

    Node MyServer
    {
        PackageManagement xWebAdministration
        {
            Name = 'xWebAdministration'
        }
    }
}

Configuration WebServerConfiguration
{
    Import-DscResource –ModuleName 'xWebAdministration'

    Node MyServer
    {
        xWebAppPool SampleAppPool
        {
            Name = 'SampleAppPool'
        }
    }
}

但是,Microsoft 使用 simple script to install modules using WinRM in their example

# Commands for pushing DSC Resource Modules to Target Nodes.
# Resources you want to push must be available on this Authoring Machine.

#Required DSC resource modules
$moduleNames = "XWebAdministration", "xSMBShare", "cNtfsAccessControl", "OctopusDSC", "PSDSCResources", "DSCR_Font"

#ServerList to push files to
$Servers = "C:\temp\serverList.txt"
$serverList = (get-content $Servers |
    Where { $_ -notlike ";*" } | #lines that start with ';' will be considered comments
    ForEach { $_ } |
    select -Unique `
)

foreach ($server in $serverList)
{
    $Session = New-PSSession -ComputerName $server

    $getDSCResources = Invoke-Command -Session $Session -ScriptBlock {
        Get-DscResource
    }

    foreach ($module in $moduleNames)
    {
        if ($getDSCResources.moduleName -notcontains $module){
            #3. Copy module to remote node.
            $Module_params = @{
                Path = (Get-Module $module -ListAvailable).ModuleBase
                Destination = "$env:SystemDrive\Program Files\WindowsPowerShell\Modules$module"
                ToSession = $Session
                Force = $true
                Recurse = $true
                Verbose = $true

            }

            Copy-Item @Module_params
        }
    }
    Remove-PSSession -Id $Session.Id
}