Adding Application Name to Invoke-SqlCmd2
In a previous post, I expressed some frustration over Invoke-SqlCmd
not setting an Application Name for its ODBC connection, leaving us with the generic .NET SqlClient Library when looking at active sessions in sp_who2
and sp_whoisactive
(and any other monitoring tool). Unfortunately, I can’t really do anything about Invoke-SqlCmd
aside from posting a suggestion on Connect or the Client Tools Trello board, but Invoke-SqlCmd2
has the same issue and that’s on GitHub. So, here we go!
In its current form, if a SqlConnection
object isn’t passed into Invoke-SqlCmd2
, the cmdlet does the following:
|
|
I decided to change this around so that it no longer uses string formatting, but instead a SqlConnectionStringBuilder
. I had a couple reasons for this:
- It will eliminate redundant code. There are several common elements in each of the
ConnectionStrings
above. If more complex logic is needed, there are potentially more copies of this ConnectionString kicking around. - It’s prone to copy/paste and other editing errors. If there’s a change that affects both versions of the ConnectionString and the developer just copies the line from one branch of the if statement to the other, code will be lost or invalid values will be substituted because of positioning.
With this in mind, I factored the common elements out to build the base of the ConnectionString, then added the remaining elements conditionally based on the cmdlet inputs.
|
|
Before going any further in adding support for inserting Application Name
into the ConnectionString
, I had to add a parameter to the cmdlet itself.
|
|
With that complete, I can now add it into the SqlConnectionStringBuilder
.
|
|
Because ApplicationName
is an optional parameter, I had to account for cases where it’s not specified by the caller. What the code above is doing is looking at the entire call stack and going back up to the very top to get the name of the script file that was run and ultimately called Invoke-SqlCmd2
. Finally, I extract the ConnectionString from the SqlConnectionStringBuilder
and assign it to the SqlConnection
’s ConnectionString property
.
|
|
My code changes complete, I reviewed my work and sent my first real pull request off to Warren (blog|twitter) as PR #7 for the module.