Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Formatting the list output into columnar with colour ?

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
With so many activities to perform, Exchange administrators are always busy in organizations. If everything, including Exchange Servers, Outlook clients, and Office 365 accounts work without any issues, they can sit and relax. But unfortunately, it…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

972 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