Checking for process and process status in PowerShell version 2 and getting two different outputs depending on single process or multiple processes? -
here code below 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 see if company_name_uat-historian
process running on server, , if running, output runn1ng
, fai1ed
if not.
the problem is, works when checking 1 process code above, not when try check list of processes.
i need check list of processes, when chain rest 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 output fai1ed
.
if them 1 one, each different process return runn1ng
, bunched return fai1ed
.
side notes (if wondering):
runn1ng
,fai1ed
'code words' replaced images using javascript. making html dashboard monitors windows servers work, think green checkmarks , red x marks , not.- using powershell version 2 not choice @ all, of servers responsible monitoring windows 2008 r2. believe going updated sometime year, project deliverable requires me enact monitoring solution immediately. there servers 2012 love write powershell scripts for.
spotify
listed process because know it's legit process not installed on our servers. when writing script, tested on own personal machine , usedspotify
means testrunn1ng
if opened orfai1ed
if closed it. if processesrunn1ng
, spotifyfai1ed
, that's indication ps code working.
it appears happen in powershell version 2.
any ideas causing , how can rewrite it?
the answer
if (($foundprocesses | select-object -expandproperty name) -contains $item) {
why
powershell 3 added called member enumeration. if have array of objects in 2.0, can't directly call properties of array, because looks properties on array object itself. in 3.0+ if member doesn't exist on array, check items member well. using select-object -expandproperty
more explicit way of calling members.
you move get-process
call foreach
loop.
foreach ($item in $processestocheckfor) { if (get-process -name $item -erroraction silentlycontinue) { '{0} runn1ng' -f $item } else { '{0} fai1ed' -f $item } }
Comments
Post a Comment