Mid level · Windows track

Write a Script That Does One Job

Logging, $PSScriptRoot, and safe names from real Phyllux Tech scripts.

One job per file. Second job, second script.

A useful script is boring on purpose. One job, a log, and a loud fail when something is wrong. That is how Phyllux Tech organize and migrate scripts stay re runnable.

#requires -version 5.1
$ErrorActionPreference = 'Stop'
$Root = $PSScriptRoot
$LogPath = Join-Path $Root 'one-job-log.txt'

function Write-Log([string]$Message) {
  $line = '{0} {1}' -f (Get-Date -Format 'o'), $Message
  Add-Content -Path $LogPath -Value $line -Encoding utf8
  Write-Host $Message
}

Write-Log "=== one job start ==="
$Target = Join-Path $Root 'notes'
if (-not (Test-Path $Target)) {
  New-Item -ItemType Directory -Path $Target | Out-Null
}
$out = Join-Path $Target ("hello-{0}.txt" -f (Get-Date -Format 'yyyy-MM-dd'))
Set-Content -Path $out -Value "Hello from one job." -Encoding utf8
Write-Log "Wrote $out"
Field House rule: giant do everything files become museums nobody dares to run.
General Windows literacy from Phyllux Tech Field House. Not remote support. Do not run scripts you do not understand. Never disable Defender for speed.
Next lesson Previous