Cool Config Creation

In my recent Special Forces PowerShell class, one of the students brought up a challenge he was facing when we were talking about the -f replacement operator. This is a handy way to build strings.

PS C:\> “Hello, {0}. It is now {1} and you have {2} processes running.” -f $env:username,(get-date).ToShortDateString(),
(get-process | measure-object).count
Hello, Jeff. It is now 5/5/2009 and you have 82 processes running.

Paul needed to build configuration files and was looking for an easy way, perhaps using a here string as a template. We tossed around a few ideas. When I got home I decided to see what I could come up with. What I have is a proof of concept really, but it might get you started.

First, we need a configuration file. This is essentially an ini-like file enclosed in a here string and wrapped up as a .PS1 PowerShell script.

#Sample configuration template
#this is for demonstration purposes and proof of concept

I can now run this script which writes the here string to the pipeline and use -f to fill in the values.

PS C:\> (c:\scripts\posh\democonfig.ps1) -f “ServerA”,”″,”″,”″,”″,””,”Te
PS C:\>

It only takes an extra step to send the output to a text file. You can also imagine it wouldn’t be too difficult to take information from a CSV file to create the configuration file. But what about a more interactive solution? I built a WinForm script using PrimalForms to serve as a GUI front end to my DemoConfig.ps1.


Simply fill in the values and click the Build Config button to create the file. The form will remain open so you can create another file. This is the relevant code

#Generated Event Script Blocks
#Provide Custom Code for events specified in PrimalForms.
#!!!!!Modify this section!!!!!
    #enter the path to the configuration template
#!!!!!Modify this section!!!!!
    if ((Get-ChildItem $file -ea "silentlyContinue").Exists) {
        if ($chkFoo.Checked) {
        else {
        #execute the template script which generates a here string
        #and format it with the -f operator       
        (&$file) -f ($txtComputername.Text).ToUpper(),$txtIP.Text,`
         $comboDNS2.SelectedItem,$txtComments.Text,$foo | 
         Out-File $txtFilePath.text
        #were there any errors?
        if ($?) {
            Write-Host "Successfully created $($txtfilepath.text)" -foreground Green
    else {
        Write-Warning "Failed to find $file"
    } #end if file exists
} #end BuildConfig scriptblock

In my proof of concept script I’ve hard coded in the here string script but you will probably want to offer the user or yourself an easy way to specify a script. The script block runs the script and integrates the data from the form using the -f operator. Results are piped to Out-File and saved.

I have some other ideas on where to take this topic, and you probably do as well.

I’ve attached a zip file with the scripts and PrimalForm file here.

What do you think?