Solved

Formatting the list output into columnar with colour ?

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
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…
Suggested Courses

615 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