Skip to content

Commit 6e26282

Browse files
fix(scripts): standardize PowerShell requirements header block (#385)
## Summary Add standardized requirements header block to all 13 production PowerShell scripts for consistent error handling, PowerShell version requirements, and strict mode enforcement. ## Changes - Added `#Requires -Version 7.0` before param() block - Added `Set-StrictMode -Version Latest` after param() block - Added `$ErrorActionPreference = 'Stop'` after Set-StrictMode - Removed duplicate `$ErrorActionPreference` assignments from script bodies ## Files Modified (13 total) - `scripts/linting/*.ps1` (6 files) - `scripts/security/*.ps1` (3 files) - `scripts/extension/*.ps1` (2 files) - `scripts/lib/Get-VerifiedDownload.ps1` - `scripts/dev-tools/Generate-PrReference.ps1` ## Validation - ✅ PSScriptAnalyzer passes with no new issues - ✅ All 13 scripts have standardized header block - ✅ No duplicate $ErrorActionPreference assignments Fixes #286 --------- Co-authored-by: Bill Berry <[email protected]>
1 parent 74a30bb commit 6e26282

13 files changed

Lines changed: 42 additions & 16 deletions

scripts/dev-tools/Generate-PrReference.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Microsoft Corporation.
22
# SPDX-License-Identifier: MIT
3+
#Requires -Version 7.0
34

45
<#
56
.SYNOPSIS
@@ -16,6 +17,7 @@ Git branch used as the comparison base. Defaults to "main".
1617
.PARAMETER ExcludeMarkdownDiff
1718
When supplied, excludes markdown (*.md) files from the diff output.
1819
#>
20+
1921
[CmdletBinding()]
2022
param(
2123
[Parameter()]
@@ -26,6 +28,7 @@ param(
2628
)
2729

2830
$ErrorActionPreference = 'Stop'
31+
2932
Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force
3033

3134
function Test-GitAvailability {

scripts/extension/Package-Extension.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env pwsh
22
# Copyright (c) Microsoft Corporation.
33
# SPDX-License-Identifier: MIT
4+
#Requires -Version 7.0
45

56
<#
67
.SYNOPSIS
@@ -70,6 +71,8 @@ param(
7071
[switch]$PreRelease
7172
)
7273

74+
$ErrorActionPreference = 'Stop'
75+
7376
Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force
7477

7578
#region Pure Functions
@@ -583,8 +586,6 @@ function Invoke-PackageExtension {
583586
try {
584587
# Only execute main logic when run directly, not when dot-sourced
585588
if ($MyInvocation.InvocationName -ne '.') {
586-
$ErrorActionPreference = "Stop"
587-
588589
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
589590
$RepoRoot = (Get-Item "$ScriptDir/../..").FullName
590591
$ExtensionDir = Join-Path $RepoRoot "extension"

scripts/extension/Prepare-Extension.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env pwsh
22
# Copyright (c) Microsoft Corporation.
33
# SPDX-License-Identifier: MIT
4+
#Requires -Version 7.0
45

56
<#
67
.SYNOPSIS
@@ -55,7 +56,8 @@ param(
5556
[switch]$DryRun
5657
)
5758

58-
$ErrorActionPreference = "Stop"
59+
$ErrorActionPreference = 'Stop'
60+
5961
Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force
6062

6163
#region Pure Functions
@@ -678,8 +680,6 @@ function Invoke-PrepareExtension {
678680
#region Main Execution
679681
if ($MyInvocation.InvocationName -ne '.') {
680682
try {
681-
$ErrorActionPreference = "Stop"
682-
683683
# Verify PowerShell-Yaml module is available
684684
if (-not (Get-Module -ListAvailable -Name PowerShell-Yaml)) {
685685
throw "Required module 'PowerShell-Yaml' is not installed."

scripts/lib/Get-VerifiedDownload.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Microsoft Corporation.
22
# SPDX-License-Identifier: MIT
3+
#Requires -Version 7.0
34

45
<#
56
.SYNOPSIS
@@ -58,6 +59,8 @@ param(
5859

5960
#endregion
6061

62+
$ErrorActionPreference = 'Stop'
63+
6164
Import-Module (Join-Path $PSScriptRoot "Modules/CIHelpers.psm1") -Force
6265

6366
#region Pure Functions
@@ -357,8 +360,6 @@ try {
357360
exit 1
358361
}
359362

360-
$ErrorActionPreference = 'Stop'
361-
362363
# Resolve destination directory and file name from OutputPath
363364
$destinationDir = Split-Path -Parent $OutputPath
364365
if (-not $destinationDir) {

scripts/linting/Invoke-LinkLanguageCheck.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
# Author: HVE Core Team
99
# Created: 2025-11-05
1010

11+
#Requires -Version 7.0
12+
1113
[CmdletBinding()]
1214
param(
1315
[string[]]$ExcludePaths = @()
1416
)
1517

18+
$ErrorActionPreference = 'Stop'
19+
1620
# Import shared helpers
1721
Import-Module (Join-Path $PSScriptRoot "Modules/LintingHelpers.psm1") -Force
1822
Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force

scripts/linting/Invoke-PSScriptAnalyzer.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# Author: HVE Core Team
99
# Created: 2025-11-05
1010

11+
#Requires -Version 7.0
12+
1113
[CmdletBinding()]
1214
param(
1315
[Parameter(Mandatory = $false)]
@@ -23,6 +25,8 @@ param(
2325
[string]$OutputPath = "logs/psscriptanalyzer-results.json"
2426
)
2527

28+
$ErrorActionPreference = 'Stop'
29+
2630
# Import shared helpers
2731
Import-Module (Join-Path $PSScriptRoot "Modules/LintingHelpers.psm1") -Force
2832
Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force

scripts/linting/Invoke-YamlLint.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env pwsh
22
# Copyright (c) Microsoft Corporation.
33
# SPDX-License-Identifier: MIT
4+
#Requires -Version 7.0
45
<#
56
.SYNOPSIS
67
Validates YAML files using actionlint for GitHub Actions workflows.
@@ -30,7 +31,6 @@
3031
- macOS: brew install actionlint
3132
- Linux: go install github.com/rhysd/actionlint/cmd/actionlint@latest
3233
#>
33-
#Requires -Version 7.0
3434

3535
[CmdletBinding()]
3636
param(
@@ -44,6 +44,8 @@ param(
4444
[string]$OutputPath = "logs/yaml-lint-results.json"
4545
)
4646

47+
$ErrorActionPreference = 'Stop'
48+
4749
# Import shared helpers
4850
Import-Module (Join-Path $PSScriptRoot "Modules/LintingHelpers.psm1") -Force
4951
Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force

scripts/linting/Link-Lang-Check.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Microsoft Corporation.
22
# SPDX-License-Identifier: MIT
3+
#Requires -Version 7.0
34

45
<#
56
.SYNOPSIS
@@ -52,6 +53,8 @@ param(
5253
[string[]]$ExcludePaths = @()
5354
)
5455

56+
$ErrorActionPreference = 'Stop'
57+
5558
Import-Module (Join-Path $PSScriptRoot "../lib/Modules/CIHelpers.psm1") -Force
5659

5760
function Get-GitTextFile {

scripts/linting/Markdown-Link-Check.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# SPDX-License-Identifier: MIT
44
#Requires -Version 7.0
55

6-
76
<#
87
.SYNOPSIS
98
Repository-aware wrapper for markdown-link-check.
@@ -45,6 +44,8 @@ param(
4544
[switch]$Quiet
4645
)
4746

47+
$ErrorActionPreference = 'Stop'
48+
4849
# Import LintingHelpers module
4950
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Modules/LintingHelpers.psm1') -Force
5051
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath '../lib/Modules/CIHelpers.psm1') -Force

scripts/linting/Validate-MarkdownFrontmatter.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# - Standard Copilot attribution footer (excludes Microsoft template files)
1414
# - Content structure by file type (GitHub configs, DevContainer docs, etc.)
1515

16-
#requires -Version 7.0
16+
#Requires -Version 7.0
1717

1818
using namespace System.Collections.Generic
1919
# Import FrontmatterValidation module with 'using' to make PowerShell class types
@@ -49,6 +49,8 @@ param(
4949
[switch]$SkipFooterValidation
5050
)
5151

52+
$ErrorActionPreference = 'Stop'
53+
5254
# Import helper modules
5355
# Note: FrontmatterValidation.psm1 is imported via 'using module' at top of script for class type availability
5456
Import-Module (Join-Path -Path $PSScriptRoot -ChildPath 'Modules/LintingHelpers.psm1') -Force

0 commit comments

Comments
 (0)