From 54b66c673c26c6110169e06bc0bab07798f275d6 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Tue, 7 Mar 2023 18:47:39 +0100 Subject: [PATCH] `Get-SqlDscAudit`: `Name` no longer mandatory (#1850) - `Get-SqlDscAudit` - The parameter `Name` is no longer mandatory. When left out all the current audits are returned. --- CHANGELOG.md | 3 ++ source/Classes/020.SqlAudit.ps1 | 10 ++++-- source/Public/Disable-SqlDscAudit.ps1 | 5 ++- source/Public/Enable-SqlDscAudit.ps1 | 5 ++- source/Public/Get-SqlDscAudit.ps1 | 36 ++++++++++++------- source/Public/New-SqlDscAudit.ps1 | 2 +- source/Public/Remove-SqlDscAudit.ps1 | 5 ++- source/Public/Set-SqlDscAudit.ps1 | 6 +++- tests/Unit/Public/Get-SqlDscAudit.Tests.ps1 | 40 ++++++++++++++++++++- 9 files changed, 91 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 486e4d68e..8d3de7479 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed the regular expression `features?` from the GitVersion configuration. Before, if a fix commit mentioned the word feature but means a SQL Server feature GitVersion would bump minor instead of patch number. +- `Get-SqlDscAudit` + - The parameter `Name` is no longer mandatory. When left out all the current + audits are returned ([issue #1812](https://github.com/dsccommunity/SqlServerDsc/issues/1812)). ### Fixed diff --git a/source/Classes/020.SqlAudit.ps1 b/source/Classes/020.SqlAudit.ps1 index 25b5afd4e..cef0670f1 100644 --- a/source/Classes/020.SqlAudit.ps1 +++ b/source/Classes/020.SqlAudit.ps1 @@ -260,9 +260,12 @@ class SqlAudit : SqlResourceBase $serverObject = $this.GetServerObject() - $auditObject = $serverObject | + $auditObjectArray = $serverObject | Get-SqlDscAudit -Name $properties.Name -ErrorAction 'SilentlyContinue' + # Pick the only object in the array. + $auditObject = $auditObjectArray | Select-Object -First 1 + if ($auditObject) { $currentState.Name = $properties.Name @@ -349,9 +352,12 @@ class SqlAudit : SqlResourceBase #> if ($this.Ensure -eq [Ensure]::Present) { - $auditObject = $serverObject | + $auditObjectArray = $serverObject | Get-SqlDscAudit -Name $this.Name -ErrorAction 'Stop' + # Pick the only object in the array. + $auditObject = $auditObjectArray | Select-Object -First 1 + if ($auditObject) { $auditIsWrongType = ( diff --git a/source/Public/Disable-SqlDscAudit.ps1 b/source/Public/Disable-SqlDscAudit.ps1 index dca7e3bfa..c6ff1423f 100644 --- a/source/Public/Disable-SqlDscAudit.ps1 +++ b/source/Public/Disable-SqlDscAudit.ps1 @@ -85,7 +85,10 @@ function Disable-SqlDscAudit } # If this command does not find the audit it will throw an exception. - $AuditObject = Get-SqlDscAudit @getSqlDscAuditParameters + $auditObjectArray = Get-SqlDscAudit @getSqlDscAuditParameters + + # Pick the only object in the array. + $AuditObject = $auditObjectArray | Select-Object -First 1 } $verboseDescriptionMessage = $script:localizedData.Audit_Disable_ShouldProcessVerboseDescription -f $AuditObject.Name, $AuditObject.Parent.InstanceName diff --git a/source/Public/Enable-SqlDscAudit.ps1 b/source/Public/Enable-SqlDscAudit.ps1 index f492d912c..e2682faf2 100644 --- a/source/Public/Enable-SqlDscAudit.ps1 +++ b/source/Public/Enable-SqlDscAudit.ps1 @@ -85,7 +85,10 @@ function Enable-SqlDscAudit } # If this command does not find the audit it will throw an exception. - $AuditObject = Get-SqlDscAudit @getSqlDscAuditParameters + $auditObjectArray = Get-SqlDscAudit @getSqlDscAuditParameters + + # Pick the only object in the array. + $AuditObject = $auditObjectArray | Select-Object -First 1 } $verboseDescriptionMessage = $script:localizedData.Audit_Enable_ShouldProcessVerboseDescription -f $AuditObject.Name, $AuditObject.Parent.InstanceName diff --git a/source/Public/Get-SqlDscAudit.ps1 b/source/Public/Get-SqlDscAudit.ps1 index 77f82a3c3..70c21533a 100644 --- a/source/Public/Get-SqlDscAudit.ps1 +++ b/source/Public/Get-SqlDscAudit.ps1 @@ -29,8 +29,9 @@ #> function Get-SqlDscAudit { + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseOutputTypeCorrectly', '', Justification = 'Because the rule does not understands that the command returns [System.String[]] when using , (comma) in the return statement')] [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')] - [OutputType([Microsoft.SqlServer.Management.Smo.Audit])] + [OutputType([Microsoft.SqlServer.Management.Smo.Audit[]])] [CmdletBinding()] param ( @@ -38,7 +39,7 @@ function Get-SqlDscAudit [Microsoft.SqlServer.Management.Smo.Server] $ServerObject, - [Parameter(Mandatory = $true)] + [Parameter()] [System.String] $Name, @@ -55,22 +56,31 @@ function Get-SqlDscAudit $ServerObject.Audits.Refresh() } - $auditObject = $ServerObject.Audits[$Name] + $auditObject = @() - if (-not $AuditObject) + if ($PSBoundParameters.ContainsKey('Name')) { - $missingAuditMessage = $script:localizedData.Audit_Missing -f $Name + $auditObject = $ServerObject.Audits[$Name] - $writeErrorParameters = @{ - Message = $missingAuditMessage - Category = 'InvalidOperation' - ErrorId = 'GSDA0001' # cspell: disable-line - TargetObject = $Name - } + if (-not $AuditObject) + { + $missingAuditMessage = $script:localizedData.Audit_Missing -f $Name + + $writeErrorParameters = @{ + Message = $missingAuditMessage + Category = 'InvalidOperation' + ErrorId = 'GSDA0001' # cspell: disable-line + TargetObject = $Name + } - Write-Error @writeErrorParameters + Write-Error @writeErrorParameters + } + } + else + { + $auditObject = $ServerObject.Audits } - return $auditObject + return , [Microsoft.SqlServer.Management.Smo.Audit[]] $auditObject } } diff --git a/source/Public/New-SqlDscAudit.ps1 b/source/Public/New-SqlDscAudit.ps1 index 99bad6a49..5706491ab 100644 --- a/source/Public/New-SqlDscAudit.ps1 +++ b/source/Public/New-SqlDscAudit.ps1 @@ -216,7 +216,7 @@ function New-SqlDscAudit $auditObject = Get-SqlDscAudit @getSqlDscAuditParameters - if ($auditObject) + if ($auditObject.Count -gt 0) { $auditAlreadyPresentMessage = $script:localizedData.Audit_AlreadyPresent -f $Name diff --git a/source/Public/Remove-SqlDscAudit.ps1 b/source/Public/Remove-SqlDscAudit.ps1 index 56a6d5e0c..e872ae439 100644 --- a/source/Public/Remove-SqlDscAudit.ps1 +++ b/source/Public/Remove-SqlDscAudit.ps1 @@ -85,7 +85,10 @@ function Remove-SqlDscAudit } # If this command does not find the audit it will throw an exception. - $AuditObject = Get-SqlDscAudit @getSqlDscAuditParameters + $auditObjectArray = Get-SqlDscAudit @getSqlDscAuditParameters + + # Pick the only object in the array. + $AuditObject = $auditObjectArray | Select-Object -First 1 } $verboseDescriptionMessage = $script:localizedData.Audit_Remove_ShouldProcessVerboseDescription -f $AuditObject.Name, $AuditObject.Parent.InstanceName diff --git a/source/Public/Set-SqlDscAudit.ps1 b/source/Public/Set-SqlDscAudit.ps1 index e783cf920..7083d7afd 100644 --- a/source/Public/Set-SqlDscAudit.ps1 +++ b/source/Public/Set-SqlDscAudit.ps1 @@ -259,7 +259,11 @@ function Set-SqlDscAudit ErrorAction = 'Stop' } - $AuditObject = Get-SqlDscAudit @getSqlDscAuditParameters + # If this command does not find the audit it will throw an exception. + $auditObjectArray = Get-SqlDscAudit @getSqlDscAuditParameters + + # Pick the only object in the array. + $AuditObject = $auditObjectArray | Select-Object -First 1 } if ($Refresh.IsPresent) diff --git a/tests/Unit/Public/Get-SqlDscAudit.Tests.ps1 b/tests/Unit/Public/Get-SqlDscAudit.Tests.ps1 index 1d4bb6896..d857b9b86 100644 --- a/tests/Unit/Public/Get-SqlDscAudit.Tests.ps1 +++ b/tests/Unit/Public/Get-SqlDscAudit.Tests.ps1 @@ -53,7 +53,7 @@ Describe 'Get-SqlDscAudit' -Tag 'Public' { It 'Should have the correct parameters in parameter set ' -ForEach @( @{ MockParameterSetName = '__AllParameterSets' - MockExpectedParameters = '[-ServerObject] [-Name] [-Refresh] []' + MockExpectedParameters = '[-ServerObject] [[-Name] ] [-Refresh] []' } ) { $result = (Get-Command -Name 'Get-SqlDscAudit').ParameterSets | @@ -146,4 +146,42 @@ Describe 'Get-SqlDscAudit' -Tag 'Public' { } } } + + Context 'When getting all current audits' { + BeforeAll { + $mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' + $mockServerObject.InstanceName = 'TestInstance' + + $mockServerObject = $mockServerObject | + Add-Member -MemberType 'ScriptProperty' -Name 'Audits' -Value { + return @( + ( + New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Audit' -ArgumentList @( + $mockServerObject, + 'Log1' + ) + ), + ( + New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Audit' -ArgumentList @( + $mockServerObject, + 'Log2' + ) + ) + ) + } -PassThru -Force + + $mockDefaultParameters = @{ + ServerObject = $mockServerObject + } + } + + It 'Should return the correct values' { + $result = Get-SqlDscAudit @mockDefaultParameters + + $result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Audit' + $result | Should -HaveCount 2 + $result.Name | Should -Contain 'Log1' + $result.Name | Should -Contain 'Log2' + } + } }