Skip to content

Commit 38b55a4

Browse files
committed
Reset now applies to registered values
1 parent 09fa5d7 commit 38b55a4

File tree

2 files changed

+61
-24
lines changed

2 files changed

+61
-24
lines changed

functions/Reset-DBODefaultSetting.ps1

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@ function Reset-DBODefaultSetting {
22
<#
33
.SYNOPSIS
44
Resets configuration entries back to default values.
5-
5+
66
.DESCRIPTION
77
This function creates or changes configuration values.
88
These can be used to provide dynamic configuration information outside the PowerShell variable system.
9-
9+
1010
.PARAMETER Name
1111
Name of the configuration entry.
1212
1313
.PARAMETER All
1414
Specify if you want to reset all configuration values to their defaults.
1515
16+
.PARAMETER Temporary
17+
The setting is not persisted outside the current session.
18+
By default, settings will be remembered across all powershell sessions.
19+
20+
.PARAMETER Scope
21+
Choose if the setting should be stored in current user's registry or will be shared between all users.
22+
Allowed values: CurrentUser, AllUsers.
23+
AllUsers will require administrative access to the computer (elevated session).
24+
25+
Default: CurrentUser.
26+
1627
.PARAMETER Confirm
1728
Prompts to confirm certain actions
1829
@@ -21,42 +32,51 @@ function Reset-DBODefaultSetting {
2132
2233
.EXAMPLE
2334
Reset-DBODefaultSetting -Name ConnectionTimeout
24-
35+
2536
Reset connection timeout setting back to default value.
26-
37+
2738
.EXAMPLE
2839
Reset-DBODefaultSetting -All
29-
40+
3041
Reset all settings.
3142
#>
3243
[CmdletBinding(DefaultParameterSetName = "Named", SupportsShouldProcess)]
3344
param (
3445
[parameter(Mandatory, ParameterSetName = 'Named')]
3546
[string[]]$Name,
3647
[parameter(Mandatory, ParameterSetName = 'All')]
37-
[switch]$All
48+
[switch]$All,
49+
[switch]$Temporary,
50+
[ValidateSet('CurrentUser', 'AllUsers')]
51+
[string]$Scope = 'CurrentUser'
3852
)
3953

4054
process {
4155
if ($Name) {
56+
$settings = @()
4257
foreach ($n in $Name) {
43-
if ($PSCmdlet.ShouldProcess($Name, "Resetting the setting back to its default value")) {
44-
$config = Get-PSFConfig -FullName dbops.$n
45-
if ($config) { $config.ResetValue() }
46-
else {
47-
Stop-PSFFunction -Message "Unable to find setting $n" -EnableException $true
48-
}
49-
}
58+
$config = Get-PSFConfig -FullName dbops.$n
59+
if ($config) { $settings += $config }
60+
else { Stop-PSFFunction -Message "Unable to find setting $n" -EnableException $true }
5061
}
5162
}
52-
elseif ($All) {
53-
foreach ($config in Get-PSFConfig -Module dbops ) {
54-
if ($PSCmdlet.ShouldProcess($config, "Resetting the setting back to its default value")) {
55-
if ($config.Initialized) {
56-
$config.ResetValue()
57-
}
58-
else {
59-
Write-PSFMessage -Level Warning -Message "Setting $($config.fullName -replace '^dbops\.','')) was not initialized and has no default value as such"
63+
elseif ($All) { $settings = Get-PSFConfig -Module dbops }
64+
$newScope = switch ($Scope) {
65+
'CurrentUser' { 'UserDefault' }
66+
'AllUsers' { 'SystemDefault' }
67+
}
68+
foreach ($config in $settings) {
69+
$sName = $config.fullName -replace '^dbops\.', ''
70+
if ($PSCmdlet.ShouldProcess($config, "Resetting the setting $sName back to its default value")) {
71+
if ($config.Initialized) {
72+
$config.ResetValue()
73+
}
74+
else {
75+
Write-PSFMessage -Level Warning -Message "Setting $sName was not initialized and has no default value as such"
76+
}
77+
if (!$Temporary) {
78+
if ($PSCmdlet.ShouldProcess($Name, "Unregistering $sName in the $newScope scope")) {
79+
$config | Unregister-PSFConfig -Scope $newScope
6080
}
6181
}
6282
}

tests/Reset-DBODefaultSetting.Tests.ps1

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Describe "Reset-DBODefaultSetting tests" -Tag $commandName, UnitTests {
2525
AfterAll {
2626
Unregister-PSFConfig -Module dbops -Name tc1
2727
Unregister-PSFConfig -Module dbops -Name tc2
28-
Unregister-PSFConfig -Module dbops -Name tc3
28+
Unregister-PSFConfig -Module dbops -Name tc3 -Scope SystemDefault
2929
Unregister-PSFConfig -Module dbops -Name secret
3030
}
3131
Context "Resetting various configs" {
@@ -41,6 +41,12 @@ Describe "Reset-DBODefaultSetting tests" -Tag $commandName, UnitTests {
4141
$testResult | Should -BeNullOrEmpty
4242
Get-PSFConfigValue -FullName dbops.tc1 | Should -Be 1
4343
}
44+
It "resets temporary config" {
45+
Get-PSFConfigValue -FullName dbops.tc1 | Should -Be 2
46+
$testResult = Reset-DBODefaultSetting -Name tc1 -Temporary
47+
$testResult | Should -BeNullOrEmpty
48+
Get-PSFConfigValue -FullName dbops.tc1 | Should -Be 1
49+
}
4450
It "resets two configs" {
4551
Get-PSFConfigValue -FullName dbops.tc1 | Should -Be 2
4652
Get-PSFConfigValue -FullName dbops.tc2 | Should -Be 'string2'
@@ -54,17 +60,28 @@ Describe "Reset-DBODefaultSetting tests" -Tag $commandName, UnitTests {
5460
Get-PSFConfigValue -FullName dbops.tc2 | Should -Be 'string2'
5561
Get-PSFConfigValue -FullName dbops.tc3 | Should -Be 'another2'
5662
$testResult = Get-PSFConfigValue -FullName dbops.secret
57-
$cred = [pscredential]::new('test',$testResult)
63+
$cred = [pscredential]::new('test', $testResult)
5864
$cred.GetNetworkCredential().Password | Should Be 'bar'
5965
$testResult = Reset-DBODefaultSetting -All
6066
$testResult | Should -BeNullOrEmpty
6167
Get-PSFConfigValue -FullName dbops.tc1 | Should -Be 1
6268
Get-PSFConfigValue -FullName dbops.tc2 | Should -Be 'string'
6369
Get-PSFConfigValue -FullName dbops.tc3 | Should -Be 'another'
6470
$testResult = Get-PSFConfigValue -FullName dbops.secret
65-
$cred = [pscredential]::new('test',$testResult)
71+
$cred = [pscredential]::new('test', $testResult)
6672
$cred.GetNetworkCredential().Password | Should Be 'foo'
6773
}
74+
It "resets an AllUsers-scoped value" {
75+
try {
76+
Register-PSFConfig -FullName dbops.tc3 -Scope SystemDefault
77+
$testResult = Reset-DBODefaultSetting -Name tc3 -Scope AllUsers
78+
$testResult | Should -BeNullOrEmpty
79+
Get-PSFConfigValue -FullName dbops.tc3 | Should Be 'another'
80+
}
81+
catch {
82+
$_.Exception.Message | Should BeLike '*access*'
83+
}
84+
}
6885
}
6986
Context "Negative tests" {
7087
It "should throw when setting does not exist" {

0 commit comments

Comments
 (0)