Define the desired state
Write the desired state in plain language before the script. Then map it to one observable signal. Avoid scripts that silently combine unrelated fixes because they are difficult to test and assign safely.
Make repair idempotent
Running a repair twice should produce the same result as running it once. Check current state before writing, preserve existing permissions, and use explicit error handling for unavailable resources.
$service = Get-Service -Name 'w32time' -ErrorAction Stop
if ($service.StartType -ne 'Automatic') {
Set-Service -Name 'w32time' -StartupType Automatic
}
if ($service.Status -ne 'Running') { Start-Service -Name 'w32time' }
exit 0Design for support
Return concise status values and log only what support needs. A failure should identify the stage and error category, not dump an opaque transcript containing sensitive machine data.
Operational checklist
- Keep one desired state per script.
- Check before changing.
- Use explicit failure handling.
- Test repeated execution and rollback.