Tag: powershell

  • Republish: The (Former) Complexity of PowerShell

    I’m at SQL Bits today, teaching a pre-con with Grant, so I’m republishing The (Former) Complexity of PowerShell

  • Getting a Day Difference in PowerShell–#SQLNewBlogger

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    SSMS froze on me the other day. Actually, it lightly responded to some things, but the window wouldn’t redraw and I couldn’t see the query window. I could see the results pane, but couldn’t get the app to respond.

    I wanted to get the difference between two dates, and wasn’t sure, so I quickly searched. I tried assigning the date to a variable, but this creates a string:

    $start = “2020-03-11”

    With a couple searches, I learned I can use Get-Date to get a date variable. In this case, I’d do this:

    $start = Get-Date -Date "2020-03-11"

    If I did that with two dates, I could get the difference. Here’s a screen show that shows I get the result in a variety of different time slices.

    2022-02-15 10_22_09-E__..._git_fwddemo

    If I wanted just days, I could do this:

    ($end - $start).Days

    That returns just the 712.

    I also learned I could shortcut this with a TimeSpan type.

    New-TimeSpan -Start “2020-03-11” -End (Get-Date)

    I get the same spread of time parts as the image above, or I can enclose this all in parenthesis and then call the “Days” property to get that value.

    SQL NewBlogger

    I hadn’t done much with date and time in PoSh, and after seeing an article from an author, I investigated a bit more. This was a part of what I tried to do, albeit as a response to something not working as expected.

    Good to know how to work with dates, as I can see this being a part of many PoSh scripts that might clean up old files or otherwise take action based on time values.

    You could write this post in about 10-15 minutes and show how you use PoSh to work with date and times.

  • A Little Current Date Arithmetic in Powershell–#SQLNewBlogger

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    I saw a fun post on Twitter recently asking days until retirement. I wrote this code:

    DECLARE @YearsToRetire INT = 11;
    SELECT DATEDIFF (DAY, GETDATE (), (DATEADD (YEAR, @YearsToRetire, GETDATE ())));

    I thought that wasn’t bad, but then I wondered, how would I do this in PowerShell? I knew there had to be a way, so I googled and ran into this article. That showed me Get-Date and the AddYears() method. So I tried this:

    2022-01-05 10_26_03-C__Users_Steve

    That didn’t work well. I ended up storing the date in a variable and then things worked. However, I was bothered. I can pipe and combine things, why didn’t this work?

    Then I thought about something. I ought to enclose the Get-Date inside something. I tried parens, like this:

    (Get-Date).AddYears(11)

    That worked great. As I get more familiar with PoSh, I can figure more things out myself, and quicker.

    2022-01-05 10_28_06-C__Users_Steve

    SQLNewBlogger

    Once I figured out the way to do this, I spent about 5 minutes on this post. Actually 6 minutes with this part added.

    I took something I figure out and wrote this up quickly. I even added a great line at the end, that I figured it out and my work with the language is paying dividends.

    A great little type of post that would highlight your blog.

  • Searching dbatools–#SQLNewBlogger

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    I learned something new about dbatools. That’s using the Find-DbaCommand cmdlet, which is handier than Get-Command. I’ll show you why in this post.

    Searching PowerShell

    You can search for commands with Get-Command. If I search for “key” in the dbatools module, I’d run this:

    Get-Command *key* -Module dbatools

    This returns me a number of items. In fact, it returns 11 items.

    2021-11-20 11_56_58-C__Users_Steve (Admin)

    That’s not bad, and this is how I’ve often been looking for command when I’m coding. This is faster than going to the index page on the site.

    Find-DbaCommand

    I was reading the upcoming dbatools in a Month of Lunches, and saw the Find-DbaCommand listed. If I use this, and include “key”, I get different results.

    2021-11-20 11_59_14-C__Users_Steve (Admin)

    This might be more than I want, but I thought this was a neat addition to the dbatools module to help someone find commands quickly.

    It is definitely easier than going to dbatools.io and trying to search for a command in the command list.

    SQLNewBlogger

    I learned something, and I sketched out how to explain it to someone. I took 5 minutes to knock this post together. You could do the same thing, but expand on a way that you actually use this to find a command.

    Might teach someone how a skill. Might even teach someone that wants to interview you.