Help with Object output

Hi guys,

If anyone had some time to help me I would appreciate it!!

My ultimate objective is to output an object that would look like this (please tell me if this is dumb);

ProcessName DoesItExist


agent True

FakeProcess False

I am trying to accomplish this with the following code so far - but up to this point it doesn’t even work as I expected as it outputs warnings for processes that should work…

$procs = "agent", "ApplicationFrameHost", "FakeProcess", "FakeProcessTwo"

Foreach ($Proc in $procs) {

    Try {

        $Test += Get-Process -Name $Proc -ErrorAction STOP

        $Hash = [PSCUSTOMOBJECT]@{
        ProcessName = $Test.ProcessName
        DoesItExist = "True" 
                          }

    } Catch {

         Write-Warning "$Proc has failed"

    }# End try/Catch


} #End Foreach

Write-Output $Hash

 

I have two questions

  1. Why am I getting Warning for the processes that should be returning True/ Example my output:WARNING: agent has failed WARNING: ApplicationFrameHost has failed WARNING: FakeProcess has failed WARNING: FakeProcessTwo has failed

    ProcessName DoesItExist


    agent True


  2. How can I make my object return like the following (Please assume that agent, applicationframehost have both returned true and fakeprocesses have returned false)ProcessName DoesItExist


    agent True

    FakeProcess False

$ProcessList = @('agent', 'ApplicationFrameHost', 'FakeProcess', 'FakeProcessTwo')

$myOutput = foreach ($ProcessName in $ProcessList) {
    try {
        Get-Process -Name $ProcessName -ErrorAction STOP | Out-Null
        [PSCustomObject]@{
            ProcessName = $ProcessName
            DoesItExist = $true 
        }
    } catch {
        Write-Warning "$ProcessName has failed"
        [PSCustomObject]@{
            ProcessName = $ProcessName
            DoesItExist = $false 
        }
    }
}

$myOutput

and to answer your question,

$Test += Get-Process -Name $Proc -ErrorAction STOP

will always error out directing your code into the ‘catch’ branch, because

Method invocation failed because [System.Diagnostics.Process] does not contain a method named 'op_Addition'

Wow OK, didn’t know or consider casting the whole block as a variable in $myOutput = foreach ($ProcessName in $ProcessList) would work…

Sam, again thanks for your help. Your advice is always appreciated.

Cheers.

 

 

$Hash is the array to be added to, not $Test. Initialize the $Hash array. Unfortunately, try hides the error messages. I haven’t used it. It’s probably better for normally terminating errors. You may want to use the boolean $true.

$procs = "ampwatchdog", "applemobiledeviceservice", "FakeProcess"
$Hash = @()

Foreach ($Proc in $procs) {

  Try {
    
    $Test = Get-Process -Name $Proc -ErrorAction STOP
    
    $Hash += [PSCUSTOMOBJECT]@{
      ProcessName = $Test.ProcessName
      DoesItExist = "True" 
    }
  } Catch {

    Write-Warning "$Proc has failed"

  }# End try/Catch


} #End Foreach

Write-Output $Hash


WARNING: FakeProcess has failed

ProcessName              DoesItExist
-----------              -----------
AMPWatchDog              True
AppleMobileDeviceService True