PowerShell and DotNetNuke

Jul 7

Written by: Joe Brinkman
7/7/2007 7:36 PM  RssIcon

PowerShell Having worked with DotNetNuke for more than 4 years now, I have performed literally hundreds of installations.  While the installation process has gotten easier over the years as we have beefed up the installation routines, it is still not automatic. Even though I can install DNN in under a couple minutes, I often cut corners.  I usually just give the Asp.Net process account full control of the web application folder and use an sa account in the DB.  Neither of these are great practices, but setting up the correct permissions is a little more tedious and after you perform this task dozens of times you find that you soon start taking shortcuts.  The downside to these shortcuts is that I am not really testing out my site in a configuration that mirrors a typical production environment.  This can lead to subtle bugs that will be harder to track down later.

OpenForce400x131_thumb This is where PowerShell can really make a difference.  In my upcoming session at DotNetNuke OpenForce '07 I will be discussing how you can use PowerShell with DotNetNuke to perform a lot of management and administrative functions.  This post is the first in a series of that will show various ways we can use PowerShell to enhance and simplify our DotNetNuke experience.  These blog posts will cover many of the topics I plan to cover in my session.  I am not an expert in PowerShell yet, but my skills are quickly improving.  This series will follow my learning experience as I tackle various tasks for which PowerShell is well-suited.

The first set of posts will focus on simplifying the installation experience.  My goal is not to create "The Ultimate DotNetNuke Installer", but rather to show how you can use PowerShell to easily script a set of very repetitive tasks thereby ensuring consistency and making the overall process easier.  In some cases I may use hard-coded values and business rules, but hopefully it will not be too difficult for you to take my scripts and adapt them to your situation.  So let's begin.

The first step is to define the process we wish to automate.  We'll break our process down and develop the script over a couple of posts.

My installation process looks like this:

  1. Unzip the DNN package to my website directory.
  2. Set permissions for the "Network Service" Account
  3. Create the Web Application in IIS
  4. Create a blank database
  5. Create a user account in the database
  6. Run the DotNetNuke web-based installer

Step 1:  Unzipping Files

This step actually turned out to be much easier than I anticipated.  DotNetNuke uses SharpZipLib in the module installer for extracting files and folders from an embedded resources file.  Since PowerShell is right at home in .Net I thought that I would just end up re-purposing the original installer code I wrote for DotNetNuke.  Using SharpZipLib is a relatively low-level abstraction for handling zip-files.  You have complete control, but it also means that there is a lot of work needed to extract the files while maintaining the folder structure.  In DotNetNuke we use approximately 50 lines of code to unzip a file.

Thinking that there might be an easier way in PowerShell, I did what all good programmers do - I Googled it to see if someone else had already solved my problem. Bingo.  David Aiken had my answer.   The answer turned out to be very simple, and it highlighted one of the great aspects of PowerShell.  Even though PowerShell is based on .Net and has no problem working in the .Net world;  it is equally at home working with COM and WMI.  A little more research showed that the "shell.application" COM object was commonly used in VBScripts and WSH for unzipping files.

function Extract-Zip
{
    param(
        [string] $zipfilename = $(throw "Please enter a zipfilename"),
        [string] $destination = $(throw "Please enter a destination directory"),
        [switch] $showdialogs
    )

    if(test-path($zipfilename))
    {    
        "Unzipping $zipfilename ..."

        $shellApplication = new-object -com shell.application
        $zipPackage = $shellApplication.NameSpace($zipfilename)
        $destinationFolder = $shellApplication.NameSpace($destination)
        # NOTE: This scripts uses the PowerShell Community Extensions for the ternary operator

        $vOptions = (?: {$showdialogs} {0} {20})

        # CopyHere vOptions Flag
        #  4 - Do not display a progress dialog box.
        # 16 - Respond with "Yes to All" for any dialog box that is displayed.

        $destinationFolder.CopyHere($zipPackage.Items(), $vOptions)
        
        "Completed unzipping file..."
    }
}

I have made a few changes from David's original script to enforce the parameters and to suppress the dialogs that popup when extracting the files.  This is a good example of how to make required parameters and how to use switches to power up your scripts.  We'll add this with some ACL magic in our next post.

 

1 comment(s) so far...


Gravatar

re: PowerShell and DotNetNuke

Very good article. Looking forward to the coming blogs on how to do the other steps needed to automate a DNN install?

By smehaffie on   7/10/2007 6:14 AM
dummy