5

我创建了一个 DSC 资源来从某个来源复制一个 Modules 目录。我正在测试它以在我的环境中进行更广泛的部署。该资源在确保所有文件都在那里并且它们与源内容匹配方面做得很好,到目前为止一切都很好......

问题是这样的;我想确保目标或目标中是否有任何其他文件,它们会被删除。

这是我的代码:

Configuration TestRun
{
  Param 
    (
      $ComputerName = 'Localhost'
    )
  Node $ComputerName
    {
      File LoadModules
        {
          Ensure = 'Present'
          Type = 'Directory'
          Force = $true
          Recurse = $true
          SourcePath = "C:\git\Modules"
          DestinationPath = 'C:\users\Jason\Documents\WindowsPowerShell\Modules'
          Checksum = "SHA-256"
          MatchSource = $true
        }
    }
}

在第一次运行配置后,我一直在目标目录中创建一个名为 Deleteme.flag 的文件来进行测试。到目前为止,我还没有任何运气让它真正被删除。

我尝试添加额外的文件提供程序要求以在目录运行之前删除它:

 File RemoveModules
    {
      Ensure = 'absent'
      Type = 'Directory'
      Force = $true
      Recurse = $true
      DestinationPath = 'C:\users\Jason\Documents\WindowsPowerShell\Modules'
    }

不幸的是,这失败并出现以下错误:

对于节点“Localhost”中资源“文件”的键“DestinationPath”,键属性组合“C:\users\Jason\Documents\WindowsPowerShell\Modules”重复。请确保节点中每个资源的关键属性都是唯一的。

无论如何,我想用文件资源来做这件事,但显然,用脚本提供者或其他一些自定义资源来做这件事会很容易。提前感谢您的所有帮助!

4

2 回答 2

3

我是 DSC 的新手。周日下午的大部分时间都在查看资源并试图找出解决这个问题的方法。所以,我真诚地感谢你。在 DSC 上查找很有趣。

我认为,这可以工作:

Configuration TestRun
{
  Param 
    (
      $ComputerName = 'Localhost'
    )
    Node $ComputerName
    {
        Script RemoveModules { 
            GetScript = {#needs to return hashtable.}
            SetScript = { 
                $ump = "$HOME" + "\Documents\WindowsPowerShell\Modules\"
                Remove-Item -Path $ump -Recurse -Force
            }
            TestScript = { 
                $ump = "$HOME" + "\Documents\WindowsPowerShell\Modules\"
                $mp = "C:\git\Modules"
                if((Compare-Object $(gci $mp) $(gci $ump))){
                    $false #at least one difference exists, SetScript will be called.
                }else{
                    $true #nothing is different
                }

            }
        }
        File LoadModules
        {
            Ensure = 'Present'
            Type = 'Directory'
            Force = $true
            Recurse = $true
            SourcePath = "C:\git\Modules"
            DestinationPath = 'C:\users\Jason\Documents\WindowsPowerShell\Modules'
            DependsOn = "[Script]RemoveModules"
            Checksum = "SHA-256"
            MatchSource = $true
        }
    }
}

参考:

于 2014-04-20T09:46:22.410 回答
1

您可以先复制文件,然后删除不需要的文件:

Configuration DirectoryCopy
{
    param
    (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String] $SourcePath,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [String] $DestinationPath
    )

    File CopyFiles
    {
        SourcePath      = $SourcePath
        DestinationPath = $DestinationPath
        Type            = 'Directory'
        Recurse         = $true
        Checksum        = 'SHA-256'  # Overwrite modified files
        Force           = $true
    }

    Script DeleteAdditionalDestinationFiles
    {
        TestScript =
        {
            $currentFiles = Get-ChildItem $using:DestinationPath -Recurse
            $desiredFiles = Get-ChildItem $using:SourcePath -Recurse

            $hasAdditionalFiles = [bool](Compare-Object -ReferenceObject $currentFiles -DifferenceObject $desiredFiles)

            return !$hasAdditionalFiles
        }
        SetScript =
        {
            $currentFiles = Get-ChildItem $using:DestinationPath -Recurse
            $desiredFiles = Get-ChildItem $using:SourcePath -Recurse

            $additionalFiles = Compare-Object -ReferenceObject $currentFiles -DifferenceObject $desiredFiles | Select-Object -ExpandProperty InputObject

            # First remove the directories and all its descendants.
            $additionalFiles | Where-Object { ($_.Attributes -band [IO.FileAttributes]::Directory) -eq [IO.FileAttributes]::Directory } | Remove-Item -Recurse -Verbose

            # Remove the remaining files that were not in any already removed directory.
            $additionalFiles | Where-Object { Test-Path -Path $_.FullName } | Remove-Item -Verbose
        }
        GetScript = {
            $currentFiles = Get-ChildItem $using:DestinationPath -Recurse

            return @{
                Result     = $currentFiles
                TestScript = $TestScript
                SetScript  = $SetScript
                GetScript  = $GetScript
            }
        }

        DependsOn = '[File]CopyFiles'
    }
}

这样,如果有任何目录树更改,则无需在复制所需文件之前删除所有文件。只有必须添加、修改或删除的文件才会受到影响。

于 2017-08-06T16:27:12.053 回答