PowerShell DSC - 如何将配置参数传递给 ScriptResources? [英] PowerShell DSC - how to pass configuration parameters to ScriptResources?

查看:57
本文介绍了PowerShell DSC - 如何将配置参数传递给 ScriptResources?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用 PowerShell 所需状态配置脚本来配置内部应用程序时遇到了很多麻烦.问题的根源在于我似乎无法将我的配置数据传递给 ScriptResource(至少不是我尝试这样做的方式).

I'm having a lot of trouble trying to get a PowerShell Desired State Configuration script working to configure an in-house application. The root of the problem is that I can't seem to pass my configuration data down into a ScriptResource (at least not with the way I'm trying to do it).

我的脚本应该为我们的内部应用程序创建一个配置文件夹,然后将一些设置写入文件:

My script is supposed to create a config folder for our in-house application, and then write some settings into a file:

configuration MyApp {
    param (
        [string[]] $ComputerName = $env:ComputerName
    )
    node $ComputerName {

        File ConfigurationFolder {
            Type = "Directory"
            DestinationPath = $Node.ConfigFolder
            Ensure = "Present"
        }

        Script ConfigurationFile {
            SetScript = {
                write-verbose "running ConfigurationFile.SetScript";
                write-verbose "folder = $($Node.ConfigFolder)";
                write-verbose "filename = $($Node.ConfigFile)";
                [System.IO.File]::WriteAllText($Node.ConfigFile, "enabled=" + $Node.Enabled);
            }
            TestScript = {
                write-verbose "running ConfigurationFile.TestScript";
                write-verbose "folder = $($Node.ConfigFolder)";
                write-verbose "filename = $($Node.ConfigFile)";
                return (Test-Path $Node.ConfigFile);
            }
            GetScript = { @{Configured = (Test-Path $Node.ConfigFile)} }         
            DependsOn = "[File]ConfigurationFolder"
        }

    }
}

作为参考,我的配置数据如下所示:

For reference, my configuration data looks like this:

$config = @{
    AllNodes = @(
        @{
            NodeName = "*"
            ConfigFolder = "C:\myapp\config"
            ConfigFile = "C:\myapp\config\config.txt"
        }
        @{
            NodeName = "ServerA"
            Enabled = "true"
        }
        @{
            NodeName = "ServerB"
            Enabled = "false"
        }
    )
}

我正在应用以下 DSC:

And I'm applying DSC with the following:

$mof = MyApp -ConfigurationData $config;
Start-DscConfiguration MyApp –Wait –Verbose;

当我应用此配置时,它很高兴地创建了文件夹,但无法对配置文件执行任何操作.查看下面的输出,很明显这是因为 $Node 变量在 ConfigurationFile/TestScript 的范围内为 null,但我不知道如何从该块中引用它.

When I apply this configuration it happily creates the folder, but fails to do anything with the config file. Looking at the output below, it's obvious that it's because the $Node variable is null inside the scope of ConfigurationFile / TestScript, but I've got no idea how to reference it from within that block.

LCM:  [ Start  Resource ]  [[Script]ConfigurationFile]
LCM:  [ Start  Test     ]  [[Script]ConfigurationFile]
                           [[Script]ConfigurationFile] running ConfigurationFile.TestScript
                           [[Script]ConfigurationFile] node is null = True
                           [[Script]ConfigurationFile] folder =
                           [[Script]ConfigurationFile] filename =
LCM:  [ End    Test     ]  [[Script]ConfigurationFile]  in 0.4850 seconds.

我花了一整天的时间在网上搜索这个特定的问题,但所有变量、参数和配置数据的示例都使用文件和注册表资源或其他非脚本资源,我已经在使用这些资源我的脚本中的ConfigurationFolder"块.我遇到的问题是如何从ConfigurationFile"等脚本资源中引用配置数据.

I've burnt off an entire day searching online for this specific problem, but all the examples of variables, parameters and configuration data all use File and Registry resources or other non-script resources, which I've already got working in the "ConfigurationFolder" block in my script. The thing I'm stuck on is how to reference the configuration data from within a Script resource like my "ConfigurationFile".

我画了一个完整的空白,所以任何帮助将不胜感激.如果所有其他方法都失败了,我可能必须为每个服务器创建一个单独的配置"脚本并对值进行硬编码,如果可能的话,我真的不想这样做.

I've drawn a complete blank so any help would be greatly appreciated. If all else fails I may have to create a separate "configuration" script per server and hard-code the values, which I really don't want to do if at all possible.

干杯,

迈克

推荐答案

ConfigurationData 仅在编译 MOF 文件时存在,在 DSC 引擎应用您的脚本时不存在.Script 资源的 SetScript、GetScript 和 TestScript 属性实际上是字符串,而不是脚本块.

ConfigurationData only exists at the time the MOF files are compiled, not at runtime when the DSC engine applies your scripts. The SetScript, GetScript, and TestScript attributes of the Script resource are actually strings, not script blocks.

可以生成这些脚本字符串(已展开 ConfigurationData 中的所有必需数据),但您必须小心正确使用转义符、子表达式和引号.

It's possible to generate those script strings (with all of the required data from your ConfigurationData already expanded), but you have to be careful to use escapes, subexpressions and quotation marks correctly.

我在 http://social.technet.microsoft.com/Forums/en-US/2eb97d67-f1fb-4857-8840-de9c4cb9cae0/dsc-configuration-data-for-script-resources?forum=winserverpowershell

这篇关于PowerShell DSC - 如何将配置参数传递给 ScriptResources?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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