Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Format Powershell Script as a Table

Posted on 2016-10-27
4
Medium Priority
?
74 Views
Last Modified: 2016-10-27
How can I alter this script so that the output will be in table format?

$arServer = Get-Content -Path "C:\ServerList.txt"

foreach ($strServer in $arServer){

    $colltems =Get-WmiObject -class Win32_OperatingSystem -computername $strServer

foreach($sProperty in $colltems)
{
   
   Write-Host "Computer name; " $strServer
   Write-Host $sProperty.Description
   Write-Host $sProperty.Caption
   Write-Host $sProperty.OSArchitecture
   Write-Host $sProperty.ServicePackMajorVersion

}
}

Open in new window

0
Comment
Question by:Tim Ballin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Spencer Scherer
ID: 41862808
What about this?  Is this what you were looking for?  Sorry if I misunderstood.

$arServer = Get-Content -Path "C:\ServerList.txt"

foreach ($strServer in $arServer){

    $colltems =Get-WmiObject -class Win32_OperatingSystem -computername $strServer

foreach($sProperty in $colltems)
{
   
   Get-WmiObject -class Win32_OperatingSystem -computername $sProperty| format-table

}
}

Open in new window

0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 41862814
You need to create a custom PSObject, or select the properties:
Get-Content -Path "C:\ServerList.txt" |
% {
   $strServer = $_
   Get-WmiObject -class Win32_OperatingSystem -computername $strServer |
       Select @{n='Computer Name'; e={$strServer}}, Description, Caption, OSArchitecture, ServicePackMajorVersion
} | format-table -auto

Open in new window

2
 

Author Comment

by:Tim Ballin
ID: 41862817
Seems like you're on the right track, but here's what I get when I try to run that:

Get-WmiObject : Invalid parameter 
At line:10 char:4
+    Get-WmiObject -class Win32_OperatingSystem -computername $sProperty| format-t ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
 
Get-WmiObject : Invalid parameter 
At line:10 char:4
+    Get-WmiObject -class Win32_OperatingSystem -computername $sProperty| format-t ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
 
Get-WmiObject : Invalid parameter 
At line:10 char:4
+    Get-WmiObject -class Win32_OperatingSystem -computername $sProperty| format-t ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Open in new window

0
 

Author Closing Comment

by:Tim Ballin
ID: 41862819
Worked perfect!
0

Featured Post

Protect Your Retail Business and Reputatio

Wi-Fi access doesn't just impact your business & customer experience, it can also affect your security.  Join us for a webinar on Sept. 28th to learn more about the top threats and trends impacting retail today, and the key solutions to protecting retail networks and reputations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question