137 lines
2.9 KiB
Markdown
137 lines
2.9 KiB
Markdown
# 📘 Week 1 · Day 4 — PowerShell Exercises
|
||
|
||
This document describes **all exercises for Week 1 Day 4**, focused on **PowerShell conditionals, loops, and sysadmin-oriented input validation**.
|
||
|
||
The goal is to translate your existing algorithmic knowledge (from C, Python, SQL) into **PowerShell syntax and pipelines**, preparing you for Windows administration and automation tasks.
|
||
|
||
---
|
||
|
||
## 🎯 Learning Objectives
|
||
|
||
By completing today’s exercises, you will:
|
||
|
||
* Use `if / elseif / else` for conditional logic
|
||
* Loop over strings and collections with `foreach`
|
||
* Apply input validation logic in a Windows scripting context
|
||
* Understand early exit using `break`
|
||
* Reason about algorithm runtime (`T(n)`) in PowerShell
|
||
* Practice defensive programming for security-sensitive scripts
|
||
|
||
---
|
||
|
||
## 🛠 Exercises
|
||
|
||
### Exercise 1 — Even / Odd Number Checker
|
||
|
||
**Task:**
|
||
|
||
* Prompt the user for a number
|
||
* Output `Even` or `Odd`
|
||
|
||
**Concepts practiced:**
|
||
|
||
* `Read-Host`
|
||
* Integer conversion
|
||
* Conditional branching (`if / else`)
|
||
|
||
---
|
||
|
||
### Exercise 2 — Count Letters in a String
|
||
|
||
**Task:**
|
||
|
||
* Prompt the user for a string
|
||
* Count only letters (`A-Z`, `a-z`)
|
||
* Output the number of letters
|
||
|
||
**Concepts practiced:**
|
||
|
||
* `foreach` loop over string characters
|
||
* Regex matching with `-match`
|
||
* Counter variable logic
|
||
|
||
---
|
||
|
||
### Exercise 3 — Username Validator
|
||
|
||
**Task:**
|
||
|
||
* Prompt the user for a username
|
||
* Validate using these rules:
|
||
|
||
* Length ≤ 20
|
||
* Starts with a letter
|
||
* No digits
|
||
* No spaces
|
||
* Output `Accepted` or `Refused`
|
||
|
||
**Concepts practiced:**
|
||
|
||
* Conditional logic
|
||
* Early exit using `break`
|
||
* Character validation via regex
|
||
* Defensive programming
|
||
|
||
---
|
||
|
||
### Exercise 4 — Validate Multiple Usernames
|
||
|
||
**Task:**
|
||
|
||
* Given an array of usernames:
|
||
|
||
```powershell
|
||
$users = @("admin", "root1", "John_Doe", "Alice", "Bob42")
|
||
```
|
||
|
||
* Print only valid usernames (using the same rules as Exercise 3)
|
||
|
||
**Concepts practiced:**
|
||
|
||
* Iterating over collections with `foreach`
|
||
* Reusing validation logic
|
||
* Output formatting
|
||
|
||
---
|
||
|
||
### Exercise 5 — Algorithm Explanation (Written)
|
||
|
||
**Task:**
|
||
|
||
* Explain what happens if the number of users doubles
|
||
* Write the runtime formula using:
|
||
|
||
* `n` = number of users
|
||
* `m` = average username length
|
||
|
||
**Expected reasoning:**
|
||
|
||
* Doubling the list doubles the execution time
|
||
* Linear runtime: `T(n, m) = n × m`
|
||
|
||
---
|
||
|
||
## 🔐 Security Perspective
|
||
|
||
PowerShell scripts are heavily used in **Windows administration and security tasks**. Today’s exercises reinforce:
|
||
|
||
* Input validation to prevent bad data
|
||
* Predictable scaling of loops
|
||
* Defensive coding practices
|
||
|
||
These habits are essential for:
|
||
|
||
* Active Directory scripting
|
||
* Log parsing and auditing
|
||
* Automating security tasks
|
||
* Incident response and analysis
|
||
|
||
---
|
||
|
||
## ✅ Completion Criteria
|
||
|
||
Day 4 is complete when:
|
||
|
||
* Exercises 1–4 run correctly and produce expected outputs
|
||
* Exercise 5 demonstrates correct understanding of scaling and runtime
|
||
* Scripts are secure, readable, and reusable |