PowerShell to the rescue

Jun 28

Written by: Joe Brinkman
6/28/2007 4:04 PM  RssIcon

PowerShell Recently, while working on a new module installer I needed to be able to roll back my test environment as I went through the various iterations.  This was not a trivial installation and therefore I knew that I would likely have to take several stabs at this before I got it right.

Normally, I would turn to either Virtual PC or VMWare in this case.  Unfortunately Vista and Virtual PC were not cooperating today and it was taking forever just to do simple file copies between the VPC and host machine.  Installing the module which clocked in at just over 4Mb took forever.  All of my time savings was being eaten up by a finicky Vista install.  Since I re-paved my machine a few months back I did not currently have a VMWare installation running on this machine so that was not really an option.

 Lucky for me, my friendly neighborhood superhero just happened to be in the area and heard my calls for help.  Whipping out my trusty copy of Windows PowerShell in Action I was able to quickly craft a function to handle this daunting task.  Since I am a relative newbie at PowerShell it was a good learning excercise.  Once I got past dot sourcing my script file, I was off and running.

function RestoreDNN ($user, $password) {
    "Restoring website..."
    $dnnDir = 'D:\Websites\DNNTest'
    Copy-Item "$dnnDir\config\web.Config" $dnnDir
    Copy-Item "$dnnDir\config\siteurls.Config" $dnnDir
    Remove-Item "$dnnDir\app_code\MyModule" -recurse -force
    Remove-Item "$dnnDir\desktopmodules\MyModule_*" -recurse -force

    "Stopping web service..."
    Get-Process w3wp |Stop-Process

    "Restoring database..."
    osql -U $user-P $password -Q "RESTORE DATABASE DNNTest FROM DISK = 
            'c:\MSSQL\Backup\dnntest.bak'"

    "Done"
}

With the script written and run in my console, I am now able to restore my environment by a simple command "RestoreDNN myUser myPassword", 10-15 seconds later my DNN environment is back to where it was before I installed the module, and ready for another round of testing.

This script is a sample for illustrative purposes and is not intended to be production code.  I have cut out some repetitive code and removed specific project references.  I am sure that over time I will make it a little more generic and more robust, but for now it does what I needed, and took me less time to write than it would have taken to install and setup a VMWare environment.

If you are interested in PowerShell, then I highly recommend you read the blogs by the PowerShell team or ThePowerShellGuy as they contain a lot of valuable information for both newbie's and experience PowerShell gurus.

1 comment(s) so far...


Gravatar

re: PowerShell to the rescue

Joy. This is exactly the sort of quick and easy script that we hoped people would be able write and save themselves a ton of work.

Thanks for sharing this.
PS - I love the icon!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

By Jeffrey Snover on   6/28/2007 5:44 PM
Sponsors Minimize
spacer
dummy