Skip to content

Commit 717dfd3

Browse files
committed
feat: Allow configuring PowerShell preference variables
chore: Change default VerbosePreference to SilentlyContinue
1 parent 25a6ee4 commit 717dfd3

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ Set of automated actions, which bucket maintainers can use to save time managing
2525
1. `SPECIAL_SNOWFLAKES`
2626
- String
2727
- List of manifest names joined with `,` used as parameter for auto-pr utility.
28+
1. [PowerShell Preference Variables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables)
29+
- If an environment variable with the same name as the following PowerShell preference variable exists, its value will be assigned to the corresponding PowerShell preference variable:
30+
| Name | Type | Default Value |
31+
| ------------------------------------------------------------ | ------ | -------------------- |
32+
| [DebugPreference](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.5#debugpreference) | String | `'SilentlyContinue'` |
33+
| [InformationPreference](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.5#informationpreference) | String | `'SilentlyContinue'` |
34+
| [VerbosePreference](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.5#verbosepreference) | String | `'SilentlyContinue'` |
35+
| [WarningPreference](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.5#warningpreference) | String | `'Continue'` |
2836

2937
## Available actions
3038

action.ps1

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# Set Global Preference
2-
$Global:ErrorActionPreference = 'Continue'
3-
$Global:VerbosePreference = 'Continue'
4-
51
# Import all modules
62
Join-Path $PSScriptRoot 'src' | Get-ChildItem -File | Select-Object -ExpandProperty Fullname | Import-Module
73

src/Variables.psm1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
function Initialize-PreferenceVariable {
2+
<#
3+
.SYNOPSIS
4+
Initializes PowerShell preference variables based on environment variables or default values.
5+
#>
6+
7+
$DefaultPreferences = [ordered]@{
8+
ErrorActionPreference = @{ Value = 'Continue'; IgnoreEnv = $true } # Prevent changing ErrorActionPreference from env
9+
DebugPreference = @{ Value = 'SilentlyContinue' }
10+
InformationPreference = @{ Value = 'SilentlyContinue' }
11+
VerbosePreference = @{ Value = 'SilentlyContinue' }
12+
WarningPreference = @{ Value = 'Continue' }
13+
}
14+
15+
foreach ($Name in $DefaultPreferences.Keys) {
16+
$Preferences = $DefaultPreferences[$Name]
17+
$Value = $Preferences.Value
18+
19+
$EnvValue = Get-Item "Env:$Name" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Value
20+
21+
if ((-not $Preferences.IgnoreEnv) -and ($EnvValue)) {
22+
$Value = $EnvValue
23+
}
24+
25+
# Use built-in output functions instead of Write-Log here to avoid dependency issues
26+
Write-Host "Setting $Name to $Value ..."
27+
28+
Set-Variable -Name $Name -Value $Value -Scope Global
29+
}
30+
}
31+
32+
# Initialize PowerShell Preference Variables
33+
Initialize-PreferenceVariable
34+
135
# Environment
236
$env:SCOOP = Join-Path $env:USERPROFILE 'SCOOP'
337
$env:SCOOP_HOME = Join-Path $env:SCOOP 'apps\scoop\current'

0 commit comments

Comments
 (0)