0

I have a configuration data file like data.psd1

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

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

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

I have a configuration like 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.

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.

I want to be able to creates MOFs based on various different data files how can I do this? Thanks.

Aaron Stainback
  • 3,489
  • 2
  • 31
  • 32

1 Answers1

0

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. So I updated FarmConfiguration.ps1 to the following.

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
Aaron Stainback
  • 3,489
  • 2
  • 31
  • 32