#TIL Powershell for Dev Env Setup Is Awesome

This is my first week back after two months off taking care of my new twin girls, and my new year’s resolution this year was to blog more, so I’m going to attempt to create a series of “Today I Learned” posts. They will consist of short commentary on some sort of technical thing I did or learned about during the week. The first one will be on our team’s Powershell script that we use to set up our development environment.

Before we had our powershell script, setting up our dev environment was a nightmare – new hires would spend up to a week working on it, and if anything was ever added / changed / removed, the entire team was down for at least a day. Given our codebase’s dependency on a bunch of other things (other teams’ projects, network setup, development servers, etc.), we were losing a considerable amount of time.

So, enter the Powershell script. Using Powershell, we’re able to automate a good portion of our dev env setup and maintenance – here’s a few examples of what we’ve been able to set up.

Adding things to your environment’s path variable

Set-ItemProperty -Path 
    "Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment" 
  -Name PATH -Value "value"

Use this to add things to your path variable. We do this mostly so we don’t have to include full absolute paths later on in our script to execute things like MSBuild.

Creating websites, applications, virtual directories in IIS

This requires the inclusion of a script called Carbon, you can find it here

Install-IisWebsite -Name "Website" -Path "path" -AppPoolName "Name"
Install-IisApplication -SiteName "Website" -Name "AppName" -Path "path"
Install-IisVirtualDirectory -SiteName "Website" 
    -Name "VirtualDirectoryName" -Path "path"

Carbon is awesome, and one of the things we use most in our script. Among other things, it allows you to manipulate your IIS configuration by adding / editing / removing websites, applications, virtual directories, and application pools.

Command line operations

$currentWorkspaces = tf workspaces /collection:http://my.tfsserver.com
$currentWorkspaces[3].ToString() #will get the 3rd line of the result of 
                                 #the command line operation

You can straight up call any command line operation (here we’re calling tf.exe to reference TFS source control), and have the output saved as an array variable. It’s a bit hacky here, relying on the 3rd output line to give us the workspace info we need, but command line output doesn’t change often and it works :)

Combining all these things together, our script sets up 5 different websites in IIS with their required app pools, virtual directories, and security settings, gets the latest version of external dependencies from TFS, gets our latest code from Github, builds it all using MSBuild, and also deploys our database changes with MSBuild and SQL Database Projects. Epic win.