Run role scripts as a scheduled task; add workshop docs
A Start-Process child dies with the WinRM shell, so the promotion never completed. Role scripts now run under the Task Scheduler as SYSTEM, which survives the disconnect and the reboot. Uploads retry on transient auth errors while a guest is busy installing features. Docs: participant guide, instructor runbook, blue-team debrief, and an instructor wordlist sized for a two-minute crack.
This commit is contained in:
+23
-12
@@ -109,17 +109,21 @@ def main() -> int:
|
||||
|
||||
if opts.action == "start":
|
||||
remote = opts.args[0]
|
||||
log = opts.args[1] if len(opts.args) > 1 else remote + ".log"
|
||||
done = remote + ".done"
|
||||
# A Start-Process child dies with the WinRM shell. A scheduled task
|
||||
# runs under the Task Scheduler service and survives the disconnect,
|
||||
# which matters for anything that reboots the guest.
|
||||
ps(session, f"Remove-Item -Force -ErrorAction SilentlyContinue '{done}'")
|
||||
rc, _, err = ps(
|
||||
session,
|
||||
f"Start-Process powershell -WindowStyle Hidden "
|
||||
f"-ArgumentList '-NoProfile','-ExecutionPolicy','Bypass','-File','{remote}' "
|
||||
f"-RedirectStandardOutput '{log}' -RedirectStandardError '{log}.err'",
|
||||
task = (
|
||||
'$tr = \'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "'
|
||||
+ remote
|
||||
+ '"\'; '
|
||||
"schtasks.exe /Create /TN NorbourgStage /F /SC ONCE /ST 00:00 /RU SYSTEM /RL HIGHEST /TR $tr; "
|
||||
"schtasks.exe /Run /TN NorbourgStage"
|
||||
)
|
||||
rc, out, err = ps(session, task)
|
||||
if rc != 0:
|
||||
print(err, file=sys.stderr)
|
||||
print(f"{out}\n{err}", file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
|
||||
@@ -129,11 +133,18 @@ def main() -> int:
|
||||
done = remote + ".done"
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
rc, out, _ = ps(session, f"Test-Path '{done}'")
|
||||
if out.strip().lower() == "true":
|
||||
rc, out, _ = ps(session, f"Get-Content -Raw '{done}'")
|
||||
print(out.strip())
|
||||
return 0
|
||||
try:
|
||||
rc, out, _ = ps(session, f"Test-Path '{done}'")
|
||||
if out.strip().lower() == "true":
|
||||
rc, out, _ = ps(session, f"Get-Content -Raw '{done}'")
|
||||
print(out.strip())
|
||||
return 0
|
||||
except Exception as exc: # noqa: BLE001 - reboots are expected
|
||||
print(f"retrying ({type(exc).__name__})", file=sys.stderr)
|
||||
try:
|
||||
session = connect(opts.host, opts.user, opts.password, opts.port)
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(10)
|
||||
return 3
|
||||
|
||||
|
||||
Reference in New Issue
Block a user