0

Below is code snippet from Powershell

$query =  "select MAX(convert(varchar(10),server_time,102))from status_history where status not like ('ERROR FETCHING%')" 
$servers = Invoke-Sqlcmd2 -query $query -ServerInstance "100.81.60.2" -Database "temp" -Username "temp" -password "temp"

I need to check in Powershell if the sql query contained in $query returns NULL. I tried using

if($servers.Column1 -eq NULL)

but it doesnt seem to work.

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
LOL
  • 101
  • 6
  • 17
  • try using `if($servers.Column1 -eq $null)` or just omit it: `if ($servers.Column1)` – Martin Brandl Mar 29 '16 at 13:20
  • 4
    Using `if ($servers.Column1)` is not a good idea in this context, because values like 0 or "" (empty string) also evaluate to `$false`. I'd try [`[DBNull]::Value.Equals()`](http://stackoverflow.com/a/22287263/1630171). – Ansgar Wiechers Mar 29 '16 at 13:27

1 Answers1

2

Compare the result to [DBNull]::Value:

if ([DBNull]::Value.Equals($server.Column1)) {
  ...
}

If $server.Column1 contains more than one value you may need to check each value individually.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328