Skip to content

Commit

Permalink
Get-SqlDscAudit: Name no longer mandatory (#1850)
Browse files Browse the repository at this point in the history
- `Get-SqlDscAudit`
  - The parameter `Name` is no longer mandatory. When left out all the current
    audits are returned.
  • Loading branch information
johlju authored Mar 7, 2023
1 parent 9d51436 commit 54b66c6
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 21 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions source/Classes/020.SqlAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = (
Expand Down
5 changes: 4 additions & 1 deletion source/Public/Disable-SqlDscAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion source/Public/Enable-SqlDscAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 23 additions & 13 deletions source/Public/Get-SqlDscAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@
#>
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
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Microsoft.SqlServer.Management.Smo.Server]
$ServerObject,

[Parameter(Mandatory = $true)]
[Parameter()]
[System.String]
$Name,

Expand All @@ -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
}
}
2 changes: 1 addition & 1 deletion source/Public/New-SqlDscAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion source/Public/Remove-SqlDscAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion source/Public/Set-SqlDscAudit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 39 additions & 1 deletion tests/Unit/Public/Get-SqlDscAudit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Describe 'Get-SqlDscAudit' -Tag 'Public' {
It 'Should have the correct parameters in parameter set <MockParameterSetName>' -ForEach @(
@{
MockParameterSetName = '__AllParameterSets'
MockExpectedParameters = '[-ServerObject] <Server> [-Name] <string> [-Refresh] [<CommonParameters>]'
MockExpectedParameters = '[-ServerObject] <Server> [[-Name] <string>] [-Refresh] [<CommonParameters>]'
}
) {
$result = (Get-Command -Name 'Get-SqlDscAudit').ParameterSets |
Expand Down Expand Up @@ -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'
}
}
}

0 comments on commit 54b66c6

Please sign in to comment.