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: 9
      Replies: 707
      Points: 2,842
      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: 9
      Replies: 707
      Points: 2,842
      Helping Hand
      Rank: Community Hero

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

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

      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 9 months, 2 weeks ago by Doug Maurer.
    • #217305
      Participant
      Topics: 4
      Replies: 10
      Points: 51
      Rank: Member

      NO JOY DIDN’T WORK

    • #217311
      Participant
      Topics: 9
      Replies: 707
      Points: 2,842
      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

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

    • #217317
      Participant
      Topics: 9
      Replies: 707
      Points: 2,842
      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

      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: 10
      Replies: 2472
      Points: 6,502
      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: 137
      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: 9
      Replies: 707
      Points: 2,842
      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: 17
      Replies: 1955
      Points: 4,004
      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
      ——————————————————————————————————–

    • #217776
      Participant
      Topics: 10
      Replies: 2472
      Points: 6,502
      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: 10
      Replies: 2472
      Points: 6,502
      Helping Hand
      Rank: Community MVP
    • #217791
      Participant
      Topics: 10
      Replies: 2472
      Points: 6,502
      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?

    • #218661
      Participant
      Topics: 10
      Replies: 46
      Points: 277
      Helping Hand
      Rank: Contributor

      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: 9
      Replies: 707
      Points: 2,842
      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
  • The topic ‘Unable to get a nice clear output HELP :-(’ is closed to new replies.