TAG | batch files
I just finished my first ever powershell script and I’m pretty proud of it – even though I did borrow a good portion of it.
We’re starting a pretty long-term project at work – it’s scheduled for a final deliverable date set in July of 2011 – and with our small(er) dev team I’m pretty certain more hands than my own will be working on this over the course of the entire project.
This new project is going to have a few external dependencies (Moq, StructureMap, possibly MvcTurbine) and I wanted a quicker and friendlier way for a new developer coming into the project to get up-to-speed environment wise. I originally started with a batch file. I know, I know. It’s my comfort zone. Too many years of not having something like powershell at my finger-tips.
After about 10 minutes of fighting with batch language limitations I decided to move on and try my hand at using powershell to help automate the developer environment setup.
So I borrowed some code from Adam Weigerts blog to bypass the need for the gacutil (and the .Net Framework SDK, a whopping 100mb download!). Once I had this, I just had to come up with the code to recurse over all the .dll files that are stored in sub-folders within a “Source Dependencies” folder.
From concept to finish I think this took me about an hour to come up with, I’m no programming super soldier so anyone who is doing windows development should be able to throw together a powershell script to automate their common, repetitive and annoying tasks.
The code is included below, I’ll be adding to it as the project goes on, pushing in some other environment setup features (dev db creation/sync would be the next feature for this). Now that I’ve taken a swing at PowerShell I’m really happy with it and I really am sorry it took so long for me to use it.
If you’re still automating things with Batch files, you’re missing out. Big.
BEGIN {
Clear-Host
$ErrorActionPreference = "Stop"
if ( $null -eq ([AppDomain]::CurrentDomain.GetAssemblies() |? { $_.FullName -eq "System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" }) ) {
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") | Out-Null
}
$publish = New-Object System.EnterpriseServices.Internal.Publish
}
PROCESS {
$files=get-childitem ".\Source Dependencies" *.dll -rec|where-object {!($_.psiscontainer)}
foreach( $file in $files ) {
$assembly = $file.fullname
if ( -not (Test-Path $assembly -type Leaf) ) {
throw "The assembly '$assembly' does not exist."
}
if ( [System.Reflection.Assembly]::LoadFile( $assembly ).GetName().GetPublicKey().Length -eq 0 ) {
throw "The assembly '$assembly' must be strongly signed."
}
Write-Output "Installing: $assembly"
$publish.GacInstall( $assembly )
}
}
I use Inno Setup as an installer for all my windows-based projects. Often-times I want to build the installer when I build the project from within Visual Studio but only when I build the “Release” configuration.
Here is the post-build script that will accomplish this:
c:
cd\
IF Release==$(ConfigurationName) "C:\Program Files\Inno Setup 5\Compil32.exe" /cc <PATH_TO_ISS_FILE>
batch files · inno-setup · post-build · Programming · visual studio
This was originally posted by Pam over on StackOverflow but I thought it was too awesome and had to share it, just in case you want validate a palindrome from the command line one day.
EDIT: I removed the code and provided a link to the comment at StackOverflow. You can check out Pams’ blog here: http://pm-db.blogspot.com/
