Start by identifying what actually runs
At 6 a.m., a scheduled task creates a CSV that the operations team opens at the start of its shift. Someone proposes putting the script in Git. First establish which file runs, under which account, and with which arguments. Capture and preserve that version. The task keeps using its existing path throughout the import.
In this fictional example, Boreal Workshop has two copies of Export-Inventaire.ps1. The scheduled task determines which copy is the pilot baseline. Its contract is a CSV with the same columns, encoding and delimiter in the same location, with failures visible in logs and monitoring.
1. Map dependencies without starting the task
On the execution host, use an account permitted to read the task definition and script. These commands do not run the task. Confirm the actual script path from the action: the executable can call a wrapper. Arguments and identities can be sensitive; keep the output with restricted change records.
Record the engine (Windows PowerShell 5.1 or PowerShell 7), version and architecture, working directory, arguments, account, module versions, UNC paths, configuration and permissions. Check for calls from services, other scripts and deployment tools. A zero task result does not prove the CSV is fresh and usable; inspect it and verify that its consumer can read it.
$task = Get-ScheduledTask -TaskPath '\Operations\' -TaskName 'Export-Inventaire' -ErrorAction Stop
$task.Actions | Select-Object Execute, Arguments, WorkingDirectory
$task.Principal | Select-Object UserId, LogonType, RunLevel
$task | Get-ScheduledTaskInfo | Select-Object LastRunTime, LastTaskResult, NextRunTime
$script = 'C:\Ops\Export-Inventaire.ps1'
Get-FileHash -LiteralPath $script -Algorithm SHA256
Get-AuthenticodeSignature -LiteralPath $script | Select-Object Status
Get-Acl -LiteralPath $script | Select-Object Owner, AccessToString2. Preserve a usable baseline
Keep an approved copy of the script that actually runs, its dependencies, task definition, ACLs and configuration in protected storage. Record hashes and signature status. A task definition export does not recover a stored password; arrange identity recovery separately. If two servers run different versions, compare the files before choosing a baseline and record differences that affect the business process.
Before copying anything to the repository, check for secrets, live data and sensitive identifiers. If the source contains a secret, preserve evidence in protected storage, arrange rotation and import a sanitized version with the difference from the production hash documented. A private repository and .gitignore cannot protect a value already committed.
3. Import into a private repository without changing the task
Use a working clone outside the execution folder. For this first import, the line “scripts/Export-Inventaire.ps1 -text” in .gitattributes disables Git line-ending conversion for that file. Check for encoding attributes or filters, then compare the SHA-256 of a fresh checkout with the approved sanitized copy. Windows PowerShell 5.1 can misread UTF-8 scripts without a BOM when they contain non-ASCII characters. Test encoding before normalization; byte changes can invalidate signatures. Handle normalization and re-signing in a separate change.
These commands change the local clone only. Review the diff before committing and push the branch only after that review. In Azure Repos, restrict policy bypass and protect main with a pull request, reviewer and build validation. Check private repository access and pipeline job capacity separately for your plan; Azure DevOps Services and Server have different terms.
git switch -c import/inventaire
git add -- .gitattributes .gitignore scripts/Export-Inventaire.ps1
git diff --cached --stat
git diff --cached --check
git diff --cached
git commit -m "Import inventory export baseline"4. Validate the contract with synthetic inputs
Run this syntax check from the clone under the same engine as the task. It parses the file without running it. Add tests with a pinned, compatible Pester release for CSV column order, accented characters, empty files, missing inputs, access denied and exit code behaviour. Mocks help test error paths; real access requires a representative identity and ACLs. The pilot does not require installing test modules on the production server.
Compare two runs with the same synthetic inputs and separate temporary output paths. For scripts that send mail, open tickets or update a database, use isolated test destinations. WhatIf only protects operations that honour it. Never run two jobs concurrently against the same output.
$file = (Resolve-Path -LiteralPath '.\scripts\Export-Inventaire.ps1').Path
$tokens = $null
$parseErrors = $null
[System.Management.Automation.Language.Parser]::ParseFile(
$file, [ref]$tokens, [ref]$parseErrors
) | Out-Null
if ($parseErrors) { throw ($parseErrors -join [Environment]::NewLine) }5. Deploy one release and observe a complete cycle
On a pilot host, use an artifact built from an approved commit and record the commit SHA, package hash and included dependencies. Sign as required and validate the signature on the final package. Give the task account read and execute permissions on the code and write access only where the job needs it; the deployment identity handles delivery. Avoid git pull in production. Preserve the calling path, arguments and configuration outside the repository.
Before replacing anything, confirm that no instance is running and the maintenance window covers the next trigger; do not kill an active job. Keep the previous package and permissions, stage the new package, check its hashes and switch the pilot under the team’s change procedure. Observe at least one normal trigger and confirm CSV freshness, schema and successful downstream consumption before promotion. Missing, incomplete, late or unreadable output is a stop condition.
6. Restore execution and correct the repository separately
If the pilot fails, block new pilot runs under the change procedure and determine whether an active instance must finish. Restore the previous package and any changed ACLs, configuration and task definition. Verify the hash, expected signature and controlled run before resuming the schedule. Handle any data writes or messages already sent before rerunning.
In Git, propose a revert through a pull request if the faulty commit is already shared. A revert records an inverse change and installs nothing on the server. Link the two decisions in the change record: which release returned to the pilot and which correction reached main. An infrastructure review can help map dependencies when nobody can explain what consumes a script’s output.