Skip to content

Commit

Permalink
SqlServerDsc: Add new commands to handle trace flags (#1853)
Browse files Browse the repository at this point in the history
- SqlServerDsc
  - The following private functions were added to the module (see comment-based
    help for more information):
    - `ConvertTo-ManagedServiceType`
    - `ConvertFrom-ManagedServiceType`
    - `Assert-ManagedServiceType`
  - The following public functions were added to the module (see comment-based
    help for more information):
    - `Get-SqlDscManagedComputer`
    - `Get-SqlDscManagedComputerService`
    - `Get-SqlDscTraceFlag`
    - `Add-SqlDscTraceFlag`
    - `Remove-SqlDscTraceFlag`
    - `Set-SqlDscTraceFlag`
    - `Get-SqlDscStartupParameter`
    - `Set-SqlDscStartupParameter`
  • Loading branch information
johlju authored Feb 23, 2023
1 parent 7a4620d commit 38a0da4
Show file tree
Hide file tree
Showing 30 changed files with 4,488 additions and 15 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
help for more information):
- `Assert-SetupActionProperties`
- `Invoke-SetupAction`
- `ConvertTo-ManagedServiceType`
- `ConvertFrom-ManagedServiceType`
- `Assert-ManagedServiceType`
- The following public functions were added to the module (see comment-based
help for more information):
- `Install-SqlDscServer`
Expand All @@ -60,6 +63,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Complete-SqlDscFailoverCluster`
- `Initialize-SqlDscRebuildDatabase`
- `Import-SqlDscPreferredModule`
- `Get-SqlDscManagedComputer`
- `Get-SqlDscManagedComputerService`
- `Get-SqlDscTraceFlag`
- `Add-SqlDscTraceFlag`
- `Remove-SqlDscTraceFlag`
- `Set-SqlDscTraceFlag`
- `Get-SqlDscStartupParameter`
- `Set-SqlDscStartupParameter`
- Added class `StartupParameters` which can parse the startup parameters
of a manged computer service object.
- New GitHub issue templates for proposing new public commands, proposing
an enhancement to an existing command, or having a problem with an existing
command.
Expand Down
150 changes: 150 additions & 0 deletions source/Classes/004.StartupParameters.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<#
.SYNOPSIS
A class to handle startup parameters of a manged computer service object.
.EXAMPLE
$startupParameters = [StartupParameters]::Parse(((Get-SqlDscManagedComputer).Services | ? type -eq 'SqlServer').StartupParameters)
$startupParameters | fl
$startupParameters.ToString()
Parses the startup parameters of the database engine default instance on the
current node, and then outputs the resulting object. Also shows how the object
can be turned back to a startup parameters string by calling ToString().
.NOTES
This class supports an array of data file paths, log file paths, and error
log paths though currently it seems that there can only be one of each.
This class was made with arrays in case there is an unknown edge case where
it is possible to have more than one of those paths.
#>
class StartupParameters
{
[System.String[]]
$DataFilePath

[System.String[]]
$LogFilePath

[System.String[]]
$ErrorLogPath

[System.UInt32[]]
$TraceFlag

[System.UInt32[]]
$InternalTraceFlag

static [StartupParameters] Parse([System.String] $InstanceStartupParameters)
{
Write-Debug -Message (
$script:localizedData.StartupParameters_DebugParsingStartupParameters -f 'StartupParameters.Parse()', $InstanceStartupParameters
)

$startupParameters = [StartupParameters]::new()

$startupParameterValues = $InstanceStartupParameters -split ';'

$startupParameters.TraceFlag = [System.UInt32[]] @(
$startupParameterValues |
Where-Object -FilterScript {
$_ -cmatch '^-T\d+'
} |
ForEach-Object -Process {
[System.UInt32] $_.TrimStart('-T')
}
)

Write-Debug -Message (
$script:localizedData.StartupParameters_DebugFoundTraceFlags -f 'StartupParameters.Parse()', ($startupParameters.TraceFlag -join ', ')
)

$startupParameters.DataFilePath = [System.String[]] @(
$startupParameterValues |
Where-Object -FilterScript {
$_ -cmatch '^-d'
} |
ForEach-Object -Process {
$_.TrimStart('-d')
}
)

$startupParameters.LogFilePath = [System.String[]] @(
$startupParameterValues |
Where-Object -FilterScript {
$_ -cmatch '^-l'
} |
ForEach-Object -Process {
$_.TrimStart('-l')
}
)

$startupParameters.ErrorLogPath = [System.String[]] @(
$startupParameterValues |
Where-Object -FilterScript {
$_ -cmatch '^-e'
} |
ForEach-Object -Process {
$_.TrimStart('-e')
}
)

$startupParameters.InternalTraceFlag = [System.UInt32[]] @(
$startupParameterValues |
Where-Object -FilterScript {
$_ -cmatch '^-t\d+'
} |
ForEach-Object -Process {
[System.UInt32] $_.TrimStart('-t')
}
)

return $startupParameters
}

[System.String] ToString()
{
$startupParametersValues = [System.String[]] @()

if ($this.DataFilePath)
{
$startupParametersValues += $this.DataFilePath |
ForEach-Object -Process {
'-d{0}' -f $_
}
}

if ($this.ErrorLogPath)
{
$startupParametersValues += $this.ErrorLogPath |
ForEach-Object -Process {
'-e{0}' -f $_
}
}

if ($this.LogFilePath)
{
$startupParametersValues += $this.LogFilePath |
ForEach-Object -Process {
'-l{0}' -f $_
}
}

if ($this.TraceFlag)
{
$startupParametersValues += $this.TraceFlag |
ForEach-Object -Process {
'-T{0}' -f $_
}
}

if ($this.InternalTraceFlag)
{
$startupParametersValues += $this.InternalTraceFlag |
ForEach-Object -Process {
'-t{0}' -f $_
}
}

return $startupParametersValues -join ';'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ function Get-ServiceObject
.EXAMPLE
ConvertTo-ManagedServiceType -ServiceType 'DatabaseEngine'
.NOTES
This helper function also exist as a private function, when this resource
is refactored into a class-based resource, this helper function can be
removed.
#>
function ConvertTo-ManagedServiceType
{
Expand Down
63 changes: 63 additions & 0 deletions source/Private/Assert-ManagedServiceType.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<#
.SYNOPSIS
Assert that a computer managed service is of a certain type.
.DESCRIPTION
Assert that a computer managed service is of a certain type. If it is the
wrong type an exception is thrown.
.PARAMETER ServiceObject
Specifies the Service object to evaluate.
.PARAMETER ServiceType
Specifies the normalized service type to evaluate.
.EXAMPLE
$serviceObject = Get-SqlDscManagedComputerService -ServiceType 'DatabaseEngine'
Assert-ManagedServiceType -ServiceObject $serviceObject -ServiceType 'DatabaseEngine'
Asserts that the computer managed service object is of the type Database Engine.
.EXAMPLE
$serviceObject = Get-SqlDscManagedComputerService -ServiceType 'DatabaseEngine'
$serviceObject | Assert-ManagedServiceType -ServiceType 'DatabaseEngine'
Asserts that the computer managed service object is of the type Database Engine.
.OUTPUTS
None.
#>
function Assert-ManagedServiceType
{
[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()]
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Microsoft.SqlServer.Management.Smo.Wmi.Service]
$ServiceObject,

[Parameter(Mandatory = $true)]
[ValidateSet('DatabaseEngine', 'SqlServerAgent', 'Search', 'IntegrationServices', 'AnalysisServices', 'ReportingServices', 'SQLServerBrowser', 'NotificationServices')]
[System.String]
$ServiceType
)

process
{
$normalizedServiceType = ConvertFrom-ManagedServiceType -ServiceType $ServiceObject.Type

if ($normalizedServiceType -ne $ServiceType)
{
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
($script:localizedData.ManagedServiceType_Assert_WrongServiceType -f $ServiceType, $normalizedServiceType),
'AMST0001', # cSpell: disable-line
[System.Management.Automation.ErrorCategory]::InvalidOperation,
$ServiceObject
)
)
}
}
}
111 changes: 111 additions & 0 deletions source/Private/ConvertFrom-ManagedServiceType.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<#
.SYNOPSIS
Converts a managed service type name to a normalized service type name.
.DESCRIPTION
Converts a managed service type name to its normalized service type name
equivalent.
.PARAMETER ServiceType
Specifies the managed service type to convert to the correct normalized
service type name.
.EXAMPLE
ConvertFrom-ManagedServiceType -ServiceType 'SqlServer'
Returns the normalized service type name 'DatabaseEngine' .
#>
function ConvertFrom-ManagedServiceType
{
[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.')]
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[Microsoft.SqlServer.Management.Smo.Wmi.ManagedServiceType]
$ServiceType
)

process
{
# Map the normalized service type to a valid value from the managed service type.
switch ($ServiceType)
{
'SqlServer'
{
$serviceTypeValue = 'DatabaseEngine'

break
}

'SqlAgent'
{
$serviceTypeValue = 'SqlServerAgent'

break
}

'Search'
{
$serviceTypeValue = 'Search'

break
}

'SqlServerIntegrationService'
{
$serviceTypeValue = 'IntegrationServices'

break
}

'AnalysisServer'
{
$serviceTypeValue = 'AnalysisServices'

break
}

'ReportServer'
{
$serviceTypeValue = 'ReportingServices'

break
}

'SqlBrowser'
{
$serviceTypeValue = 'SQLServerBrowser'

break
}

'NotificationServer'
{
$serviceTypeValue = 'NotificationServices'

break
}

default
{
<#
This catches any future values in the enum ManagedServiceType
that are not yet supported.
#>
$writeErrorParameters = @{
Message = $script:localizedData.ManagedServiceType_ConvertFrom_UnknownServiceType -f $ServiceType
Category = 'InvalidOperation'
ErrorId = 'CFMST0001' # CSpell: disable-line
TargetObject = $ServiceType
}

Write-Error @writeErrorParameters

break
}
}

return $serviceTypeValue
}
}
Loading

0 comments on commit 38a0da4

Please sign in to comment.