foreach ($service in $services){
IF($service.Status -eq “Running”) {write-host -ForegroundColor Green $service}
Else { Write-host -ForegroundColor Red $service }
}
<hr />
I get the following results which does not look nice. I would like it to be have heading like.
Machine Name Status Name
@{MachineName=SERVERNAME; Status=Running; Name=FTA; DisplayName=Skype for Business Server File Transfer Agent}
@{MachineName=SERVERNAME; Status=Running; Name=LYNCBACKUP; DisplayName=Skype for Business Server Backup Service}
@{MachineName=SERVERNAME; Status=Running; Name=MASTER; DisplayName=Skype for Business Server Master Replicator Agent}
@{MachineName=SERVERNAME; Status=Running; Name=REPLICA; DisplayName=Skype for Business Server Replica Replicator Agent}
@{MachineName=SERVERNAME; Status=Running; Name=RTCASMCU; DisplayName=Skype for Business Server Application Sharing}
@{MachineName=SERVERNAME; Status=Running; Name=RTCATS; DisplayName=Skype for Business Server Audio Test Service}
@{MachineName=SERVERNAME; Status=Running; Name=RTCAVMCU; DisplayName=Skype for Business Server Audio/Video Conferencing}
@{MachineName=SERVERNAME; Status=Running; Name=RTCCAA; DisplayName=Skype for Business Server Conferencing Attendant}
@{MachineName=SERVERNAME; Status=Running; Name=RTCCAS; DisplayName=Skype for Business Server Conferencing Announcement}
@{MachineName=SERVERNAME; Status=Running; Name=RTCCLSAGT; DisplayName=Skype for Business Server Centralized Logging Service Agent}
@{MachineName=SERVERNAME; Status=Running; Name=RTCCPS; DisplayName=Skype for Business Server Call Park}
@{MachineName=SERVERNAME; Status=Running; Name=RTCDATAMCU; DisplayName=Skype for Business Server Web Conferencing}
@{MachineName=SERVERNAME; Status=Running; Name=RTCHA; DisplayName=Skype for Business Server Health Agent}
@{MachineName=SERVERNAME; Status=Running; Name=RTCIMMCU; DisplayName=Skype for Business Server IM Conferencing}
@{MachineName=SERVERNAME; Status=Running; Name=RTCMEDSRV; DisplayName=Skype for Business Server Mediation}
@{MachineName=SERVERNAME; Status=Running; Name=RTCPDPAUTH; DisplayName=Skype for Business Server Bandwidth Policy Service (Authentication)}
@{MachineName=SERVERNAME; Status=Running; Name=RTCPDPCORE; DisplayName=Skype for Business Server Bandwidth Policy Service (Core)}
@{MachineName=SERVERNAME; Status=Running; Name=RTCRGS; DisplayName=Skype for Business Server Response Group}
@{MachineName=SERVERNAME; Status=Running; Name=RtcSrv; DisplayName=Skype for Business Server Front-End}
@{MachineName=SERVERNAME; Status=Running; Name=RTCXMPPTGW; DisplayName=Skype for Business Server XMPP Translating Gateway}
Are you wanting the entire line to be green or red? Why even check if the service is running if you want to have the ones not running show up in red? Lastly, is the ultimate goal for this output is to be consumed by a user watching the console? Also, the first post in the general forum can help you to better utilize the site and the marvelous minds that contribute. I think you should check it out!
Maybe the formatting got screwed up from copy/paste. Try putting it into the powershell ISE, highlighting all, and pressing shift + tab until all lines are aligned to the left. Then try running it.
You can always check parts out alone. Try running this alone
It does not make any sense to colorize your console output when you actually want to send this info by mail. These colors will not make it through. You would need to use HTML format and create some colorized text there. You may get more info about this in the e-book “Creating HTML Reports in Windows PowerShell” what’s availabe here on this site.
Since the services with the status “running” are actually good and do not need any action why not limitting the output to “bad” (stopped) services and send only these by mail? Another thought you may think about is the startup type. Some services do not need to be running all the time. So when a service has the startup type “manual” it is ok when it has the status “stopped” and do not need any action.
Regardless of all that there are many examples of solutions for task like this already available even here on Powershell.org or in the PowershellGallery - so no need to re-invent the wheel again. Make your life easier and use your fevorite search enginge.
As pointed out by another user your code suggests you’re trying to format the output in the console, but you’ve said your goal is to send a ‘colourized’ report via email. You seem to be a few steps behind but I wanted to give an example of how you could accomplish this anyway.
Let’s first explain the steps in what you will need to do in your final script.
Gather service information (from local or remote hosts) with only the properties you wish to output
Convert that output into an HTML fragment
Convert the resultant HTML fragment into XML
Use logic and XML methods to set the 'class' attribute appropriately based on the service status
Create the css that we will insert into the head of the final output HTML which we intend to send by email
Create a final HTML output that will become the body of your HTML-formatted email message
I am making some assumptions based on my experience with formatting in these HTML email reports.
Yeah I overlooked that. That’s why initially I asked if the intended consumer is a human at a console. Write-Host will only output to the console so you won’t be able to email or do anything other than look at the output. If you do need colorization in email, I would look into formatting the email body as HTML and use HTML to adjust appearance.
I have managed to output the powershell to html but need to set the color status to GREEN if it is running and Red when Stopped.
Any ideas? nearly cracked this one
Thank you for those links. They really helped. I am stuck on the following script i have created. When i run the script, the services which are stopped get removed from the list and do not get highlight RED. any ideas?
###Grabbing skype services that are running and stopped###
$servername = "SPSKF01"
$services = Get-Service -ComputerName $servername | where {($_.DisplayName -like "Skype*") -and ($_.Status -eq "Running")} | Select-Object MachineName, Status, Name, DisplayName | ConvertTo-Html -Title "Skype Services Alert" -Body "Hello TDS Deskside Support team,<br />
<br />
Please see the below Skype Services and their current status.<br />
<br />
Please ensure all skype services highlighted in red have a ticket raised in Marval and actioned through to resolution in a prompt manner.<br />
<br />
" -Property MachineName,Status,Name,DisplayName |
foreach {if($_ -like "*Running*"){$_ -replace "", ""}elseif($_ -like "*Stopped*"){$_ -replace "", ""}else{$_}} > c:\data\SkypeServices.html
###Grab results and send them to email address###
$body = [System.IO.File]::ReadAllText('C:\data\SkypeServices.html')
Send-MailMessage -Body $body -BodyAsHtml -From “” -To “” -Subject “Skype Services Alerts” -SmtpServer ""