Solved

Format Powershell Script as a Table

Posted on 2016-10-27
4
40 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
  • 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 68

Accepted Solution

by:
Qlemo earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This article will help you understand what HashTables are and how to use them in PowerShell.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now