Search code examples
powershelltabular

Getting format-table to work correctly with multiple sources


I want to check for specific services for a few specific servers and want the output to show on the same formatted table. I've only been able to create multiple tables, or to show only the last table formatted the way I want to. My intention is to show all on the same table.

Get-Service "ServiceA", "ServiceB", "ServiceC" -ComputerName SERVER1   
Get-Service "ServiceD", "ServiceE", "ServiceF" -ComputerName SERVER2 |
    Format-Table -Property MachineName, Status, Name, DisplayName -Auto

How do I include SERVER1 and SERVER2 on the same formatted table? The example above will only show me formatted table for the SERVER2?

Other way I've tried was to

Get-Service "ServiceA", "ServiceB", "ServiceC" -ComputerName SERVER1 |
    Format-Table -Property MachineName, Status, Name, DisplayName -Auto
Get-Service "ServiceD", "ServiceE", "ServiceF" -ComputerName SERVER2 |
    Format-Table -Property MachineName, Status, Name, DisplayName -Auto

but that way there's two different tables created, and not all the info in only one like i would like to.

I need to check differente services on six different servers, but with just two I think is enough to exemplify my difficulties on this script.


Solution

  • If you want them both to appear in one table, you need to send all of the results over to Format-Table at the same time. If you call Format-Table twice, you're going to get two separate tables.

    Fortunately, PowerShell makes it really easy to store the results of a command in a variable, and then use that later.

    What you need to do is make a variable to hold the results, and then store all of your Get-Service commands inside of it, like so:

    #take the output and store it in $services
    $services = get-service bits,winrm -computername ServerA
    #add the output of the next to $services as well
    $services += get-service AdobeARMservice,ALG -computername ServerB
    
    #finally, make one big table to display it all
    $services |Format-Table -Property MachineName, Status, Name, DisplayName -auto
    
    MachineName  Status Name            DisplayName                              
    -----------  ------ ----            -----------                              
    ServerA     Running bits            Background Intelligent Transfer Service  
    ServerA     Running winrm           Windows Remote Management (WS-Management)
    ServerB     Running AdobeARMservice Adobe Acrobat Update Service             
    ServerB     Stopped ALG             Application Layer Gateway Service    
    

    Before you go too far down this rabbit hole, keep in mind that Format-Table is ONLY meant for viewing stuff in the console. You can't take a table made with FT and then send export it as a .csv, for instance. If you're OK with just viewing it in the console though, this should work.