-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SqlServerDsc: Add new commands to handle trace flags (#1853)
- 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
Showing
30 changed files
with
4,488 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ';' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.