How To Make A PowerShell Script Wait For A Specific Condition, With A Timeout
Have you ever written a PowerShell script and realized that you need to wait for something to happen between two operations? Perhaps you created a new user in Active Directory and you need to wait for it to replicate to the rest of your Domain Controllers before you can enable that user’s Exchange mailbox?Or maybe you were generating a report, and need to wait until the file exists and isn’t locked before you can email it to your boss?One approach to solving this problem is to pop a ‘Start-Sleep’ into your script and hope you picked a time that’s long enough. Or maybe you’re using a while loop to check that your condition is met… but then find that your script hangs indefinitely 1 in 20 times.Fortunately, it’s possible to solve both of these challenges, and the solution can be wrapped up in a reusable function.In this video, Josh will show you how to make your scripts wait for a specific condition to be met including a built-in timeout feature using a ‘while’ loop and the Stopwatch .NET class!
Prerequisites include: PowerShell