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.
I, too, am curious about the best way to do this.
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
This is the first thing I tried and it did not work, I got no output nothing when trying this.
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