Category: Blog

  • DLM Automation–Making a Database Connection

    One of the things I’ve been wanting to do is dig more into the command line automation cmdlets from the Redgate DLM Automation suite. While there are plugins for many build and release systems, I should be able to customize, or fall back, to the command line if necessary.

    Plus, this allows me to get a better feel for what’s happening at each step.

    This post looks at the basics of making a connection to a database. Note that this is documented on the DLM Automation 2 Cmdlet Reference page.

    New-Database Connection

    When I started working with the connections, I used the New-DatabaseConnection cmdlet. However, when I run this now, I get this:

    2016-10-14 15_32_04-DLM Automation–Making a Database Connection - Open Live Writer

    That’s fine, as my call to the new cmdlet works.

    2016-10-14 15_32_34-cmd - powershell

    Do I know what’s happening here? I should investigate further.

    First, if I’ve run this, what does it do? Not much, apparently. If I run an Extended Events session looking for a connection, I don’t get one. Let’s try a new command, this time using a name and password.

    2016-10-14 15_37_53-cmd - powershell

    That’s an invalid password for my instance (as it should be on every instance in the world). However, I don’t get an error. No connection was made. In this case, the cmdlet is storing off the connection credentials that will be used. If I don’t provide the user/password, then a trusted (Windows auth), connection is assumed.

    Can I now test this? Sure, I’ll use the Test-DLMDatabaseConnection to make a connection.

    2016-10-14 15_39_37-cmd - powershell

    This fails. That’s good to know. Now I can use this to actually determine if I target database is alive before I continue processing. My preferred method here would be to wrap this up in some error handling. For example, I can use a try..catch structure in PoSh. I’ll switch to the ISE to make this cleaner. Here’s the code

    try
    {
    Test-DlmDatabaseConnection -$test -ErrorAction Stop
    } 
    catch
    {
    Write-Host "error"
    }

    In the ISE:

    2016-10-14 16_22_45-Windows PowerShell ISE

    I can also get the error details from the current object ($_). Note, I’m not a PoSh expert, so if there are better ways, let me know.

    2016-10-14 16_36_48-Windows PowerShell ISE

    I can also pass in some common parameters, like –ErrorAction and –ErrorVariable. Then I can use these to decide whether or not to continue. In my case, I’ll just write a message. Here’s my code:

    $testconn = New-DlmDatabaseConnection -ServerInstance ".\SQL2014" -Database "st_integration" -U "sa" -Password "test"
    try
    {
    Test-DlmDatabaseConnection $testconn -ErrorAction SilentlyContinue -ErrorVariable ErrorHolder;
    } 
    catch
    {
    Write-Host $_.Exception.Message
    };
    
    if ($ErrorHolder)
    {
    Write-Host($ErrorHolder)
    }
    else
    {
    Write-Host("Everything works");
    }

    That will give me this:

    2016-10-14 16_41_08-Windows PowerShell ISE

    If I remove the user and password parameters, I get this:

    2016-10-14 16_41_29-Windows PowerShell ISE

    You didn’t think I’d publish the real sa password, did you? Of course not. If I change the code to the right password, things connect.

    Checking the Object

    What if I just want to check the object? Well, there are properties associated with the object. For example, if I use a simple variable to assign to the object, I can see some properties. If I just type the variable name, $testconn, I get these properties:

    • ServerInstance
    • Database
    • SQLServerCredential
    • ConnectionString
    • Description

    You can see these returned in a test session.

    2016-11-14 16_58_29-cmd - powershell

    Other Options

    I have other parameters I can use with my connection, or rather, in place of my connection object. I can use a connection string, with all of the parameters that are valid there. For example:

    2016-10-14 16_53_17-Windows PowerShell ISE

    If I enter the correct password, I get this:

    2016-10-14 16_54_09-Windows PowerShell ISE

    My connection string can contain all of the items I might normally use: ApplicationIntent, Encrypt, NetworkLibrary, etc. A full list of parameters is at: https://www.connectionstrings.com/all-sql-server-connection-string-keywords/

    The New-DlmDatabaseConnection object outputs a RedGate.DLMAutomation.Compare.SchemaSources.DatabaseConnection object, which is the input to quite a few other of the cmdlets. While not complex, it is worth understanding how to use this cmdlet, which is the basis for determining from which source or two which target you will be moving database changes.

    In another post, I’ll start to bind this cmdlet with others and actually perform some database deployment automation work.

  • More dbatools–Get-DbaTcpPort

    After the MVP Summit, I was excited to check out more code and features in all aspects of SQL Server, but since I’d had a quick conversation with Aaron Nelson and Chrissy LeMaire, I decided to tackle another part of dbatools.

    BTW, they gave me a cool sticker.

    Photo Nov 11, 5 05 19 PM

    I like using PoSh for some tasks, especially when I don’t have an easy way to do something in SSMS or want to run a task across a variety of instances. In this case, as I glanced through the September updates, I found a good one.

    Get-DbaTcpPort

    I don’t love the mixed naming, and I’ll get used to it, but I do love the autocomplete in PoSh. I can type this:

    2016-11-11 17_13_13-powershell

    When I hit tab, I get this:

    2016-11-11 17_13_19-powershell

    So I don’t need to worry about the case, but I can easily use Get-DbaTcpPort. This is one of those quick things I’ve done more than a few times as I troubleshoot connectivity with VMs or new instances. I have learned I can get this from the error log, but that’s a pain.

    The main thing I can do here is pass in the SqlServer parameter and get a port. Here’s the ports for an instance on this machine:

    2016-11-11 18_06_24-powershell

    There is a –Detailed option, which gives me the server name and cleaner results.

    2016-11-11 18_07_57-powershell

    I was expecting the result of whether this was static or now, but it’s not appearing. I see the warning, but am not sure why this doesn’t work. Apparently this works with SqlWmi, but I’d like to know more to troubleshoot things.

    When I hit one of my other instances, I got this:

    2016-11-11 18_07_02-powershell

    At first I thought this was my remote admin connections setting, which was disabled, but that wasn’t it. I found instead that I’d left TCP disabled by default on this instance. Once I changed that and restarted the instance, it worked fine.

    2016-11-11 18_14_14-powershell

    This isn’t something I’d use often, but it’s a really quick way to check on the status of an instance port if you need to. I’d urge you to give it a try and see what you think.

  • A Good Deed After Game Night

    When we set up Game Night, we decided to sell tickets to reduce our risk. However, we didn’t have a good way to easily match up the ticket price with the costs of drinks. As a result, we ended up with about $105 extra from our ticket sales over the bar bill.

    When Andy (b|t) and I work on putting together an event, it’s not usually to make money. In fact, we often joke that our best ideas have no way to make money. That’s OK, since part of what we believe is that we need to make a difference in the world at times. We work to earn money for our families, but we also want to spend time trying to do some good in the world.

    As a result, we wanted to take the extra money and help someone. We had a few ideas, and contacted some people, but eventually settled on this.

    Don Kolenda needed eye surgery. He’s a member of our SQL Server community, and we decided to donate the $105 to Don. If any of you want to help Don, please feel free to do so.

    Thanks to everyone that supported Game Night, and know that a little bit of your ticket sales went to help someone in need.

  • Growing Speakers for T-SQL Tuesday #84

    tsql2sday150x150It’s T-SQL Tuesday again, and for $#84, we have a non-technical topic. Hopefully this will then let us produce more technical content by growing new speakers. That’s the topic from Andy Yun, and it’s a good one.

    This is the monthly blog party, and you should feel to participate today, or any day in the future. Read Andy’s invitation for the rules.

    No Shortage of Speakers

    When we started SQL Saturday, Andy Warren and I worried about finding speakers for events. In the last 10 years, we’ve seen many, many people begin speaking and enjoy it. I don’t worry about finding speakers for SQL Saturdays anymore.

    However, I do think we could do more to encourage even more people to speak and share knowledge. One of the problems we have in this industry is that we don’t always get information spread out quickly to a wide audience and evolve our practices. Too many of us use our habitual practices long after there are better ways to build software. One of the ways we can work to improve the industry is with more speakers.

    I’ve given a talk on creating your first technical speaking opportunity. I can say that it worked since a couple of people that were in the audience have actually given presentations at the user group. One of the items that I suggest in there is picking something you’ve learned/solved, and then teach it to others in your company/department/group.

    This is often an easier way to get started, since it will be a smaller group, one you know, and it’s safer. It could even be more of a discussion you lead than a presentation.

    Certainly this can be hard as you need to be open to debate, accept you might get some things wrong, and that you might learn something. There is stress in trying to come across as an expert, but I’d rather you try to teach something, with the idea that this is your understanding of a topic, but you are open to admitting you might be wrong if someone else knows more.

    If that’s the case, then perhaps have that individual present next time.

    I’d really encourage more groups and departments to get a weekly presentation together from someone. This can be at lunch, or a standing meeting. This can be a group learning style like the SDTIG, or a quick 20 minute presentation on some topic.

    Just try it.

    Learn, speak, share.

    Get some buy in from your management to support this, and if you need a good reason, use this.

    Corporate Dilemma