Category: Blog

  • Attaching All Databases with PowerShell–Finding My MDF Files

    I wrote a PowerShell script recently to actually accomplish a task I that I needed. What’s more, this was the first time I thought that Powershell might prove more useful than other methods. This series looks at my script, and this part examines the first part that I wrote.

    The overview contains information about my strategy and breakdown, and this post looks at the first item.

    When I decided I was going to use Powershell for this task, the first thing I decided to do was find all the MDF files in my folder. When I examined my folder, I saw lots of files.

    attach_c

    Actually, my instance had no files, but I copied over all my mdf/ldf files (apart from system databases) from my original install to the \Data folder for my new instance. I had created these databases for various tests and experiments, and as such, they tended to use the default naming from SQL Server. This meant:

    • The database name was used as the mdf file, i.e. the Baseball database has Baseball.mdf as the file.
    • The log file is the database name with _log.ldf. As in Baseball_log.ldf.

    To start the script, I began by noting I’d need some parameters for the script, as in the folder where the data was stored. I decided to start with a variable, which I can then turn into a parameter.

    $folder = ‘D:\mssqlserver\MSSQL11.MSSQLSERVER\MSSQL\DATA’
    $debug = 0

    I include a “debug” variable that I can use to print out information if needed.

    I started with the  Get-ChildItem command using the folder. I can use this in a foreach loop to run through all the child items.

    # loop through each  of the file
    foreach ($file in Get-ChildItem $folder)
    {

    # end for loop of files
    }

    Note that when I build these loops, I close the brackets first, and include a comment that helps me figure out where this item ends. Before I go further, I decided to start outputting information. I added a debug statement.

    # loop through each  of the file
    foreach ($file in Get-ChildItem $folder)
    {
    #Debug
    if ($debug -eq 1)
    {
    write-host $file.name
    #end debug
    }

    # end for loop of files
    }

    This will output all the files in the folder. I can change the debug value to 1 and then I’ll get this output:

    attach_g

    I see all my .mdf and .ldf files, along with my Filestream storage folders. Now I need to limit things to a specific type of file.

    There’s an Extension property for the items in a folder that I can use. I’ll add that.

    if ($file.Extension -eq ‘.mdf’)
    {
    if ($debug -eq 1)
    {
    write-host $file.name
    #end debug
    }

    # end if
    }

    When I run this, I get this output, and immediately see a problem.

    attach_h

    My output runs together. I need to differentiate which log output is being printed. I do that with a message before each file name.

    write-host “MDF Files: ” + $file.name

    With this added (and customized) for each debug message, I get this:

    attach_i

    That gets me the list of MDF files. If I turn off debugging, and add just a print, I see just my MDF files.

    attach_j

    That’s a good loop. I’m sure there are easier and shorter ways to do this, but this works well, and it gives me flexibility if I’d like to change to another extension.

    This is also the basis of moving forward, where I’ll need to connect to SQL Server and check this list of files against the databases on the server.

  • Sabbatical–Plate Joiner

    One of the tools I need for building the flagpole is a plate joiner. This is a tool that cuts slots in wood that you fill with a little wooden piece called a biscuit. It’s a neat item that I’ve seen used before, but I’ve never owned one.

    I ordered one from Amazon with a bit of bonus money from work and it arrived. I wanted to get working with it, but life got in the way, so it was a few days before I could get to it. My plan was to work with it today, maybe building a few simple drawers that I’ll use in the garage.

    I haven’t read the manual, after all, who does that? However while I applied a bit of ice to my knee, I decided to watch a bit of YouTube to get some ideas and hints on how to use the tool. I think learning how to gain some skill has been one of the most amazing things from YouTube for me.

    In any case, I watched a basic Plate Joiner video, then a Tips and Tricks video.

    Then it was time to practice. First I unpacked the tool. I spent a few minutes checking out the manual to be sure I knew where the knobs where.

    Photo May 24, 1 36 55 PM

    I grabbed two boards, thinking I’d make the right angle jog I saw in one of the videos. I lined them up and used a corner brace to put lines on both boards.

    Photo May 24, 1 36 59 PM

    Once that was done, I used the sand paper trick to raise the tool slightly and cut three slots.

    Photo May 24, 1 39 02 PM

    Not really a bit deal, but I was somewhat excited.

    Photo May 24, 1 39 14 PM

    Once that was done, I cut the other board and then put the biscuits in.

    Photo May 24, 1 40 38 PM

    The test fit looked good, things lining up. One biscuit was a little thick, so I sandpapered it with 3 or 4 strokes and then it was fine.

    Photo May 24, 1 40 57 PM

    I added glue and then used my tool to actually apply a little weight as I moved on to another project.

    Photo May 24, 1 44 48 PM

  • Sabbatical–Flagpole Project Plan

    Here are some thoughts on my flagpole project. This is how I see the project going.

    To start, I know there are a few things I need to get done:

    • Clean out stones and set new base
    • Build base
    • build sawhorses to hold pole
    • buy raw materials
    • measure space to work
    • build flagpole
    • finish (paint) flagpole
    • order metal supplies for the flag
    • get new flag

    That’s the rough list of things, but obviously this doesn’t really give me the plan or order of things. Realistically, I’m looking at something more like this:

    • measure space to work
    • build sawhorses to hold pole
    • buy raw materials
    • build flagpole
    • Build base
    • Clean out stones and set new base
    • order metal supplies for the flag
    • finish (paint) flagpole
    • get new flag

    This is a more likely order, though I can do some things like ordering parts while I’m doing others. I’ll also fit the base cleaning time in the middle of the building.

    How about timing?

    Obviously I need to lay out some times here. I’ve allotted myself 6 weeks to do all this, though I may end up with some prep ahead of time.

    • measure space to work – 1 day, May
    • build sawhorses to hold pole – 2 days – May
    • buy raw materials – 1 day – May
    • build flagpole – June, 2 weeks
    • Build base – June 1 – week
    • Clean out stones and set new base – 1 week, June
    • order metal supplies for the flag
    • finish (paint) flagpole – 1 week – July
    • get new flag

    That’s the rough plan. I’ll prep the last few weekends in May, getting sawhorses ready and things laid out in the front yard. I’ll also get the raw materials ready, I think. That could wait until June 1 or 2, but we’ll see. I may stage things a bit.

    I do also need to order more clamps, after watching the show. It’s also an excuse for me to get a plate joiner, but I’ll do that and get biscuits. I’ve been wanting to try that for a long time.

    That’s a rough plan, enough to get me started, though I’m sure I’ll need to rough out more details over time.

    For now, I’m looking to get some materials to build these: Knockdown Sawhorses

  • Sabbatical–Habitat for Humanity

    My employer, Red Gate Software, offers a sabbatical to their employees, and this year I’m taking mine. I’ll be gone from June 2 – July 14, away from work, but I’ll be blogging here about my experience.

    A few years ago I was on vacation with my wife and a few other couples. One night at dinner, one of the other couples posed this question: what would you do if you won the lottery and never had to work again?

    There were various answers from the people at the table. When my turn came, the first thing that came to my mind was working full time for Habitat for Humanity. I’d consider other charities, but the things that speak to me are food, shelter, and education.

    One of my sabbatical decisions was the volunteer for Habitat of Denver, and I began that journey by attending a volunteer orientation and filling out paperwork. In a few days my name should be in their database and I’ll be able to start signing up for shirts at builds and the ReStores that Habitat runs. I’m looking forward to getting a few shifts a week at their stores, and I’m hoping I can help them fulfill their mission.

    Photo May 12, 6 49 41 PM

    I do believe strongly that we all should volunteer and give back to society in our lives It’s at a time and place of your choosing, when it works for you. You might have done it in the past, you might do it now, or maybe it will be something that you do when you retire from technology, but I hope you consider sharing of yourself and helping the world at some point.

    However choose those things that speak to you. Things that mean something to you and which will benefit others.