Solved

Format Powershell Script as a Table

Posted on 2016-10-27
4
61 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 70

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

752 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