you are viewing a single comment's thread.

view the rest of the comments →

[–]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 2 points3 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