i have a powershell script… well, i have several, but … that builds an object based on the output from a SQL query. That seems to fail nicely if the sql statement does not return an indexed reader. I mean if i am querying a table that does not exist in, for example, MS SQL 2005, i can work around it. Typically, i test for the version and then change the SQL command statement to something benign so i don’t get an error.
In the ‘good case’, i get back 7 columns. In the degenerate case, i only get back one. In the powershell script, i add a note property for a new PSObject for each returned value (hard coded) – and if there is no returned value, the add-member creates it with -value as $null.
I loop over a bunch of servers, append to the object, and bind the object to out-gridview.
So for the 2005 servers, i get the name of the server and nothing/little else from the server. My object contains a lot of blank columns. But i want to do better. Specifically, i want to post an explanatory comment — for example in column 7 (which is filled by $rdr[6]. So i ask if ($rdr[6] -eq $null), and if true, i assume that the SQL version is 2005, so i add-member -value “not in this version”. But this is done at the last step, and if i add-member, it complains that there already is a member with that name.
There are other ways to handle this (eq. in a if statement) but i just asked if the value of that last object was $null (empty) and if so, i changed it with $obj.column = “not in this version” and that worked fine. I still have to test for the MS SQL version, but there are no errors in the output and the gridview can contain additional information.
Incidentally, (out-gridview -title “the server name and version”) does what you expect – i get a grid and the title of that string so i don’t have to worry about which grid i am looking at from the output.