# Topic 22: Programming, Scripting, and Automation

**Fictional Northstar case · self-paced · 45–90 minutes plus optional setup**

## Opening question
How do you prevent a useful script from making a broad unwanted change?

## Explain
Automation makes a repeatable decision or action executable. Safe scripts validate inputs, limit scope, handle errors and make results visible. Idempotency means repeated execution preserves the intended state rather than duplicate side effects. Begin with read-only analysis and a dry-run plan before considering write actions. Approval belongs at the meaningful boundary, not after an irreversible operation.

## Supporting technologies
- **Variables and structured data:** Represent and validate inputs.
- **Control flow:** Apply explicit conditions and error handling.
- **Dry run and idempotency:** Preview changes and avoid duplicate effects.
- **Least privilege:** Use only necessary access for the specific task.

## Scenario
Northstar needs to identify stale fictional device records. The exercise must report candidates without deleting anything.

```text
device,days_since_seen,owner
LAB-A,5,team_a
LAB-B,95,team_b
LAB-C,unknown,unknown
```

## Hands-on procedure
1. Read the CSV and define stale as a numeric age greater than 90 days; unknown is a separate review state.
2. Write pseudocode or Python that validates the header and each value.
3. Generate candidate and review reports without editing the input.
4. Run twice and compare results to check deterministic behavior.
5. Test a missing column and a nonnumeric value; require a visible failure or explicit review result.
6. Document a future deletion approval process, but include no delete operation in the lab.

## Expected reasoning and limits
LAB-B is a candidate, LAB-C requires review and LAB-A is current under the supplied rule. The script performs no external action; a candidate is not authorization to delete.

## Best practices
- Validate inputs and fail clearly.
- Prefer read-only and dry-run modes first.
- Avoid hard-coded credentials and broad privileges.
- Test repeat execution and failure paths.

## Vendors, projects and learning
- [Python Software Foundation](https://docs.python.org/3/library/csv.html): Open-source Python runtime and CSV library
- [Microsoft](https://learn.microsoft.com/en-us/powershell/): PowerShell documentation
- [Red Hat](https://docs.ansible.com/): Ansible automation documentation

## Optional technical extension
Implement the report with Python csv.DictReader, output a new CSV and add cases for missing headers, empty input and invalid ages. Use only supplied synthetic data.

## Career practice
Relevant role families to investigate: IT automation technician, Junior infrastructure engineer, Technical support analyst. No current vacancy or hiring requirement is asserted.

Interview: How would you design a script so a malformed file cannot trigger unintended actions?

## Sources
- [Python CSV reference](https://docs.python.org/3/library/csv.html)

## Portfolio evidence
Use the [portfolio guide](../guides/portfolio.md). Submit original input, decisions, tests, actual results, limitations and source attribution. Clearly label modeled, planned and performed work.

## Suggested prerequisites
Topic 6
