Skip to content

Commit 1c91099

Browse files
Uwe Jankeclaude
andcommitted
Fix Invoke-sqmRestoreDatabase AG-check and Invoke-sqmLogging WhatIf leak (1.6.1.0)
- Invoke-sqmRestoreDatabase: Get-DbaAvailabilityGroup has no -Database parameter; the terminating parameter-binding error bypassed -ErrorAction SilentlyContinue and aborted the restore as GlobalError when the target DB already existed. Check AG membership via Get-DbaAgDatabase and reload the AG object by name. - Invoke-sqmLogging: caller -WhatIf leaked via $WhatIfPreference into Out-File/New-Item, producing "Output to File" noise and writing no log. Both now run with -WhatIf:$false. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 27a1746 commit 1c91099

4 files changed

Lines changed: 30 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# sqmSQLTool — Changelog
22

3+
## [1.6.1.0] — 2026-06-22
4+
5+
### 🔧 Fixes
6+
7+
- **Invoke-sqmRestoreDatabase**: Brach bei existierender Ziel-Datenbank mit
8+
„A parameter cannot be found that matches parameter name 'Database'" ab. Ursache war
9+
`Get-DbaAvailabilityGroup -Database` (diesen Parameter gibt es nicht; der Parameter-Binding-Fehler
10+
ist terminierend und wird von `-ErrorAction SilentlyContinue` nicht abgefangen). AG-Mitgliedschaft
11+
wird jetzt über `Get-DbaAgDatabase` geprüft, das AG-Objekt über den AG-Namen nachgeladen.
12+
- **Invoke-sqmLogging**: `-WhatIf` des Aufrufers leakte über `$WhatIfPreference` in die
13+
internen `Out-File`/`New-Item`-Aufrufe und erzeugte „What if: Output to File"-Rauschen, während
14+
gar kein Log geschrieben wurde. Beide Aufrufe laufen jetzt mit `-WhatIf:$false` (Logging ist ein
15+
Seitenkanal und darf nicht unter ShouldProcess fallen).
16+
317
## [1.6.0.0] — 2026-06-21
418

519
### ✨ Neu

Private/Invoke-sqmLogging.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ function Invoke-sqmLogging
3838
# Sicherstellung dass LogPath existiert (könnte nach Modulimport gelöscht worden sein)
3939
if (-not (Test-Path $logPath -PathType Container))
4040
{
41-
New-Item -ItemType Directory -Path $logPath -Force -ErrorAction Stop | Out-Null
41+
# -WhatIf:$false: Logging ist ein Seitenkanal und darf nicht unter ShouldProcess
42+
# fallen. Sonst leakt ein -WhatIf des Aufrufers (via $WhatIfPreference) hier herein
43+
# und erzeugt "What if: Output to File"-Rauschen statt zu schreiben.
44+
New-Item -ItemType Directory -Path $logPath -Force -ErrorAction Stop -WhatIf:$false | Out-Null
4245
}
4346

4447
$dateStamp = Get-Date -Format "yyyyMMdd"
4548
$fileName = "sqmSQLTool_$($dateStamp)_$($FunctionName).log"
4649
$fullPath = Join-Path $logPath $fileName
4750

4851
$timestamp = Get-Date -Format "HH:mm:ss"
49-
"[$timestamp] [$Level] $Message" | Out-File -FilePath $fullPath -Append -Encoding UTF8 -ErrorAction Stop
52+
"[$timestamp] [$Level] $Message" | Out-File -FilePath $fullPath -Append -Encoding UTF8 -ErrorAction Stop -WhatIf:$false
5053
}
5154
catch
5255
{

Public/Invoke-sqmRestoreDatabase.ps1

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,17 @@ function Invoke-sqmRestoreDatabase
229229
{
230230
Invoke-sqmLogging -Message "Datenbank '$DatabaseName' existiert auf $SqlInstance." -FunctionName $functionName -Level "INFO"
231231

232-
# Pruefen, ob die Datenbank in einer AG ist
233-
$agCheck = Get-DbaAvailabilityGroup -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $DatabaseName -ErrorAction SilentlyContinue
234-
if ($agCheck)
232+
# Pruefen, ob die Datenbank in einer AG ist.
233+
# WICHTIG: Get-DbaAvailabilityGroup hat KEINEN -Database-Parameter. Ein -Database loest einen
234+
# terminierenden Parameter-Binding-Fehler aus, den -ErrorAction SilentlyContinue NICHT abfaengt.
235+
# Daher Mitgliedschaft ueber Get-DbaAgDatabase pruefen und das AG-Objekt (mit .Name) anschliessend
236+
# ueber den AG-Namen nachladen.
237+
$agDbCheck = Get-DbaAgDatabase -SqlInstance $SqlInstance -SqlCredential $SqlCredential -Database $DatabaseName -ErrorAction SilentlyContinue
238+
if ($agDbCheck)
235239
{
236240
$isAGDatabase = $true
237-
$availabilityGroup = $agCheck
241+
$agName = ($agDbCheck | Select-Object -First 1).AvailabilityGroup
242+
$availabilityGroup = Get-DbaAvailabilityGroup -SqlInstance $SqlInstance -SqlCredential $SqlCredential -AvailabilityGroup $agName -ErrorAction SilentlyContinue
238243
Invoke-sqmLogging -Message "Datenbank ist Mitglied der AG '$($availabilityGroup.Name)'." -FunctionName $functionName -Level "INFO"
239244
if (-not $KeepAlwaysOn)
240245
{

sqmSQLTool.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
RootModule = 'sqmSQLTool.psm1'
1818

1919
# Version number of this module.
20-
ModuleVersion = '1.6.0.0'
20+
ModuleVersion = '1.6.1.0'
2121

2222
# ID used to uniquely identify this module
2323
GUID = 'c4b10ba2-aee2-4d8d-ad86-a6e97c346ba6'
@@ -243,7 +243,7 @@
243243
# IconUri = ''
244244

245245
# ReleaseNotes of this module
246-
ReleaseNotes = 'See CHANGELOG.md and GitHub: https://github.com/JankeUwe/sqmSQLTool/releases/tag/v1.6.0.0'
246+
ReleaseNotes = 'See CHANGELOG.md and GitHub: https://github.com/JankeUwe/sqmSQLTool/releases/tag/v1.6.1.0'
247247

248248
# External module dependencies
249249
ExternalModuleDependencies = @('dbatools')

0 commit comments

Comments
 (0)