简体   繁体   English

是否可以延迟评估Windows PowerShell所需状态配置中的功能?

[英]Is it possible to lazily evaluate features in Windows PowerShell Desired State Configuration?

In Windows PowerShell Desired State Configuration, you can define features: 在Windows PowerShell所需状态配置中,可以定义功能:

Node $MachineName { 
    # define the IIS Role 
    WindowsFeature IIS { 
        Name = “Web-Server” 
    }

    # define the SQL Role 
    WindowsFeature SQL { 
        Name = “SQL-Server” 
    }

    # require ASP.NET 4.5 
    WindowsFeature ASP { 
        Ensure = “Present” 
        Name = “Web-Asp-Net45” 
        DependsOn = "[WindowsFeature]IIS"
    }
}

Using this setup, not only ASP gets installed, but also the IIS and the SQL features. 使用此设置,不仅会安装ASP ,还会安装IISSQL功能。 The IIS feature i can understand, since ASP depends on that. 我能理解的IIS功能,因为ASP依赖于此。 But the SQL feature is not defined as "Present", and is not required by another "Present" feature. 但是, SQL功能未定义为“存在”,并且另一个“存在”功能也不需要该SQL功能。

Is it possible to define these basic features (as a kind of repository) but only install the required features? 是否可以定义这些基本功能(作为一种存储库),但仅安装必需的功能?

Yes and no. 是的,没有。

First, I want to point out that DependsOn tells DSC which order to do things, and that's about as far as DSC understands it. 首先,我想指出DependsOn告诉DSC做什么事情的顺序 ,这是DSC所了解的。 It just lets you decide that some resource needs to be executed after one or more other resources. 它只是让您决定某些资源需要在一个或多个其他资源之后执行。

Leaving out Ensure = 'Present' , if it works, is likely just defaulting to 'Present' ; 如果可以Ensure = 'Present'不使用Ensure = 'Present' ,则可能只是默认为'Present' there is no way to put a resource in there (without additional code), that doesn't get executed. 无法将没有执行的资源放入其中(没有其他代码)。

I'm not certain what you mean by a "repository" unless you just mean to have it present in the Configuration {} block for future reference or use. 我不确定您所说的“存储库”是什么意思,除非您只是想让它出现在Configuration {}块中以备将来参考或使用。

What I mean by "additional code" is that you can control which resources get used when you generate your config from this script. 我所说的“附加代码”是指您可以控制从该脚本生成配置时使用哪些资源。

Stepping back a bit, the code block you have there is a configuration script which you must execute to generate an MOF file, which will then be the actual thing that gets applied to the node. 退一步,您所拥有的代码块中有一个配置脚本 ,必须执行该配置脚本才能生成MOF文件,然后该文件将成为应用于节点的实际内容。

That script is in fact powershell so you can apply your logic as to what gets applied at that point (when the MOF is being generated). 该脚本实际上是强大的工具,因此您可以应用有关此时应用的逻辑(生成MOF时)。 This happens in the context of the machine generating the MOF though, not on the target node, so you can't use any logic that requires code being run on the target at the time of configuration application. 但是,这是在生成MOF的机器的上下文中发生的,而不是在目标节点上,因此您不能使用任何要求在配置应用程序时在目标上运行代码的逻辑。

DSC has some built-in stuff to facilitate this, through the use of the -ConfigurationData parameter and the automatic variables like $AllNodes and $Node . DSC有一些内置的东西来推动这项工作,通过使用的-ConfigurationData参数和自动变量类似$AllNodes$Node

I'd like to put some code here instead of just links and an explanation, but you really need to get a full sense of where and when these pieces fit together. 我想在这里放一些代码,而不仅仅是链接和说明,但是您确实需要全面了解这些部分在何时何地组合在一起。

I think the most important takeaways are these: 我认为最重要的要点是:

  • The configuration you write (as shown in your code block) is Powershell code and as such you can use logic to determine which resources are applied to which node. 您编写的配置(如代码块中所示) 是Powershell代码 ,因此,您可以使用逻辑来确定将哪些资源应用于哪个节点。
  • The execution of the configuration happens typically on a computer that is not the target node, before the target node is up or being configured, so consider the context of execution. 配置的执行通常在目标节点启动或配置之前不是目标节点的计算机上进行,因此请考虑执行的上下文。
  • Look at the ways that -ConfigurationData is used in Microsoft's examples. 查看在Microsoft的示例中使用-ConfigurationData的方式。

Have a look at these: 看看这些:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM