Category: Blog

  • Using a PoSh variable in a string- #SQLNewBlogger

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

    This is something I haven’t quite understood or used often, but I’ve been aware of it and wanted to learn more.

    A member at SQLServerCentral wanted to embed a value in a string, and was having issues. In this case, they had this code:

    $dt = get-date -format "_yyyyMMMdd_HHmss"
    Invoke-Sqlcmd -Query "SELECT * FROM [Sandbox].[dbo].[Customer]" -ServerInstance "Plato\SQL2017" |
    Export-Csv -Path E:\Documents\sql\$dt.csv -NoTypeInformation

    In this case, there was an error with the Export-Csv cmdlet, with a syntax issue near the period. I suspected this was some variable expansion that didn’t work.

    I found this post that helped me understand a bit more and decided to experiment a bit. Let’s try some things. First, I used to do this type of code:

    $dt = Get-Date –format “yyyyMMdd”
    write-host(“Today is “ + $dt)

    I then see this:

    2019-12-02 14_42_23-cmd - powershell

    However, I can use this code:

     write-host("Today is $dt")

    That gives me the same result. Apparently, I can include the variable in the string and it gets expanded. This works with just a string, as shown here:

    PS C:\Users\Steve> write-host("Today is $dt.csv")
    Today is 20191202.csv
    PS C:\Users\Steve>

    Not the error I expected, but this makes more sense with a value that’s needed in a parameter. The blog helps explain this with the following code:

    PS C:\Users\Steve> $directory = Get-Item 'c:\windows'
    PS C:\Users\Steve> $message = "Time: $directory.CreationTime"
    PS C:\Users\Steve> $message
    Time: C:\windows.CreationTime
    PS C:\Users\Steve>

    An issue. However, if I use the expression evaluation of $() inside, I get this:

    PS C:\Users\Steve> $message = "Time: $($directory.CreationTime)"
    PS C:\Users\Steve> $message
    Time: 09/15/2018 00:09:26
    PS C:\Users\Steve>

    That’s the trick I needed for Export-Csv. I used this code in the last line:

    Export-Csv -Path E:\Documents\sql\$($dt).csv –NoTypeInformation

    And the code worked as expected.

    There’s likely more I should know, but I will start to use varaiables inside strings when I just need the value of the variable as a string. If I need this to better work with some property, method, or parameter value, I’ll use $() around the variable.

    SQLNewBlogger

    This post was about 20 minutes of me experimenting with a few things and slowly working out how some variables worked. I somewhat wrote this as I was experimenting, adding in the code that ran.

    A good example of writing while learning. You could do this on your blog as you learn to work through some code or a feature.

  • Unprepared for Travel

    I’ve had a month off from travel, which has been nice. It’s been an interesting time catching up on things at home, and a nice break from the disruption to my schedule that travel entails. However, all good things come to an end, and last week I headed out on Thursday morning for another trip, this time to Slovenia and the UK.

    I found myself woefully unprepared. A list of things I’ve forgotten:

    1. two pairs of bluetooth headphones
    2. phone charger cable
    3. usb adapter for phone cable
    4. two pairs of wired headphones
    5. external mouse
    6. HDMI wireless adapter
    7. gloves
    8. belt
    9. naproxen

    At least I remembered to pack my laptop charger, wallet, and passport, something I haven’t always done. I also did get sneakers packed for the gym.

    In the last month, I did spend a few nights in the mountains, and had some long day trips around Denver, during which I’d slowly moved a few things from my laptop bag, or roller bag, to use for a few hours. In the past I’ve been good about putting those things back in bags right away, so I’d be ready to travel. I usually keep my main luggage and laptop bag ready to go, since I travel so much. Often I can just throw a few changes of clothes in the bag and leave.

    With a busy week last week, I was slightly worried I might have forgotten something, but I was in a hurry Thursday morning as I packed and didn’t double check myself. I did look for the wallet and passport, since with those I can likely replace anything I need.

    When I got to the airport, I realized that I’d left the bluetooth headphones charging on my desk. I’d used one pair for a meeting while cooking, and grabbed another for the gym last week and didn’t put them back. Worse, I’d taken some of the wired headphones from a jacket and bag and used them at different times, getting lazy about putting them back. Same for the phone charger cable. I used that while cooking, and it’s sitting in the kitchen now.

    These are minor issues, and I can certainly survive. Fortunately I’ve kept a spare pair of wired headphones and charging cable in my luggage for emergencies and pulled them out. I survived Slovenia without gloves, though it wasn’t that cold. I did have to buy a belt and some pain meds for an injured wrist, and I can live without the mouse.

    This is the same type of thing I’ve seen in an office at work, where myself or someone deviates from a routine, gets lazy and then starts taking more shortcuts to get around the other shortcuts I’ve taken. I need to stick to a routine, and certainly adhere to any expectations I’ve set for myself and others. Hopefully I’ll remember to do this in the new year, with quite a bit of time off.

  • A Smooth Laptop Setup

    A little over a week ago, I got a new laptop. This was late on a Sunday afternoon, and I went home to spend time with me wife. After dinner, we relaxed with a little TV time. While trying to decide what to watch, I opened the laptop and started the Windows setup process. This post talked about how smoother and simple things were for me, though certainly not seemless as I’ve had on OSX machines. Still, it went well.

    Windows

    The first part of getting a new Windows machine going is usually the account. I have a Microsoft account I use for a few things. It’s actually an old Hotmail account, but it’s worked well for me.I entered this, and it linked to a few things I already had set up, including my OneDrive. This bootstrapped quite a bit of the process.

    I then checked Windows Update (nothing to apply) and upgraded to Windows 10 Pro. Once this was done, I opened the Windows Store and applied a few updates from there. While this was running, I did two things.

    Chrome

    I prefer Chrome as a browser, and I keep some settings linked to this browser, like bookmarks, some throwaway user/pwds, and settings. I usually start by opening Edge on a new install and going to Chrome and installing it. I could to this separately, but it’s usually my first step.

    Chocolatey

    This is my go-to install system. It’s yum or apt-get for Windows and I’ve used it for close to a decade now. I go to the website, and grab the script. Since I usually don’t have DropBox yet, and that’s where I have lots of stuff, I start here. Run this in an admin command prompt that’s got PowerShell running:

    Set-ExecutionPolicy AllSigned or Set-ExecutionPolicy Bypass -Scope Process
    Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

    Those two things get the package manager running. Once this is done, I usually start grabbing stuff I know I need. I used to have a long list of things to install, but since my needs change over time, I usually go with this:

    choco install dropbox -y
    choco install evernote -y
    choco install greenshot –y
    choco install spotify –y

    These are the main things I want, and really need. While these are running, I usually go get the o365 portal from work and start that install, because it’s long.

    With every laptop, I somewhat change the things I install. I posted I was doing this and Aaron Nelson dropped me a link to his gist of database pro software: https://gist.github.com/SQLvariant/d29ffd1e9905992318b4585c83399328.

    This is a great list, and I immediately added some of these items. I don’t use all of these, but I use many.

    • SSMS
    • ADS
    • Git
    • VSCode
    • PowerBi
    • VS
    • Python
    • Sublime Text
    • Slack
    • Teams
    • Docker Desktop

    I do need SQL Server, but I held off since I wasn’t sure if I want to install it or just run containers. I’m debating this as I write this. I’m really tempted to skip an install and set up folders for holding db/log files at specific versions.

    Once these tools were installed, I added the Redgate Toolbelt, missing that in Aaron’s list. I saw Kendra tried this and it worked, but I was half paying attention and just running choco commands while watching a movie.

    Just in glancing at the machine on a dresser a few times and typing a few commands had most of the machine set up in the space of a 2 hour movie. It was Holiday in the Wild, if you’re wondering. An enjoyable movie.

    More to do, but really typing a few commands, I had most software installed that I need.

    Now I just need to decide what to do about SQL Server.

  • Prompt in ADS

    Ever since VS Code and Azure Data Studio came out, people have been asking for SQL Prompt to port over. I’ve been right there with you, though unsure of the value, given how UI dependent Prompt is for users.

    Things have changed.

    sql-prompt_early-access_social-graphic_1200x630

    There’s an EAP program, with an early look at the formatting part of Prompt in ADS.

    I’ve been using this for a few weeks, and it’s different, but it works well. All my styles from SSMS are ported and linked, and it’s a quick set of keystrokes, albeit different ones, to format code.

    I’m curious to see what others think, so join the program if you want formatting in ADS and are willing to give some feedback.