Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Formatting the list output into columnar with colour ?

Posted on 2011-03-25
3
Medium Priority
?
445 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
[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
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 2000 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

This article will help to fix the below error for MS Exchange server 2010 I. Out Of office not working II. Certificate error "name on the security certificate is invalid or does not match the name of the site" III. Make Internal URLs and External…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…
Suggested Courses

704 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