user847990
user847990

Reputation:

PowerShell formatting multiple outputs from one function

My best guess is this has something to do with the default output methods used in the Powershell console, but I am at a wall...

I am working on a script that contains a data table of default values found for configuration options in a SQL Server instance. I then have a function that goes and pulls the current in use values of those same configuration options.

I want to output two sets of information from this function: (1) filters specific information out of the current values itself and (2) filters specific information comparing the default values to the current values. So it would look something similar to this (leaving out the data table stuff):


$var1 = $current | where {my filters} | 
   Select @{label='name';Expression{stuff}}, 
   @{label='value_in_use';Expression{stuff}},
   @{label='value';Expression{stuff}}

$var2 = foreach ($o in $tblDefault) {
   $current | where {my filters} | 
   Select @{label='name';Expression{stuff}},
   @{label='value_in_use';Expression{stuff}},
   @{label='default';Expression{stuff}}

Write-Host "This is a message"
$var1
Write-Host "This is a message"
$var2
}

My issue is with the output of the two variables.

  1. The third column for $var2, "default", is not showing.
  2. As well, the output shows only the column headings at the top of the output for $var1. enter image description here Commenting out the first variable call this is the output that I should see for the second variable: enter image description here PowerShell does not even appear to see the additional columns from the second variable, when calling them both. I can do a Get-Member on the function and it shows the property names only for the first variable. enter image description here

What am I supposed to do in order to force this into separate objects (meaning column headings for both sets?

Just pasting this together with MSPaint this is the output I expected, and would like: enter image description here

Upvotes: 1

Views: 1530

Answers (1)

jon Z
jon Z

Reputation: 16626

Adding out-default when outputting your variables should fix your problem:

Write-Host "This is a message"
$var1
Write-Host "This is a message"
$var2 | out-default

Why? Because you are forcing out-default to look at the first item of $var2, instead of having it look at the first item of the entire stream (returned by your function). This blog post explains how powershell formatting and outputting works.

Upvotes: 2

Related Questions