0

The following script will feed ConvertTo-Html with $null value.

Invoke-SqlCmd -ServerInstance xxxx "select top 0 1 A -- returns no row" |
  ConvertTo-Html -Fragment

It will generate the following html code. Is there a simple way (without introducing variable and if statement) to not generate the empty <table> but a text message if the input is null?

<table>
</table>

Or is there a way to let Invoke-SqlCmd return a string if empty?

ca9163d9
  • 27,283
  • 64
  • 210
  • 413
  • `ConvertTo-Html -Fragment` is doing what it is being told to do. Wrap `$null` in a table. Don't think you can change this behavior. What no `if`? You are asking to react to the null so a condition has to be established. You could also check the output to see if it matches `
    `
    – Matt Feb 05 '15 at 16:45
  • Not possible without an `if` statement because of the absence of [this](http://stackoverflow.com/questions/2416662/what-are-the-powershell-equivalent-of-bashs-and-operators), which would work in bourne shell – arco444 Feb 05 '15 at 17:17

1 Answers1

0

Don't think there is a way to get what you are asking for within the restricting criteria you are providing. The behaviour you are trying to prevent/account for is this

$null | ConvertTo-Html -Fragment
<table>
</table>

$null is something tangible that ConvertTo-Html is wrapping with table tags. If you want to react to this that requires some conditional statement.

 $table =  $null | ConvertTo-Html -Fragment
 If([string]$table -eq "<table> </table>"){Write-Host "Table is empty" -ForegroundColor Red} 
Matt
  • 45,022
  • 8
  • 78
  • 119