4

I currently want to check if a list of processes are running, then display the result within a table such as:

 Process        Status
 =======        ======
Process 1       Running
Process 2      Not Running
Process 3       Running

I have the below code which produces an output showing each input and output as a string, but it looks messy depending on the length of the Process name.

$Node = Read-Host -Prompt 'Input Node name'

$Process = @("Process1", "Process2", "Process3")
$Process | foreach-object {if(!(Get-Process -Name $_ -ComputerName $Node - ErrorAction SilentlyContinue)) {"$_     -     Not Running"} else {"$_     -    Running"}}

I am at a loss. All help appreciated.

1
  • Aside from the better suited object oriented answers you can prettify your output with the string format operator -f {"{0,-30} Not Running" -f $_} else {"{0,-30} Running" -f $_} Commented May 4, 2018 at 13:39

2 Answers 2

5

Better (faster) to make a single remoting call to get all the processes, than one per process, so do that and store all the results - at least the names of the processes.

The next part is non-trivial. The way PowerShell and the neatly formatted tables work, is by making one object (bundle of things all together) for each table row, with each object having properties for each column name.

# Processes to look for
$Process = @("Process1", "Process2", "Process3")

$Node = Read-Host -Prompt 'Input Node name'

# Get running processes, and only keep their names
$runningProcesses = Get-Process -ComputerName $Node -ErrorAction SilentlyContinue | 
    Select-Object -ExpandProperty Name


$Process | ForEach-Object {

    # For each process name to look for, generate a hashtable of
    # columns and their values,
    # then cast it into a PS Object

    [PSCustomObject]@{

        'ProcessName' = $_
        'Status' = if ($runningProcesses -contains $_) { "Running" } else { "Not Running" }

    }

}

This gives a neat formatted table output, and is also structured data so you can feed the output of this into | ForEach-Object { $_.Status } and pick out the individual parts by name, something you can't do as neatly with your string formatted approach.

Sign up to request clarification or add additional context in comments.

4 Comments

Get-Process accepts arrays for the names. Would it not be better to just ask for the ones you want instead off all of them?
The output wasn't quite right on this, but I assume this is due to me running V2, rather than V3+. I tried to copy my code into a comment, but I am clueless with Stack! I added the below line in instead of PSCustomObject. $Results = New-Object -TypeName PSObject -Property @{
@Matt probably would, yes, I didn't think of it. Probably make the code get a horizontal scrollbar here though. RyanClayton ah yes, V2 won't like that type accelerator, good fix.
| ForEach-Object { $_.Status } this is unnecessarily long. You should instead do | ForEach-Object -MemberName 'Status' (|% Status)
1

Try this:

$node = Read-Host -Prompt 'Input Node name'

$processList = "Process1", "Process2", "Process3"

$processList |
    ForEach-Object {
        [PsCustomObject]@{
            NodeName = $node
            ProcessName = $_
            IsRunning = (Get-Process -Name $_ -ComputerName $node -ErrorAction SilentlyContinue | Select-Object -First 1) -ne $null
        }
    }

Output will be like this:

NodeName   ProcessName IsRunning
--------   ----------- ---------
Node1      Process1    True
Node1      Process2    True
Node1      Process3    False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.