Save-Config.ps1
function Save-Config { <# .Synopsis Saves a configuration .Description Saves a DSC configuration .Example Save-Config -Name MyConfig .Link Get-Config .Link Use-Config #> [CmdletBinding(DefaultParameterSetName='Name')] [OutputType([Nullable])] param( # The Configuration Command [Parameter(Mandatory=$true,ParameterSetName='CommandInfo', Position=0,ValueFromPipeline=$true)] [Management.Automation.ConfigurationInfo] $Configuration, # The name of the configuration [Parameter(Mandatory=$true,ParameterSetName='Name', Position=0)] [string] $Name, # Any notes about the configuration [Parameter(Position=1)] [string] $Note, # If set, will overwrite an existing configuration file. [Switch] $Force ) process { if ($PSCmdlet.ParameterSetName -eq 'Name') { # If we're provided a name, look up the configuration function $Configuration = Get-Command -CommandType Configuration -Name $name if (-not $Configuration) { return } $null = $PSBoundParameters.Remove('Name') $PSBoundParameters["Configuration"] = $Configuration Save-Config @PSBoundParameters return } #region Save Configuration if ($PSCmdlet.ParameterSetName -eq 'CommandInfo') { $configText = @" configuration $($Configuration.Name) { $($Configuration.Definition) } "@ $ConfigurationRoot = Join-Path (Join-Path $env:ProgramData "Start-Automating") "cFg" if (-not (Test-Path $ConfigurationRoot)) { $null = New-Item -ItemType Directory -Path $ConfigurationRoot } $ConfigurationPath = Join-Path $ConfigurationRoot "$($Configuration.Name).config.ps1" if ((Test-Path $ConfigurationPath) -and -not $Force) { Write-Error "$($configuration.Name) already exists at $ConfigurationPath, use -Force to overwrite" return } if ($Note) { $notePath = Join-Path $ConfigurationRoot "$($Configuration.Name).config.notes.txt" [IO.File]::WriteAllText($notePath, $Note) } [IO.File]::WriteAllText($ConfigurationPath, $configText, [Text.Encoding]::Unicode) } #endregion Save Configuration } } |