Archive for July 2010
I helped Tamara figure this out the other day and thought others might want to know. She needed to know which databases would be backed up when a given backup plan was executed. The server was a sql 2000 server and the machine she was using didn’t have enterprise manager installed.
SELECT
*
FROM
msdb.dbo.sysdbmaintplans
LEFT JOIN msdb.dbo.sysdbmaintplan_databases
ON msdb.dbo.sysdbmaintplan_databases.plan_id = msdb.dbo.sysdbmaintplans.plan_id
WHERE
msdb.dbo.sysdbmaintplans.plan_name = 'PUT YOUR PLAN NAME HERE'
So if anyone out there finds themselves needing to know which databases will be backed up by a given backup plan, and for whatever reason you don’t have Sql Management Studio or Enterprise Manager then this should work.
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’ve been thinking about the whole Comments-as-a-code-smell argument for a very long time. When I first heard this idea, I was in the comments are definitely needed side of the argument.
“Who could hate comments so much that they would label them as a code smell?” I thought.
Well, I had an epiphany the other day.
I’ve never seen a block of code that actually needed a comment. Ever.
If you have a section of code that feels too complex, instead of adding a comment do any/all of the following and you can, in almost every case, reduce the need for comments by 100%:
- add a sourcecontrol commit comment
- do a simple refactor (Compose Method ftw people),
- use more descriptive variable/function/class/whatever names.
If you do all of these things and the code you are working on still seems too complex, you may have bigger problems with your overall architecture/design and adding a comment isn’t going to help. In this case it’s time to go back to the drawing board.
Also, everytime you commit a changeset into sourcecontrol that contains a section of code commented out god kills a kitten. Please think of the kittens.
code · code-style · opinionated · Programming
