在 DSC 中使用文件提供程序 - 确保目标仅包含来自源的文件 [英] Using the File provider in DSC - Ensure Destination only contains files from Source

查看:47
本文介绍了在 DSC 中使用文件提供程序 - 确保目标仅包含来自源的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a DSC resource to copy a Modules directory from a certain source. I'm testing it for a broader deployment in my environment. The resource does a great job ensuring all the files are there and that they match the source content, so far so good...

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

The problem is this; I want to ensure that if there are any additional files in the target, or destination, a folder that they get removed.

这是我的代码:

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 的配置后,我一直在通过在目标目录中创建一个文件来进行测试.到目前为止,我还没有幸运地将其删除.

I've been testing by creating a file in the destination directory after running the config the first time called Deleteme.flag. So far I haven't had any luck getting it actually to be deleted.

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

I tried adding an additional File provider requirement to remove the directory before it runs:

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

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

Unfortunately, this fails with the following error:

键属性组合'C:\users\Jason\Documents\WindowsPowerShell\Modules' 被复制节点Localhost"中资源File"的键DestinationPath".请确保节点中每个资源的关键属性都是唯一的.

The key properties combination 'C:\users\Jason\Documents\WindowsPowerShell\Modules' is duplicated for keys 'DestinationPath' of resource 'File' in node 'Localhost'. Please make sure key properties are unique for each resource in a node.

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

Anyway, I'd like to do it with the file resource, but obviously, it would be easy to do it with the script provider or some other custom resource. Thanks in advance for all your help!

推荐答案

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

I am new to DSC. Spent the best part of Sunday afternoon looking at the resources and trying to figure out how to solve this. So, I sincerely thank you for that. It was fun looking up on 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
        }
    }
}

参考:

这篇关于在 DSC 中使用文件提供程序 - 确保目标仅包含来自源的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