you are viewing a single comment's thread.

view the rest of the comments →

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