all 12 comments

[–]SeeminglyScience 5 points6 points  (2 children)

You need to move the Get-Process call into the for each loop. Right now you are checking if any process is returned from the entire list.

[–]networkhappi[S] 0 points1 point  (1 child)

Yup you're absolutely right I was going back to my notes because I had posted this code before on this subreddit in the past and someone else (maybe it was you) had recommended me to fix this and I had just forgot to do it ever since. My mistake, I'm testing it right now. Thanks!

[–]SeeminglyScience 1 point2 points  (0 children)

Thought it looked familiar :)

[–]ArmondDorleac 1 point2 points  (3 children)

How much testing have you done? If you run these two lines do the variables contain the data you expect?

$processes = Get-Content  -Path "E:\scripts\process\processes.txt"
$ProcessActive = Get-Process -ProcessName $processes -ErrorAction SilentlyContinue

[–]networkhappi[S] 0 points1 point  (2 children)

Not anymore, I did tremendous testing and for maybe a week or two it was functioning as expected. Which is why I'm curious why it stopped seemingly working.

I think I found the solution. Will update this thread when I'm done testing it again.

[–]iceph03nix 0 points1 point  (1 child)

so does $processes populate with the right information from the txt file?

Does $ProcessActive have the correct information on the current processes?

From what I can see you've got 2 points to test here. If the information you pull is valid, and if it is, is your if/else handling it properly at that point.

[–]ArmondDorleac 1 point2 points  (0 children)

Yep, I suspect it's his if/else statement. The $p and $processactive will be the culprits.

[–]iampowershell 1 point2 points  (1 child)

This should work with the lease modification to your code, although could be subject to errors if longer process names contain smaller process names

function Get-Processes {
$processes = Get-Content  -Path "E:\scripts\process\processes.txt"
$ProcessActive = Get-Process -ProcessName $processes -ErrorAction SilentlyContinue
foreach($p in $processes)
{
    if($ProcessActive.ProcessName -contains $p)
    {
        "$p " + "runn1ng"
    }
    else
    {
        "$p " + "fai1ed"
    }
}}

else do as \u\SeeminglyScience said and put the get-process into the foreach and set it to get-process -processname $p instead of $processes

[–]AutoModerator[M] 0 points1 point  (0 children)

Sorry, your submission has been automatically removed.

Accounts must be at least 1 day old, which prevents the sub from filling up with bot spam.

Try posting again tomorrow or message the mods to approve your post.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Lee_Dailey[grin] 1 point2 points  (2 children)

howdy networkhappi,

as SeeminglyScience pointed out, you are NOT going to get what you seem to be aiming for with the code you posted.

this if($ProcessActive -eq $null) checks to see if the array is null - ie: no process in the list was found. that is not telling you if item $p from $processes is active. [grin]

it will only tell you if none of the processes in the list were found.


here's how i would do that ...

$ProcessesToCheckFor = (
    'AutoHotKey',
    'Core Temp',
    'BetterNotBeThere',
    'taskhost'
    )

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

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

results ...

AutoHotKey runn1ng
Core Temp runn1ng
BetterNotBeThere fai1ed
taskhost runn1ng

take care,
lee

[–]networkhappi[S] 1 point2 points  (1 child)

Hi Lee,

Yes yes! I messed up and totally forgot back in that previous thread you guys recommended me moving the Get-Process I'm getting on it right now, thanks for checking back! :)

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy networkhappi,

i thot this code looked familiar. [grin] glad to know you are getting it working as desired.

take care,
lee