locked
color the output if service state is stopped RRS feed

  • Question

  • Hi'

    I have this lines:

    $SPServices = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName 
    $SPServices | Select-Object DisplayName, StartName, StartMode, State, Status |Sort-Object DisplayName | Ft -Auto
    
    I need that a service in state "Stopped" will be in red at the output, haw can I achieve this?

    Tuesday, November 14, 2017 7:25 AM

All replies

  • Write-Host 'Stopped' -Fore Red

    \_(ツ)_/

    Tuesday, November 14, 2017 7:39 AM
    Moderator
  • but this line already get me the output:

    $SPServices | Select-Object DisplayName, StartName, StartMode, State, Status |Sort-Object DisplayName | Ft -Auto

    Example output:

    DisplayName                              StartName              tartMode   State   Status
    -----------                                    ---------                 ---------    -----   ------

    AppFabric Caching Service                          sys1\userservice                    Disabled      Stopped  OK    

    so where I put your line ? "Write-Host 'Stopped' -Fore Red"

     
    Tuesday, November 14, 2017 8:00 AM
  • There is no build in way to change the standard output of the cmdlets. You have to create your own output. Make a condition and based on that output the line you want in red or not.

    Best regards (79,108,97,102|%{[char]$_})-join''

    Tuesday, November 14, 2017 8:04 AM
  • Tuesday, November 14, 2017 8:06 AM
    Moderator
  • Either you can change the color using property of the powershell terminal  or you can change the color using the cmdlets as mentioned above .

    Prashant Dev Pandey LIVE IN YOUR OWN WAY Please remember to mark the replies as answers if they help

    Tuesday, November 14, 2017 8:17 AM
  • Hi'

    I have this lines:

    $SPServices = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName 
    $SPServices | Select-Object DisplayName, StartName, StartMode, State, Status |Sort-Object DisplayName | Ft -Auto
    I need that a service in state "Stopped" will be in red at the output, haw can I achieve this?

    Play with the below script

    $services = Get-WmiObject -Class Win32_Service | write-output
    
    foreach ($objItem in $services) {
    if($objItem.State -eq 'Stopped')
    {
          write-host $objItem.DisplayName, $objItem.State  -foregroundcolor "red"
          }
          else
          {
           write-host $objItem.DisplayName, $objItem.State -foregroundcolor "green"
          }
    }


    (66,65,83,65,84,73|%{[char]$_})-join''

    Tuesday, November 14, 2017 8:39 AM
  • Hi,

    How to get Red or green only the State column, not the entire row?

    Wednesday, July 17, 2019 11:41 AM