I searched for this on here and google and didn't see an exact answer to my question, so sorry if it's been asked and answered before. If I run a query using Invoke-Sqlcmd and put it in a variable:
$results = Invoke-Sqlcmd -Query $query -ServerInstance $QueryServer -Database $QueryDB;
and I get multiple rows back, I see that $results has a type of System.Array. I can get a count of the number of returned results by using $results.Length.
However if I get only 1 row back, the Type is Syste.Data.DataRow and $results.Length is NULL
I've gotten around this by creating a new variable called ResultsLen and doing the following:
$resultsLen = $results.<field in query>.Length
$resultsLen += $results.Length
but this doens't really work, as the $results.<field in query>.Length is a count of the number of characters in the returned field. this works for what I need...which is just to run a IF statement to only run some code if there are 1+ records returned in the query...but I'm wondering if there is a better way of doing it?