Output not showing correctly in Powershell

Oliver Fung 21 Reputation points
2023-01-02T22:49:23.987+00:00

When inputting parameters into this function it's displaying the objects as one long string instead of on multiple lines. Not sure what I'm doing wrong here.

275488-image.png

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Aung Zaw Min Thwin 306 Reputation points
    2023-01-03T03:08:22.267+00:00

    You passed two hostnames as array. Two options...
    Edit the function with loop to process each computernames, e.g. Foreach, or use pipeline.

    #Option one: using loop  
    function Get-ComputerInfo  
    {  
        param ($computername)  
        foreach ($cn in $computername) {  
            $bios = Get-WmiObject -ComputerName $cn -Class Win32_Bios  
            $OperatingSystem = Get-WmiObject -ComputerName $cn -Class Win32_OperatingSystem  
            $ComputerSystem = Get-WmiObject -ComputerName $cn -Class Win32_ComputerSystem  
              
            $CustomObject = New-Object -TypeName psobject  
            $CustomObject | Add-Member -MemberType NoteProperty -Name 'Computer Name' -Value $cn  
            $CustomObject | Add-Member -MemberType NoteProperty -Name 'Serial Number' -Value $bios.SerialNumber  
            $CustomObject | Add-Member -MemberType NoteProperty -Name 'Host Name' -Value $ComputerSystem.DNSHostName  
            $CustomObject | Add-Member -MemberType NoteProperty -Name 'Domain' -Value $ComputerSystem.Domain  
            Write-Output $CustomObject  
        }  
    }  
    Get-ComputerInfo -computername 'Server1', $env:COMPUTERNAME | Format-Table -AutoSize -Wrap  
    
    
    #Option two: using pipeline  
    #the computername must pass from pipeline  
      
    function Get-ComputerInfo  
    {  
        param ([Parameter(ValueFromPipeline=$true)]$computername)  
        $bios = Get-WmiObject -ComputerName $computername -Class Win32_Bios  
        $OperatingSystem = Get-WmiObject -ComputerName $computername -Class Win32_OperatingSystem  
        $ComputerSystem = Get-WmiObject -ComputerName $computername -Class Win32_ComputerSystem  
        $CustomObject = New-Object -TypeName psobject  
      
        $CustomObject | Add-Member -MemberType NoteProperty -Name 'Computer Name' -Value $computername  
        $CustomObject | Add-Member -MemberType NoteProperty -Name 'Serial Number' -Value $bios.SerialNumber  
        $CustomObject | Add-Member -MemberType NoteProperty -Name 'Host Name' -Value $ComputerSystem.DNSHostName  
        $CustomObject | Add-Member -MemberType NoteProperty -Name 'Domain' -Value $ComputerSystem.Domain  
        Write-Output $CustomObject  
    }  
      
    #pass the computername from pipeline  
    'Server1', 'localhost' | Get-ComputerInfo | Format-Table -AutoSize -Wrap  
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Chendrayan Venkatesan 11 Reputation points
    2023-01-03T16:14:08.877+00:00

    Try the below snippet

    function Get-ComputerInfo {  
        [CmdletBinding()]  
        Param  
        (  
            [Parameter(Mandatory = $true,  
                ValueFromPipeline = $true,  
                ValueFromPipelineByPropertyName = $true,  
                Position = 0)]  
            $ComputerName  
        )  
      
        Begin {  
        }  
        Process {  
            try {  
                $BIOS = Get-WmiObject -Class Win32_Bios -ComputerName $ComputerName -ErrorAction Stop   
                $OperatingSystem = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName -ErrorAction Stop  
                $ComputerSystem = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ComputerName -ErrorAction Stop  
                [pscustomobject]@{  
                    ComputerName = $ComputerName  
                    SerialNumber = $BIOS.SerialNumber  
                    Domain       = $ComputerSystem.Domain  
                    OSVersion    = $OperatingSystem.Version  
                }  
            }  
            catch {  
                $_.Exception.Message   
            }  
                  
        }  
        End {  
        }  
    }  
    $env:COMPUTERNAME , $env:COMPUTERNAME | Get-ComputerInfo   
    
    1. You should use CIM Instance instead of WMI - Refer https://learn.microsoft.com/en-us/powershell/module/cimcmdlets/get-ciminstance?view=powershell-7.3
    2. Exception handling of your choice.
    3. process block here avoids foreach loop.
    1 person found this answer helpful.

  2. Oliver Fung 21 Reputation points
    2023-01-03T05:21:25.123+00:00

    The foreach loop is giving me multiple lines but with both parameter inputs still as one long string instead of Server1 on one line and Localhost on another line.

    275582-image.png


  3. Rich Matheisen 47,901 Reputation points
    2023-01-03T16:08:02.93+00:00

    In addition to the answer given by @Aung Zaw Min Thwin you can also try this:

    function Get-ComputerInfo {  
        param ($computername)  
        $computername |  
            ForEach-Object {  
                $OperatingSystem = Get-WmiObject -ComputerName $_ -Class Win32_OperatingSystem     # Why is this here? The data are never used.  
                $ComputerSystem  = Get-WmiObject -ComputerName $_ -Class Win32_ComputerSystem  
                [PSCustomObject]@{  
                    'Computer Name' = $_  
                    'Serial Number' = (Get-WmiObject -ComputerName $_ -Class Win32_Bios).SerialNumber  
                    'Host Name'     = $ComputerSystem.DNSHostName  
                    'Domain'        = $ComputerSystem.Domain  
                }  
            }  
    }  
      
    Get-ComputerInfo -computername 'Server1', $env:COMPUTERNAME | Format-Table -AutoSize -Wrap  
    

    Using a hash to create a PSCustomObject is faster (and less error-prone) than using the older Add-Member cmdlet.

    0 comments No comments

  4. Oliver Fung 21 Reputation points
    2023-01-06T09:01:24.117+00:00

    Thank you RichMatheisen-8856, chenv and of course AungZawMinThwin-9152 for the replies. I understand where my mistake was now and appreciate the alternate ways of going about this. You guys have helped my Powershell journey. Thanks very much!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.