1

I'm having some issues getting some info to write to the console before Read-Host. Let me throw out a simplified example.

Function Add-Build {
[CmdletBinding()]
Param ([Parameter(Mandatory=$True,Position=1)][String]$Build
      ,[Parameter(Mandatory=$False,Position=2)][System.Nullable``1[[System.Int32]]]$VersionID
      ,[Parameter(Mandatory=$False,Position=3)][String]$BuildDescription
)

    Write-Host -BackgroundColor DarkYellow "Adding SQL Build $($Build)"


IF ($VersionID -eq $null)
{
    Get-SqlVersions | Out-String


    $VersionID = Read-Host -Prompt "SELECT Version (Enter To Skip)" | % { IF ($_ -eq '') {$null} ELSE {$_}}
    }
}

FUNCTION Test-Function {

   $BuildID = (Get-BuildID -Build "11.0.3156.0").ToString()
}

If I call Add-Build directly then the Get-SqlVersions | Out-String output before the Read-Host. If I call Test-Function though the Get-SqlVersions no longer outputs to the console at all. Get-SqlVersions makes a SQL proc call and the output is a couple Datarows.

Is there a way to ensure the Get-SqlVersions data shows up when calling Test-Function?

MrTCS
  • 177
  • 2
  • 8

3 Answers3

3

Make it explicitly output to the host.

$GetSQL = Get-SqlVersions | Out-String
Write-Host $GetSQL
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
0

Could you please store Get-SqlVersions | Out-String; in a Variable and display that. I think that should work.

$versions = Get-SqlVersions | Out-String; 
$versions
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
0

I know this is old, but I stumbled on it and can't help but contribute.

The problem is here:

Get-SqlVersions | Out-String

Change it to this:

Get-SqlVersions | Out-Host

I did some quick looking around, and Out-String seems to collect and prepare things for displaying. Out-Host just does it.

  • After some measuring I've found out that Out-Host performs way slower then Out-String. No surprise on that, but here are the stats: For a 10k array, Out-String outputs it on average in 114 ms. Out-Host outputs it on average in 833 ms. For completeness, Out-Null outputs it on avg in 14 ms. Avg based on 20 samples – IT M Jan 06 '21 at 15:29