Function output - results are correct, but format is strange

This topic contains 2 replies, has 2 voices, and was last updated by  stephenmbell 3 years, 11 months ago.

  • Author
  • #10031


    I have a (soon to be) advanced function that I am writing to gather some system information from various machines in our environment (name, os, service tag, disk, ram, etc).

    I am getting all of the data back that I want to get, it just isn't displaying the way that I am expecting. Here is part of my code...

            foreach($c in $ComputerName)
                Get-WmiObject -Class Win32_LogicalDisk -ComputerName $c -Credential $Credential -Filter "DriveType=3" -OutVariable disk | Out-Null
                Get-WmiObject -ComputerName $c -Class Win32_BIOS -Credential $Credential | 
                    Select -ExpandProperty SerialNumber -OutVariable serviceTag | out-null    
                Get-WmiObject -Class Win32_OperatingSystem -ComputerName $c -Credential $Credential | 
                    Select -Property Caption,CDSVersion,OSArchitecture,LastBootUpTime -OutVariable os | Out-Null
                Get-WmiObject -Class Win32_ComputerSystem -ComputerName $c -Credential $Credential | 
                    Select -Property Caption, Manufacturer, Model, NumberOfProcessors,TotalPhysicalMemory -OutVariable sys | Out-Null
                [int]$amountOfDisk = 0
                foreach($d in $disk)
                    $amountOfDisk += $d.size / 1gb -as [int]
                $prop = [ordered]@{
                    'ComputerName' = $c 
                    'Manufacturer'= $sys.manufacturer
                    'Model'= $sys.model 
                    'ServiceTag' = $serviceTag
                    'OS' = $os.caption
                    'Architecture' = $os.OSArchitecture
                    'Processors'= $sys.NumberOfProcessors
                    'Memory (GB)'= $sys.TotalPhysicalMemory / 1gb -as [int]
                    'Disk (GB)' = $amountOfDisk#>
                $obj = New-Object -TypeName PSObject -Property $prop
                write-output $obj | Format-Table -AutoSize 

    I don't know why I am getting headings for each object returned.. whether the -Autosize switch is put on the Format-Table or not. I have attached an image to explain.

    Any help or explanation would be greatly appreciated.

  • #10033

    Don Jones

    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.

  • #10038


    Thanks for the response. I implemented the changes you suggested and I am seeing what I am looking for. I was confused initially because I had a similar script from the MVA Jumpstart series, and when I ran this script – the output was, by default, showing as a table.

    Your suggestion certainly helped clear it up.

    Thanks again

You must be logged in to reply to this topic.