简体   繁体   English

如何在PowerShell期望状态配置中从配置文件和配置数据文件创建MOF?

[英]How to create MOFs from Configuration File and Configuration Data File in PowerShell Desired State Configuration?

I have a configuration data file like data.psd1 我有一个配置数据文件,如data.psd1

@{
  AllNodes = @(
    @{
       NodeName = "*"
       LogPath = "C:\Logs"
    },

    @{
       NodeName = "machine1";
       Roles = @( "SmtpRole", "WebRole" )
    },

    @{
       NodeName = "machine2";
       Roles = @( "SmtpRole" )
    }
  )
}

I have a configuration like FarmConfiguration.ps1 我有一个类似FarmConfiguration.ps1的配置

Configuration FarmConfiguration {
 Import-DscResource -ModuleName 'PSDesiredStateConfiguration'
 Import-DscResource -ModuleName 'MyCustomDsc'

  Node $AllNodes.NodeName
  {
     SimpleTcpIpConfiguration SimpleTcpIp
     {
     }
  }

  Node $AllNodes.Where{$_.Roles -contains "WebRole"}.NodeName
  {
     WebConfiguration Web
     {
     }
  }

  Node $AllNodes.Where{$_.Roles -contains "SmtpRole"}.NodeName
  {
     SmtpConfiguration Smtp
     {
     }
  }
}

I know if I add "FarmConfiguration-ConfigurationData data.psd1" to the bottom of my FarmConfiguration.ps1 file then everything works fine. 我知道如果我在FarmConfiguration.ps1文件的底部添加“ FarmConfiguration-ConfigurationData data.psd1”,则一切正常。

But this is a big problem I don't want my FarmConfiguration to know about the data file at all, it's check into source control. 但这是一个大问题,我完全不希望我的FarmConfiguration知道数据文件,而是将其检查到源代码管理中。

I want to be able to creates MOFs based on various different data files how can I do this? 我希望能够基于各种不同的数据文件创建MOF,该怎么做? Thanks. 谢谢。

Okay I found out how to do what I'm asking. 好吧,我知道了该怎么做。

For all practical purposes FarmConfiguration is a private function inside of the .ps1 file. 出于所有实际目的,FarmConfiguration是.ps1文件内部的私有函数。 So I updated FarmConfiguration.ps1 to the following. 因此,我将FarmConfiguration.ps1更新为以下内容。

param(
    [Parameter(Mandatory)]
    [string]$ConfigurationData
)

Configuration FarmConfiguration {
 Import-DscResource -ModuleName 'PSDesiredStateConfiguration'
 Import-DscResource -ModuleName 'MyCustomDsc'

  Node $AllNodes.NodeName
  {
     SimpleTcpIpConfiguration SimpleTcpIp
     {
     }
  }

  Node $AllNodes.Where{$_.Roles -contains "WebRole"}.NodeName
  {
     WebConfiguration Web
     {
     }
  }

  Node $AllNodes.Where{$_.Roles -contains "SmtpRole"}.NodeName
  {
     SmtpConfiguration Smtp
     {
     }
  }
}

FarmConfiguration -ConfigurationData $ConfigurationData

Now that I've done that I'm able to call it from a separate script like so 现在,我已经可以像这样在单独的脚本中调用它了

.\FarmConfiguration.ps1 -ConfigurationData data.psd1

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

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