Solved

Formatting the list output into columnar with colour ?

Posted on 2011-03-25
3
435 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

919 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

17 Experts available now in Live!

Get 1:1 Help Now