Unable to get a nice clear output HELP :-(

Welcome Forums General PowerShell Q&A Unable to get a nice clear output HELP :-(

Viewing 19 reply threads
  • Author
    Posts
    • #217287
      Participant
      Topics: 4
      Replies: 10
      Points: 51
      Rank: Member

      $servername = “SERVER NAME”
      $services = Get-Service -ComputerName $servername | where {($_.DisplayName -like “Skype*”) -and ($_.Status -eq “Running”)} | Select MachineName, Status, Name, DisplayName

      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}

    • #217290
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      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!

    • #217293
      Participant
      Topics: 4
      Replies: 10
      Points: 51
      Rank: Member

      Thanks for your input Doug.

      The goal is to get a list of Skype services that are UP=Green and services are down=RED and send via email to a distribution list

      Yes i want the whole line to appear in the colour that the status is provided.

      I have checked in a few forums but with no luck 🙁

       

    • #217296
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      OK well your current code will not get any services that are stopped due to

      -and ($_.Status -eq “Running”)
      
      

      Definitely remove that if you want to see anything but green. Here is one way you could accomplish your goal

      $services = Get-Service -ComputerName $servername -Include *skype* |
          Select-object -property machinename, Status, Name, DisplayName | Sort-Object -Property machinename | ForEach-Object {
              $output = "Server: {0}    Service: {1} ({2})    Status: {3}" -f $_.MachineName,$_.displayname,$_.name,$_.status
              IF($_.Status -eq “Running”){
                  write-host -ForegroundColor Green $output
              }
              Else{
                  Write-host -ForegroundColor Red $output
              }
          }
      
      

      Oh and you can remove the $services = part because it will be assigned nothing since write-host does not write to the pipeline.

      I hope this helps.

      • This reply was modified 1 month, 3 weeks ago by Doug Maurer.
    • #217305
      Participant
      Topics: 4
      Replies: 10
      Points: 51
      Rank: Member
      $servername = "SKYPE SERVER"
      $services = Get-Service -ComputerName $servername -Include *Skype* |
      
      Select-object -property machinename, Status, Name, DisplayName | ForEach-Object {
              $output = "Server: {0}    Service: {1} ({2})    Status: {3}" -f $_.MachineName,$_.displayname,$_.name,$_.status
              IF($_.Status -eq “Running”){
                  write-host -ForegroundColor Green $output
              }
              Else{
                  Write-host -ForegroundColor Red $output
              }
          }
      

      NO JOY DIDN’T WORK

    • #217311
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      Looks like you got an extra blank line from copying/pasting. Line 3 is a blank line that does not belong there.

    • #217314
      Participant
      Topics: 4
      Replies: 10
      Points: 51
      Rank: Member
      $servername = "SPSKF01"
      $services = Get-Service -ComputerName $servername -Include *skype* |
      Select-object -property machinename, Status, Name, DisplayName | Sort-Object -Property machinename | ForEach-Object {
      $output = "Server: {0}    Service: {1} ({2})    Status: {3}" -f $_.MachineName,$_.displayname,$_.name,$_.status
      IF($_.Status -eq “Running”){
      write-host -ForegroundColor Green $output
      }
      Else{
      Write-host -ForegroundColor Red $output
      }
      }

      Tried that with no luck. servername is my actual server what its called.

    • #217317
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      It works so it’s gotta be one of a few problems.

      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

      Get-Service -ComputerName $servername -Include *Skype*
      
      

      just as a sanity check to make sure any services are being returned. Or if maybe you’re getting an error. Good luck.

    • #217374
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      The goal is to get a list of Skype services that are UP=Green and services are down=RED and send via email to a distribution list

      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. 😉

    • #217404
      Participant
      Topics: 1
      Replies: 114
      Points: 121
      Rank: Participant

      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.

      1. Gather service information (from local or remote hosts) with only the properties you wish to output
      2. Convert that output into an HTML fragment
      3. Convert the resultant HTML fragment into XML
      4. Use logic and XML methods to set the ‘class’ attribute appropriately based on the service status
      5. Create the css that we will insert into the head of the final output HTML which we intend to send by email
      6. 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.

      https://gist.github.com/peterinhk/6237d2cced1618c9d88ad7305e2951c7

      This would only work for the local machine but I will leave it up to you to modify where necessary to fit your needs.
    • #217509
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      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.

    • #217548
      Participant
      Topics: 12
      Replies: 1622
      Points: 2,560
      Helping Hand
      Rank: Community Hero

      If you are looking for something quick and easy, you can re-write this quickly as well:

      https://en.it1352.com/article/ba4610157cb04343b5374a0e3fe7ae83.html

    • #217770
      Participant
      Topics: 4
      Replies: 10
      Points: 51
      Rank: Member

      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
      ——————————————————————————————————–

      $servername = "SPSKF01"
      Get-Service -ComputerName $servername | where {($_.DisplayName -like "Skype*") -and ($_.Status -eq "Running")} | Select MachineName, Status, Name, DisplayName | ConvertTo-HTML | Out-File -Filepath C:\data\DailyChecks.html
      
      Invoke-Expression C:\data\DailyChecks.html
      Get-Service | ConvertTo-Html -Property MachineName,Status,Name,DisplayName | Out-File -FilePath PSDrives.html
      
      $Header = @"
      <style>
      TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
      TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
      </style>
      "@
      Get-Service | ConvertTo-Html -Property  MachineName,Status,Name,DisplayName -Head $Header | Out-File -FilePath DailyChecks.html
      
    • #217776
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      …. but need to set the color status to GREEN if it is running and Red when Stopped.

      Any ideas?

      Please read the book I linked above. It has everything you need.

    • #217785
      Participant
      Topics: 4
      Replies: 10
      Points: 51
      Rank: Member

      Sorry this didn’t help 🙁

    • #217794
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP
    • #217791
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP
    • #218649
      Participant
      Topics: 4
      Replies: 10
      Points: 51
      Rank: Member

      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 ""
      
    • #218661
      Participant
      Topics: 10
      Replies: 38
      Points: 173
      Helping Hand
      Rank: Participant

      Someone mentioned earlier – this piece of code will filter out services that are not “Running” from your results.

      -and ($_.Status -eq “Running”)

      If you want to show services that are not Running, remove that from your code.

    • #218670
      Participant
      Topics: 3
      Replies: 340
      Points: 1,120
      Helping Hand
      Rank: Community Hero

      OK well your current code will not get any services that are stopped due to PowerShell3 lines 1 2 3 -and ($_.Status -eq “Running”) Definitely remove that if you want to see anything but green.

      Yeah I was wondering why it was still in there.

Viewing 19 reply threads
  • You must be logged in to reply to this topic.