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.

#DemoConfig.ps1
#Sample configuration template
#this is for demonstration purposes and proof of concept
@"
[Configuration]
    Computername={0}
    IPAddress={1}
    SubnetMask={2}
    DefaultGateway={3}
    DNS1={4}
    DNS2={5}
    Comments={6}
[Update]
    EnableFoo={7}
"@

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”,”192.168.1.3″,”255.255.255.0″,”192.168.1.254″,”192.168.1.1″,””,”Te
sting”,”False”
[Configuration]
    Computername=ServerA
    IPAddress=192.168.1.3
    SubnetMask=255.255.255.0
    DefaultGateway=192.168.1.254
    DNS1=192.168.1.1
    DNS2=
    Comments=Testing
[Update]
    EnableFoo=False
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.

configurator

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.
$BuildConfig= 
{
#!!!!!Modify this section!!!!!
    #enter the path to the configuration template
    $file="c:\scripts\posh\democonfig.ps1"
#!!!!!Modify this section!!!!!
 
    if ((Get-ChildItem $file -ea "silentlyContinue").Exists) {
 
        if ($chkFoo.Checked) {
            $foo="True"
         }
        else {
            $foo="False"
        }
        
        #execute the template script which generates a here string
        #and format it with the -f operator       
        (&$file) -f ($txtComputername.Text).ToUpper(),$txtIP.Text,`
         $txtSubnet.text,$txtGateWay.text,$comboDNS1.SelectedItem,`
         $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"
        $form1.Close()
     }
    } #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?