Tag Archives: SSMS2008

Invoke-SqlCmd throws exception when trying to set the database via parameter

This was properly doing my nut today! Basically I created a generic script to fix orphaned users and replaced the database with the standard SqlCmd variable syntax:

use $(DatabaseName);

When running in SSMS setting the variables like this:

:setvar DatabaseName MyDatabase

works fine, as it does when setting as a param to the sqlcmd.exe:

sqlcmd -i C:\PathToScript.sql -v DatabaseName=MyDatabase

however when using the PowerShell Invoke-SqlCmd Cmdlet from the SqlServer PowerShell extensions it throws an exception that gives you absolutely no clue as to why:

Invoke-Sqlcmd : Object reference not set to an instance of an object.
At C:\Users\mayesr\Desktop\PowerShellScratchPad.ps1:22 char:14
+ Invoke-Sqlcmd <<<< -InputFile "$g_ScriptLocation\FixUser.sql" -ServerInstance "localhost" -Verbose + CategoryInfo : InvalidResult: (localhost:PSObject) [Invoke-Sqlcmd], NullReferenceExcep tion + FullyQualifiedErrorId : ExecutionFailed,Microsoft.SqlServer.Management.PowerShell.GetScriptComm and

I eventually noticed that the Invoke-SqlCmd has a parameter that you can set the database with. Once that was set and the use statement removed, the script worked fine! Luckily it didn’t take me too long to notice but hopefully if you come across this post it will save you some time!!

UPDATE

After I thought I had fixed this I realised that it wasn’t the Database param at all and in-fact was any param! I eventually found out there is a bug in the MSDN docs (and/or code)!! The docs say that the following format should be used:

$MyArray = "MyVar1 = 'String1'", "MyVar2 = 'String2'"
Invoke-Sqlcmd -Query "SELECT `$(MyVar1) AS Var1, `$(MyVar2) AS Var2;" -Variable $MyArray

However this doesn’t work and the following should be used instead (remove the spaces around the ‘=’):

$MyArray = "MyVar1='String1'", "MyVar2='String2'"
Invoke-Sqlcmd -Query "SELECT `$(MyVar1) AS Var1, `$(MyVar2) AS Var2;" -Variable $MyArray

I just wish I’d scrolled down to the bottom of the page as other people had added comments saying this!! 😡

TextMate theme for SSMS 2005/2008

UPDATE: The SSMS 2008 version also works on SSMS 2008 R2!! 🙂

I’ve been using the the TextMate theme thanks to Rob Connery (See here)  on VS2005 and VS2008 for about a year now. (Previously I was using the DarkSide theme by Dave Reed (see here) but that was a little too Vibrant in areas and I found some of the blues quite difficult to read.)

There seemed to be no way of importing any styles or even changing the colours in SSMS2005/8 and found it almost painful to switch to the standard bright theme.

Luckily after a little digging on google I managed to find this post by Tomas Restrepo where he had actually written a tool to import the registry settings from VS2005 into VS2008. With a couple of mods to the code to point to VS2008 and SSMS2008 I was able to transfer most of the settings across. Unfortunately when I opened SSMS2008 it wasn’t quite what I had hoped as some of the text was still left as black so was effectively invisible. So I rolled up sleeves and stared digging about in the registry to perfect the theme.

Orginally I started off with this (for some reason LightScreen has made the colours a little duller than normal!! Edit: It was because it was setup to export as jpg!) :

screenshot.23

but wasn’t happy with the keywords still showing in white (“join” in the above example, but  also applies to “exists” and possibly a couple of others) so had a little tinker again and found the Registry setting required (SQL Operator Foreground). Using an unused colour from the original theme it came out like this:

screenshot.21

and now comes the dilema do I leave the colour set so the “join” keywords are and put up with it also changing the “=”, “(“, “*”, “.” etc to the modified colour as well.

The Registry settings for the 2 versions can be downloaded here (SSMS2005 is untested but should work):

I still want to update the theme so the output window is black with a green foreground like the original, but atm I can’t seem to see anything that looks like it would be the right key! look like I’ll have to suck it and see!! 😀

Let me know what you think!!

PS Please remember to take a back of you registry beforehand, and don’t come running to me if you break SSMS!!