Powershell custom table

Posted on 2010-09-23
Last Modified: 2012-08-13

I am looking to use PowerShell to gather information about several hosts, and display the information in a table.  I have the logic down to gather the information, and sort the machines into 3 arrays based on the results, but can't figure out how to get them into a table type view.

Here the basic idea,

Script gathers list of hosts
Pings host, if no response add machine name to $noping
If it is pingable, searches for an active process, if the process is there add it to $running
If process is not there add it to $complete

This is all in a loop that checks every 5 minutes and all of that works fine if I just print the list lengthwise.

I would like to print it in 3 columns, i.e.

Completed          Running          No Pingy
Host1                 Host3             Host9
Host2                 Host99

Then when it runs again in the loop it will clear the screen and display the current status.

Can't think of a way to do it though.

Is there any simple way to accomplish a manual table with column1 containing the contents of an array/string, column2 the contents of a different array, etc?

Thank you.
Question by:omnipower321
LVL 70

Expert Comment

by:Chris Dent
ID: 33744066

A simple way? No. PS isn't built to produce that kind of output formatting. That means you'd have to write your own format, which is possible, just not entirely easy considering the example.


Author Comment

ID: 33745063
Yeah that is what I figured.  But I wanted to make sure before going through the nightmare.  I am working on creating a custom object with 3 properties, and it will print out the ammount of rows for the largest array count, and each object will contain the info for that line if needed.

$row1.col1 = $arr1[0]
$row1.col2 = $arr2[0]
$row1.col3 = $arr3[0]

Then I will just loop through the $rows to print it.  It's gonna be ugly, but I think it will work.

Expert Comment

ID: 33745081
do you want to write the data to a database because then every fiv minutes you could just write your three columns to a table.
OR to a csv file and then import it wherever you want

but just to be clear are you trying to do something like:

write-output "$completed,$running,$nopingy"
| out-file -filepath "\\servername\powershell\log_files\log.csv"


Author Comment

ID: 33745242
Just outputting to the screen directly, wanted to keep any DB or external files out of it, but csv may be another route, I will tinker and give it a try.
Why do Marketing keep bothering you?

Is your marketing department constantly asking for new email signature updates? Are they requesting a different design for every department? Do they need yet another banner added? Don’t let it get you down! There is an easy way to manage all of these requests...

LVL 12

Accepted Solution

GusGallows earned 500 total points
ID: 33746250
Ok, this is going to look a little complicated, but it really isn't.
First, I have three functions, one to add characters to a string to make the string a uniform size. this is important to making the columns line up. The other two functions are for the ping test and the process test. Replace the servernames with your list of computers, replace the column width (in my script it is set to 14) to whatever you want the columns to be sized too, and finally replace 'Outlook' with the process you are looking for. Wrap it in whatever you are using to run it every 5 minutes and you should be good to go.

Function Repeat-String([string]$str, [int]$repeat)
      $builder = new-object System.Text.StringBuilder
      for ($i = 0; $i -lt $repeat; $i++)

Function Ping-Host ($Server)
      $status=get-wmiobject win32_pingstatus -Filter "Address='$Server'" | Select-Object statuscode
      if($status.statuscode -eq 0)
            $res = $True
            $res = $False
      Return $Res
Function Get-RProcess ([String]$processName, [string]$Computer)
      $procres = [system.Diagnostics.Process]::GetProcessesByName($processName,$Computer)
      if ($procres -ne $null)
            $result = $True
            $result = $False
      Return $result
[Array]$NoPingList = $null
[Array]$RunningList = $null
[Array]$CompletedList = $null
$i = 0
$ColWidth = 14
$ServerList = @('Servername1','Servername2','Servername3','Servername4','Servername5','Servername6')
ForEach ($Server in $ServerList)
      $pingres = Ping-Host $Server
      If ($pingres -eq $true)
            $rprocres = Get-RProcess 'Outlook' $Server
            If ($rprocres -eq $True)
                  $Running = $Server
                  $RunningList = $RunningList + $Running
                  $Completed = $Server
                  $CompletedList = $CompletedList + $Completed
            $NoPing = $Server
            $NoPingList = $NoPingList + $NoPing
$CountRun = $RunningList.Count
$CountComp = $CompletedList.Count
$CountNoPing = $NoPingList.Count
$HiCount = $CountRun
If ($CountComp -gt $HiCount){$HiCount = $CountComp}
If ($CountNoPing -gt $HiCount){$HiCount = $CountNoPing}

