Solved

Formatting the list output into columnar with colour ?

Posted on 2011-03-25
3
434 Views
Last Modified: 2012-05-11
Hi,

I have the following script that I'd like to format it so that i can view it in columnar format and then highlighted the "STOPPED" services with red if any ?

how can I achieve this with Powershell ?

Thanks
#-------------------------------------------------------------------------
#pull all the Exchange Services status runing on the exchange 2007 Servers
#-------------------------------------------------------------------------
#
$Exchserver = Get-ExchangeServer
$ExchServices = (gwmi -computer $Exchserver -query "select * from win32_service where Name like 'MSExchange%' or Name like 'IIS%' or Name like 'SMTP%' or Name like 'POP%' or Name like 'W3SVC%'")
$Services = @()
	Foreach ($Service in $ExchServices){
		" "
		"Server  DNS   Name :" + $Service.__SERVER
		"Service Caption    :" + $Service.Caption
		"Service Start Name :" + $Service.Startname
		"Service Start Mode :" + $Service.StartMode
		"Service Start State:" + $Service.State
		"Service Status     :" + $Service.Status
		" "
	}

Open in new window

0
Comment
Question by:jjoz
  • 2
3 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 35222256
Try this. You can adjust the column widths in line 7.
function trim ([string]$s,[int]$i)
{
    if($s.length -gt $i){$s.substring(0,$i)}
    else{$s}
}

$columns = 17,28,20,18,19,14
$properties = "__Server", "Caption", "Startname", "Startmode", "State", "Status"
$caption = "Server  DNS Name", "Service Caption",  "Service Start Name", "Service Start Mode", "Service Start State", "Service Status"
$ofs = ""
$ExchServices = (gwmi -query "select * from win32_service where Name like 'a%'")  
$ExchServices | % -Begin {
                $i=0
                write-host [string] ($columns | % -process {
                    "{0,-$($columns[$i])} "  -f (trim $caption[$i] $columns[$i])
                    $i++
                    })
                } -process { 
                $service = $_
                $i=0
                write-host [string] ($columns | % -process {
                    "{0,-$($columns[$i])} "  -f (trim $service.($properties[$i]) $columns[$i])
                    $i++
                    }) -foregroundcolor $(if($service.state -eq "Stopped"){"red"}else{[console]::ForegroundColor})
                }

Open in new window

0
 
LVL 13

Accepted Solution

by:
soostibi earned 500 total points
ID: 35222263
Sorry, I left my test WMI query in. Here is the solution with your original WMI query.
function trim ([string]$s,[int]$i)
{
    if($s.length -gt $i){$s.substring(0,$i)}
    else{$s}
}

$columns = 17,28,20,18,19,14
$properties = "__Server", "Caption", "Startname", "Startmode", "State", "Status"
$caption = "Server  DNS Name", "Service Caption",  "Service Start Name", "Service Start Mode", "Service Start State", "Service Status"
$ofs = ""
$Exchserver = Get-ExchangeServer  
$ExchServices = (gwmi -computer $Exchserver -query "select * from win32_service where Name like 'MSExchange%' or Name like 'IIS%' or Name like 'SMTP%' or Name like 'POP%' or Name like 'W3SVC%'")  
$ExchServices | % -Begin {
                $i=0
                write-host [string] ($columns | % -process {
                    "{0,-$($columns[$i])} "  -f (trim $caption[$i] $columns[$i])
                    $i++
                    })
                } -process { 
                $service = $_
                $i=0
                write-host [string] ($columns | % -process {
                    "{0,-$($columns[$i])} "  -f (trim $service.($properties[$i]) $columns[$i])
                    $i++
                    }) -foregroundcolor $(if($service.state -eq "Stopped"){"red"}else{[console]::ForegroundColor})
                }

Open in new window

0
 
LVL 1

Author Closing Comment

by:jjoz
ID: 35224749
thanks man !
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

746 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

18 Experts available now in Live!

Get 1:1 Help Now