How to create mofs from Configuration File and Configuration Data File

Welcome Forums DSC (Desired State Configuration) How to create mofs from Configuration File and Configuration Data File

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
2 years, 8 months ago.

  • Author
    Posts
  • #34801

    Participant
    Points: 0
    Rank: Member

    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 ConfigurationData.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.

  • #34802

    Participant
    Points: 0
    Rank: Member

    I, too, am curious about the best way to do this.

  • #34819

    Participant
    Points: 0
    Rank: Member

    Assuming you are in the same directory as where you files are placed

    # Dot-source the PowerShell script holding your configuration
    . .\FarmConfiguration.ps1
    
    # Then your FarmConfiguration "function" is available for you to run, pointing to a data file of your choice
    FarmConfiguration -ConfigurationData .\ConfigurationData.psd1
    
  • #34841

    Participant
    Points: 0
    Rank: Member

    This is the first thing I tried and it did not work, I got no output nothing when trying this.

  • #34886

    Participant
    Points: 0
    Rank: Member

    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 you suggest

    .\FarmConfiguration.ps1 -ConfigurationData data.psd1

The topic ‘How to create mofs from Configuration File and Configuration Data File’ is closed to new replies.