$H1 = 'Completed'
$H2 = 'Running'
$H3 = 'NoPingy'
$h1len = $ColWidth - $H1.Length
$h2len = $ColWidth - $H2.Length
$h3len = $ColWidth - $H3.Length
$tH1 = Repeat-String " " $h1len
$tH2 = Repeat-String " " $h2len
$tH3 = Repeat-String " " $h3len
$Header = "$H1$tH1`t$H2$tH2`t$H3$tH3"

$R1 = Repeat-String "-" $ColWidth
$R2 = Repeat-String "-" $ColWidth
$R3 = Repeat-String "-" $ColWidth
$Row1 = "$R1`t$R2`t$R3"

for ($i = 0; $i -le $hiCount; $i +=1)
      If ($CompletedList -eq $Null)
            $clistlen = 0
            $CompletedList = $CompletedList + ""
            $clistlen = $CompletedList[$i].Length
      $lCom = $ColWidth - $clistlen
      $lc1 = Repeat-String " " $lCom
      If ($RunningList -eq $Null)
            $rlistlen = 0
            $RunningList = $RunningList + ""
            $rlistlen = $RunningList[$i].Length
      $lRun = $ColWidth - $rlistlen
      $lr1 = Repeat-String " " $lRun

      If ($NoPingList -eq $Null)
            $plistlen = 0
            $NoPingList = $NoPingList + ""
            $plistlen = $NoPingList[$i].Length
      $plistlen = $NoPingList[$i].Length
      $lPin = $ColWidth - $plistlen
      $lp1 = Repeat-String " " $lPin
      $Line = $CompletedList[$i] + $lc1 + "`t" + $RunningList[$i] + $lr1 + "`t" + $NoPingList[$i] + $lp1

Author Closing Comment

ID: 33746516
Thank you very much, that is exactly what I was looking for!

Expert Comment

ID: 33752085
That is lovely

Author Comment

ID: 33757098
Here is my final result using most of GusGallows' ideas.  I probably made it less elegant with the exclusion of funcations, and more verbose, but it makes sense to now.    This will be in a larger timed loop eventually.Thanks again!
$ErrorActionPreference = "SilentlyContinue"

$strH1 = "Running"

$strH2 = "No Ping"

$strH3 = "Complete"

$arrNoPingList = @()

$arrRunningList = @()

$arrCompletedList = @()

$arrResultsCtr = @()

$colComputers = GC .\Computers.txt

$intColumnWidth = ($colComputers | %{$_.Length} | Sort | Select -Last 1)

If($intColumnWidth -lt $strH1.Length){$intColumnWidth = $strH1.Length}

If($intColumnWidth -lt $strH2.Length){$intColumnWidth = $strH2.Length}

If($intColumnWidth -lt $strH3.Length){$intColumnWidth = $strH3.Length}

Write-Host -F Cyan -NoNewLine "$strH1 $(" " * ($intColumnWidth - $strH1.Length))`t"

Write-Host -F Cyan -NoNewLine "$strH2 $(" " * ($intColumnWidth - $strH2.Length))`t"

Write-Host -F Cyan "$strH3 $(" " * ($intColumnWidth - $strH3.Length))"

Write-Host -F Cyan "$("=" * $($intColumnWidth * 3 + 8))"

ForEach($objComputer in $colComputers){

	$intSpacesToAdd = $intColumnWidth - $objComputer.Length

	If(Test-Connection -ComputerName $objComputer -Quiet -Count 1){

		$objIsItRunning = $Null

		$objIsItRunning = Get-Process -ComputerName $objComputer Outlook, Notepad

		If($objIsItRunning){$arrRunningList += "$objComputer $(" " * $intSpacesToAdd)`t"}

		Else{$arrCompletedList += "$objComputer $(" " * $intSpacesToAdd)`t"}


	Else{$arrNoPingList += "$objComputer $(" " * $intSpacesToAdd)`t"}


$arrResultsCtr += $arrNoPingList, $arrRunningList, $arrCompletedList

$intTotalDataRows = $arrResultsCtr | %{$_.Count} | Sort | Select -Last 1

For($j = 0; $j -le $intTotalDataRows; $j++){

	If($arrRunningList[$j] -eq $Null){$strDataCol1 = "$(" " * ($intColumnWidth + 4))"}

	Else{$strDataCol1 = $arrRunningList[$j]}

	If($arrNoPingList[$j] -eq $Null){$strDataCol2 = "$(" " * ($intColumnWidth + 4))"}

	Else{$strDataCol2 = $arrNoPingList[$j]}

	If($arrCompletedList[$j] -eq $Null){$strDataCol3 = "$(" " * ($intColumnWidth + 4))"}

	Else{$strDataCol3 = $arrCompletedList[$j]}

	$strDataCol1 + $strDataCol2 + $strDataCol3


Open in new window


Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

15 Experts available now in Live!

Get 1:1 Help Now