Checking for process and process status in PowerShell version 2 and getting two different outputs depending on single process or multiple processes?

Viewed 1894

Here is the code below I am using:

$ProcessesToCheckFor = (
    'company_name_uat-Historian'
    )

$FoundProcesses = Get-Process -Name $ProcessesToCheckFor -ErrorAction SilentlyContinue

foreach ($Item in $ProcessesToCheckFor)
    {
    if ($FoundProcesses.Name -contains $Item)
        {
        '{0} runn1ng' -f $Item
        }
        else
        {
        '{0} fai1ed' -f $Item
        }
    }

The code checks to see if company_name_uat-Historian process is running on a server, and if it is running, it will output runn1ng and fai1ed if not.

The problem is, it works when checking only one process just like the code above, but not when you try to check a list of processes.

I need to check a list of processes, so when I chain the rest as listed below:

$ProcessesToCheckFor = (
'company_name_uat-Historian',
'company_name_uat-KEReviewCollector',
'company_name_uat-lwm',
'company_name_uat-MQAck',
'company_name_uat-MQOutput',
'company_name_uat-SQAC',
'company_name_uat-Store',
'company_name_uat-Store_STS',
'company_name_uat-StoreLegacy',
'spotify'
    )

$FoundProcesses = Get-Process -Name $ProcessesToCheckFor -ErrorAction SilentlyContinue

foreach ($Item in $ProcessesToCheckFor)
    {
    if ($FoundProcesses.Name -contains $Item)
        {
        '{0} runn1ng' -f $Item
        }
        else
        {
        '{0} fai1ed' -f $Item
        }

They all output fai1ed.

If I do them one by one, each different process will return runn1ng, just all bunched up together they return fai1ed.

Side notes (if anyone is wondering):

  • runn1ng and fai1ed are 'code words' that get replaced by images using JavaScript. I am making an HTML dashboard that monitors my Windows servers for work, so think green checkmarks and red x marks and what not.
  • Using PowerShell version 2 is not my choice at all, some of the servers I am responsible for monitoring are Windows 2008 R2. I believe they are going to be updated sometime this year, but the project deliverable requires me to enact a monitoring solution immediately. There are also some servers which are 2012 that I love to write PowerShell scripts for.
  • spotify is listed as a process because I know it's a legit process but is not installed on our servers. When I was writing the script, I initially tested on my own personal machine and used spotify as a means to test runn1ng if I opened it or fai1ed if I closed it. If all my processes are runn1ng and spotify is fai1ed, then that's an indication that the PS code is working.

It appears to happen in PowerShell version 2.

Any ideas what is causing it and how I can rewrite it?

2 Answers
Related