Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: Function output – results are correct, but format is strange

$
0
0

So, let’s look at a couple of things.

First, consider changing your Get-WmiObject approach.

$variable = Get-WmiObject ...

Without the Out-Null is a lot more effective and easier to read. Performs better, too.

The main problem you’ve got is that you’ve planted a Format-Table in your code. Don’t do that. When you only send one object at a time to the formatting system, it gets a bit confused, which is what you’re seeing. It’s treating each object as a new thing to be formatted, so you get table headers every time.

Get rid of the Format-Table and just write $obj to the pipeline. Enclose that entire script of yours in a function, named something like Get-SysInfo. At the bottom of the script:

Get-SysInfo | Format-Table -AutoSize

That way the FUNCTION produces a set of objects to the pipeline, and nothing else. You then call that function, and pipe its output to Format-Table. You’ll be a lot happier with the results.

A given unit of work – a script, or a function – should not attempt to both output objects AND control their formatting. What I’ve suggested gives you TWO units of work: A function that produces the output, and then the script (which contains the function) that controls the formatting of that output.


Viewing all articles
Browse latest Browse all 13067

Trending Articles