Solved

Format Powershell Script as a Table

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 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

16 Experts available now in Live!

Get 1:1 Help Now