Powershell issue putting process name into hashtable if it's not found

Viewed 115

I'm trying to run on a list of processes and if found put the process name in hashtable with value True and if not False, the True part works just fine I got an issue if the process is not found it won't pair the searched process name with Flase just be blank

CODE

$processStatus = $null

$processStatus = @{}

$processName = "firefox", "discord", "fgdfg"
[System.Diagnostics.Process[]]$processList = Get-Process $processName -ErrorAction SilentlyContinue |
 Sort-Object -Property name -Unique

ForEach ($process in $processList) {
    if($process -ne $null) {
        $processStatus.Add($process.ProcessName,"True")
    } else {
        $processStatus.Add($process,"Flase")
      }
}

Result

Name                           Value                                           
----                           -----                                           
Discord                        True                                            
firefox                        True

Expected result

Name                           Value                                           
----                           -----                                           
Discord                        True                                            
firefox                        True
fgdfg                          False 
2 Answers

Your quite close. You just need loop through the process names and run Get-Process against each process name. Then check if the process exists before setting the status in the hash table.

$processNames = "firefox", "discord", "fgdfg"

$processStatus = @{}
foreach ($processname in $processNames) {
    $processes = Get-Process -Name $processName -ErrorAction SilentlyContinue

    if ($null -ne $processes) {
        $processStatus[$processName] = $true
    }
    else {
        $processStatus[$processName] = $false
    }
}

Also make sure to put $null on the left side of the operator when doing comparisons. If your using VSCode, PSScriptAnalyzer will give a PSPossibleIncorrectComparisonWithNull warning if you don't do this. Have a look at the documentation for more information as to why this is recommended.

We could also use the .NET method System.Diagnostics.Process.GetProcessByName to get the processes that share the same name. This method returns System.Diagnostics.Process[], so we can simply check if the count is greater than 0.

$processNames = "firefox", "discord", "fgdfg", "notepad"

$processStatus = @{}
foreach ($processname in $processNames) {
    $processes = [System.Diagnostics.Process]::GetProcessesByName($processName)

    if ($processes.Count -gt 0) {
        $processStatus[$processName] = $true
    }
    else {
        $processStatus[$processName] = $false
    }
}

Execute Get-Process against each individual process name instead:

$processStatus = @{}
$processName = "firefox", "discord", "fgdfg"

$processName |ForEach-Object {
  $processStatus[$_] = @(Get-Process -Name $_ -ErrorAction SilentlyContinue).Count -gt 0
}

If you want a list of status objects instead, you could also use Select-Object:

$processNames |Select-Object @{Name='Name';Expression={$_}},@{Name='Status';Expression={@(Get-Process -Name $_ -ErrorAction SilentlyContinue).Count -gt 0}}
Related