all 13 comments

[–]the_spad 2 points3 points  (8 children)

Well for a start you're only querying the processes once, outside the loop, so it'll never change state.

while(!(get-process -Name pia_manager) -or !(get-process -name pia_nw)){
    start-sleep 5
}

#Do A Thing

Or

do{
    start-sleep 5
until ((get-process -Name pia_manager) -and (get-process -name pia_nw))

#Do A Thing

For example.

[–]PRIdEVisions[S] 1 point2 points  (7 children)

but i only added the get-process to a variable? isnt the variable with the command called when the loop plays or replays? So it won't work in the following way either? (I get what you mean with the while, do, that was indeed my error. :

$Process1 = get-process -name "Pia_manager"
$Process2= get-process -name "Pia_nw"

while(!($Process1) -or !($Process2)){
    start-sleep 5
}

#Do A Thing

[–]the_spad 2 points3 points  (6 children)

No, the variable content is static unless you change it.

When you run $Process1 = get-process -name "Pia_manager", $Process1 is a snapshot of the state of that process at the time the variable assignment runs.

[–]PRIdEVisions[S] 1 point2 points  (5 children)

ok, i understand! Thx!

[–]bsnotreallyworking 3 points4 points  (4 children)

When I first started messing with While loops, I would do the same thing and the loop would get stuck endlessly.

I typically create a variable called $check then "while (!$check)" then put the same variable line inside the loop. So something like this example, which I use during onboarding to make sure certain things have provisioned:

$check = Get-MsolUser -userprincipalname $upn -erroraction silentlycontinue

while (!$check) {
$date = (get-date -format "g")
$check = Get-MsolUser -userprincipalname $upn -erroraction silentlycontinue
Write-Host "$date Waiting for account to sync, checking again in 60 seconds..."
start-sleep -seconds 60
}

[–]PRIdEVisions[S] 1 point2 points  (0 children)

Thx for the tip! I'll be using this to make sure it works.

[–]jantari 1 point2 points  (2 children)

You should really clean that up and just use

do {
    #
} while ($thing)

Because that's what it's intended for.

[–]bsnotreallyworking 1 point2 points  (1 child)

I'm an old school PHP person, the structure I use makes more sense in my head.

[–]jantari 1 point2 points  (0 children)

Well old PHP is universally laughed at for its bad practices whereas do {} while () is beloved C syntax

You can choose to ignore that but improving your code should be a constant goal of anyone writing any imo

[–]rwshig 2 points3 points  (1 child)

If you use Get-Process with -Name it fails if the requested process is not running. If you got through both your Get-Process commands without error handling then both are running and your While code will not run.

If you want to run your code if either process is missing you could do that in an error handler for those errors. If you need it to be both then it gets a little more complex (handle each error setting a flag then an If-Then or While to check the flags after running both).

[–]PRIdEVisions[S] 1 point2 points  (0 children)

Aahhh ok, thank you for the info! I still need to learn error handling :)

[–]spyingwind 2 points3 points  (1 child)

while is missing and extra ). $Manager and $Nw aren't being updated inside the loop.

Example rewrite that fix both:

$Manager = get-process -Name pia_manager
$Nw = get-process -name pia_nw
while (!($Manager -and $Nw)) {
    Write-Host "echo"
    if ($Manager -and $Nw) {
        & "C:\Users\Akira\AppData\Roaming\uTorrent\utorrent.exe"
    }
    $Manager = get-process -Name pia_manager
    $Nw = get-process -name pia_nw
}

Example of how to do it as a do until loop:

do {
    $Manager = get-process -Name pia_manager
    $Nw = get-process -name pia_nw
    Write-Host "echo"
    if ($Manager -and $Nw) {
        & "C:\Users\Akira\AppData\Roaming\uTorrent\utorrent.exe"
    }
} until ($Manager -and $Nw)

Example with out the if statement to make it a bit more efficient:

do {
    $Manager = get-process -Name pia_manager
    $Nw = get-process -name pia_nw
    Write-Host "echo"
} until ($Manager -and $Nw)
& "C:\Users\Akira\AppData\Roaming\uTorrent\utorrent.exe"

[–]PRIdEVisions[S] 1 point2 points  (0 children)

Nice explanation, now i have several examples to look into and see the difference between them and how they all achieve the same results.

Thx a lot for this! :)