A PowerShell Do-Until loop repeats until a condition becomes true. Unlike Do-While, it continues while the condition remains false, which is useful for waiting on an event, a file change, or another state transition. The structure is common in polling scripts and monitoring tasks.
Expanded Definition
A PowerShell Do-Until loop is a control structure that repeats a block of commands until a tested condition becomes true. In practice, the loop runs at least once, then evaluates the exit condition after each pass, which makes it different from Do-While and useful when the first action must occur before state can be checked. In NHI and automation contexts, this pattern is often used for polling a service, waiting for a secret rotation to complete, or confirming that an identity-related object has reached a desired state before continuing. Definitions vary across vendors when the loop is embedded in broader orchestration platforms, but the PowerShell behavior itself is straightforward and does not depend on the surrounding toolchain. For operational guidance on how automation fits into security governance, the NIST Cybersecurity Framework 2.0 is a useful external reference. The most common misapplication is using a Do-Until loop for a condition that is already true at startup, which occurs when the script author expects pre-check logic but the loop still executes once.
Examples and Use Cases
Implementing a Do-Until loop rigorously often introduces timing and retry tradeoffs, requiring organisations to weigh responsiveness against the risk of hammering a dependency with repeated checks.
- Waiting for a newly issued API key to appear in a secrets manager before deploying a service.
- Polling a directory or configuration source until a service account attribute has updated after rotation.
- Retrying a health check until an NHI-dependent endpoint reports a ready state.
- Watching for a file or log marker that signals a downstream automation step can continue.
For NHI-focused scripting, the Ultimate Guide to NHIs helps place these workflows in the broader context of lifecycle control, visibility, and credential governance. A separate way to think about the execution pattern is through the NIST lens on secure automation and continuous monitoring in the NIST Cybersecurity Framework 2.0. Because the loop always runs once, it is better suited to post-action verification than to precondition gating alone.
Why It Matters in NHI Security
PowerShell Do-Until loops matter in NHI security because a large share of identity work is automated, and automation often decides when a secret is rotated, when a service account is enabled, or when a deployment is allowed to proceed. NHIMG data shows that only 5.7% of organisations have full visibility into their service accounts, while 71% of NHIs are not rotated within recommended time frames. Those gaps make polling scripts both useful and risky: useful because they can confirm completion, risky because they can hide failure if the exit condition is too loose or the retry window is too generous. When a loop is used to wait on identity state, its condition becomes part of security control design, not just scripting convenience. The Ultimate Guide to NHIs is directly relevant here because it frames the operational consequences of weak visibility and rotation discipline. Organisations typically encounter the operational impact only after a rotation stalls, a token expires unexpectedly, or a deployment fails mid-release, at which point the Do-Until pattern becomes operationally unavoidable to address.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP Non-Human Identity Top 10 address the attack and risk surface, while NIST CSF 2.0 and NIST Zero Trust (SP 800-207) set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP Non-Human Identity Top 10 | NHI-02 | Polling loops can mask secret sprawl and delayed rotation failures in NHI workflows. |
| NIST CSF 2.0 | PR.AC-4 | Do-Until loops often govern automated access and readiness checks for non-human identities. |
| NIST Zero Trust (SP 800-207) | Zero Trust requires continuous verification, which aligns with state-checking automation patterns. |
Use retry loops only with bounded checks and verify they support secure secret handling and rotation.
Related resources from NHI Mgmt Group
- What is the core decision loop Agentic AI follows and why does it create security risk?
- Why do agentic AI systems need human-in-the-loop controls?
- What is the difference between code review and judgment-in-the-loop?
- Should organisations delay production AI agents until identity governance is mature?