Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlRS: Using Invoke-SqlDscQuery #1919

Merged
merged 10 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
for a file.
- `Assert-Feature` - Throws an exception if a feature is not supported
for a specific Microsoft SQL Server major version.
- SqlServerDsc.Common
- `Connect-SQL`.
- Add new parameter `Encrypt`.
- `Connect-SqlDscDatabaseEngine`
- Add new parameter `Encrypt`.
- `Invoke-SqlDscQuery`
- Add new parameter `Encrypt`.

### Changed

Expand All @@ -40,6 +47,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- SqlSetup
- Update to support checking non-supported features using the command
`SqlDscIsSupportedFeature` ([issue #1872](https://github.com/dsccommunity/SqlServerDsc/issues/1872)).
- SqlRS
- Now uses the command `Invoke-SqlDscQuery` instead of `Invoke-SqlCmd`
([issue #1917](https://github.com/dsccommunity/SqlServerDsc/issues/1917)).
- The parameter `Encrypt` has changed so that `Mandatory` or `Strict`
will turn on encryption when connecting to the database instance.
- `Invoke-SqlDscQuery`
- Now shows the correct instance name when called using a server object
([issue #1918](https://github.com/dsccommunity/SqlServerDsc/issues/1918)).
- Correctly outputs query in verbose message when parameter `RedactText`
is not passed.

## [16.2.0] - 2023-04-10

Expand Down
1 change: 1 addition & 0 deletions RequiredModules.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@
PSDscResources = '2.12.0.0'
StorageDsc = '4.9.0.0'
NetworkingDsc = '7.4.0.0'
WSManDsc = '3.1.1'
}
72 changes: 26 additions & 46 deletions source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@ $script:localizedData = Get-LocalizedData -DefaultUICulture 'en-US'
Name of the SQL Server instance to host the Reporting Service database.

.PARAMETER Encrypt
Specifies how encryption should be enforced when using command `Invoke-SqlCmd`.
When not specified, the default value is `Mandatory`.

This value maps to the Encrypt property SqlConnectionEncryptOption
on the SqlConnection object of the Microsoft.Data.SqlClient driver.

This parameter can only be used when the module SqlServer v22.x.x is installed.
Specifies how encryption should be enforced. There are currently no
difference between using `Mandatory` or `Strict`.
#>
function Get-TargetResource
{
Expand Down Expand Up @@ -186,13 +181,8 @@ function Get-TargetResource
will not be restarted, even after initialization.

.PARAMETER Encrypt
Specifies how encryption should be enforced when using command `Invoke-SqlCmd`.
When not specified, the default value is `Mandatory`.

This value maps to the Encrypt property SqlConnectionEncryptOption
on the SqlConnection object of the Microsoft.Data.SqlClient driver.

This parameter can only be used when the module SqlServer v22.x.x is installed.
Specifies how encryption should be enforced. There are currently no
difference between using `Mandatory` or `Strict`.

.NOTES
To find out the parameter names for the methods in the class
Expand Down Expand Up @@ -329,15 +319,6 @@ function Set-TargetResource
$reportingServicesDatabaseName = "ReportServer`$$InstanceName"
}

if ( $DatabaseInstanceName -eq 'MSSQLSERVER' )
{
$reportingServicesConnection = $DatabaseServerName
}
else
{
$reportingServicesConnection = "$DatabaseServerName\$DatabaseInstanceName"
}

$wmiOperatingSystem = Get-CimInstance -ClassName Win32_OperatingSystem -Namespace 'root/cimv2' -ErrorAction SilentlyContinue
if ( $null -eq $wmiOperatingSystem )
{
Expand Down Expand Up @@ -466,32 +447,36 @@ function Set-TargetResource

$reportingServicesDatabaseRightsScript = Invoke-RsCimMethod @invokeRsCimMethodParameters

<#
Import-SqlDscPreferredModule cmdlet will import SQLPS (SQL 2012/14) or SqlServer module (SQL 2016),
and if importing SQLPS, change directory back to the original one, since SQLPS changes the
current directory to SQLSERVER:\ on import.
#>
Import-SqlDscPreferredModule

$invokeSqlCmdParameters = @{
ServerInstance = $reportingServicesConnection
$invokeSqlDscQueryParameters = @{
ServerName = $DatabaseServerName
InstanceName = $DatabaseInstanceName
DatabaseName = 'master'
Force = $true
Verbose = $VerbosePreference
ErrorAction = 'Stop'
}

if ($PSBoundParameters.ContainsKey('Encrypt'))
if ($PSBoundParameters.ContainsKey('Encrypt') -and $Encrypt -ne 'Optional')
{
$commandInvokeSqlCmd = Get-Command -Name 'Invoke-SqlCmd'

if ($null -ne $commandInvokeSqlCmd -and $commandInvokeSqlCmd.Parameters.Keys -contains 'Encrypt')
{
$invokeSqlCmdParameters.Encrypt = $Encrypt
}
$invokeSqlDscQueryParameters.Encrypt = $true
}

Invoke-SqlCmd @invokeSqlCmdParameters -Query $reportingServicesDatabaseScript.Script
Invoke-SqlCmd @invokeSqlCmdParameters -Query $reportingServicesDatabaseRightsScript.Script
Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query $reportingServicesDatabaseScript.Script
Invoke-SqlDscQuery @invokeSqlDscQueryParameters -Query $reportingServicesDatabaseRightsScript.Script

Write-Verbose -Message "Set database connection on $DatabaseServerName\$DatabaseInstanceName to database '$reportingServicesDatabaseName'."

if ( $DatabaseInstanceName -eq 'MSSQLSERVER' )
{
$reportingServicesConnection = $DatabaseServerName
}
else
{
$reportingServicesConnection = "$DatabaseServerName\$DatabaseInstanceName"
}

$invokeRsCimMethodParameters = @{
CimInstance = $reportingServicesData.Configuration
MethodName = 'SetDatabaseConnection'
Expand Down Expand Up @@ -863,13 +848,8 @@ function Set-TargetResource
will not be restarted, even after initialization.

.PARAMETER Encrypt
Specifies how encryption should be enforced when using command `Invoke-SqlCmd`.
When not specified, the default value is `Mandatory`.

This value maps to the Encrypt property SqlConnectionEncryptOption
on the SqlConnection object of the Microsoft.Data.SqlClient driver.

This parameter can only be used when the module SqlServer v22.x.x is installed.
Specifies how encryption should be enforced. There are currently no
difference between using `Mandatory` or `Strict`.
#>
function Test-TargetResource
{
Expand Down
14 changes: 13 additions & 1 deletion source/Modules/SqlServerDsc.Common/SqlServerDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,9 @@ function Start-SqlSetupProcess
.PARAMETER StatementTimeout
Set the query StatementTimeout in seconds. Default 600 seconds (10 minutes).

.PARAMETER Encrypt
Specifies if encryption should be used.

.EXAMPLE
Connect-SQL

Expand Down Expand Up @@ -493,7 +496,11 @@ function Connect-SQL
[Parameter()]
[ValidateNotNull()]
[System.Int32]
$StatementTimeout = 600
$StatementTimeout = 600,

[Parameter()]
[System.Management.Automation.SwitchParameter]
$Encrypt
)

Import-SqlDscPreferredModule
Expand All @@ -513,6 +520,11 @@ function Connect-SQL
$sqlConnectionContext.StatementTimeout = $StatementTimeout
$sqlConnectionContext.ApplicationName = 'SqlServerDsc'

if ($Encrypt.IsPresent)
{
$sqlConnectionContext.EncryptConnection = $true
}

if ($PSCmdlet.ParameterSetName -eq 'SqlServer')
{
<#
Expand Down
9 changes: 8 additions & 1 deletion source/Public/Connect-SqlDscDatabaseEngine.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
.PARAMETER StatementTimeout
Set the query StatementTimeout in seconds. Default 600 seconds (10 minutes).

.PARAMETER Encrypt
Specifies if encryption should be used.

.EXAMPLE
Connect-SqlDscDatabaseEngine

Expand Down Expand Up @@ -80,7 +83,11 @@ function Connect-SqlDscDatabaseEngine
[Parameter()]
[ValidateNotNull()]
[System.Int32]
$StatementTimeout = 600
$StatementTimeout = 600,

[Parameter()]
[System.Management.Automation.SwitchParameter]
$Encrypt
)

# Call the private function.
Expand Down
19 changes: 19 additions & 0 deletions source/Public/Invoke-SqlDscQuery.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
are written to the console. Strings will be escaped so they will not
be interpreted as regular expressions (RegEx).

.PARAMETER Encrypt
Specifies if encryption should be used.

.PARAMETER Force
Specifies that the query should be executed without any confirmation.

Expand Down Expand Up @@ -116,6 +119,10 @@ function Invoke-SqlDscQuery
[System.String]
$LoginType = 'Integrated',

[Parameter(ParameterSetName = 'ByServerName')]
[System.Management.Automation.SwitchParameter]
$Encrypt,

[Parameter(Mandatory = $true)]
[System.String]
$DatabaseName,
Expand Down Expand Up @@ -161,6 +168,11 @@ function Invoke-SqlDscQuery
Verbose = $VerbosePreference
}

if ($Encrypt.IsPresent)
{
$connectSqlDscDatabaseEngineParameters.Encrypt = $true
}

if ($LoginType -ne 'Integrated')
{
$connectSqlDscDatabaseEngineParameters['LoginType'] = $LoginType
Expand All @@ -174,6 +186,13 @@ function Invoke-SqlDscQuery
$ServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters
}

if ($PSCmdlet.ParameterSetName -eq 'ByServerObject')
{
$InstanceName = $ServerObject.InstanceName
}

$redactedQuery = $Query

if ($PSBoundParameters.ContainsKey('RedactText'))
{
$redactedQuery = ConvertTo-RedactedText -Text $Query -RedactPhrase $RedactText
Expand Down
7 changes: 7 additions & 0 deletions tests/Integration/DSC_SqlRS.config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,17 @@ Configuration DSC_SqlRS_CreateDependencies_Config
{
Import-DscResource -ModuleName 'PSDscResources' -ModuleVersion '2.12.0.0'
Import-DscResource -ModuleName 'StorageDsc' -ModuleVersion '4.9.0.0'
Import-DscResource -ModuleName 'WSManDsc' -ModuleVersion '3.1.1'
Import-DscResource -ModuleName 'SqlServerDsc'

node $AllNodes.NodeName
{
WSManConfig Config
{
IsSingleInstance = 'Yes'
MaxEnvelopeSizeKb = 600
}

User 'CreateReportingServicesServiceAccount'
{
Ensure = 'Present'
Expand Down
7 changes: 7 additions & 0 deletions tests/Integration/DSC_SqlRS_Default.config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,17 @@ Configuration DSC_SqlRS_CreateDependencies_Config
{
Import-DscResource -ModuleName 'PSDscResources' -ModuleVersion '2.12.0.0'
Import-DscResource -ModuleName 'StorageDsc' -ModuleVersion '4.9.0.0'
Import-DscResource -ModuleName 'WSManDsc' -ModuleVersion '3.1.1'
Import-DscResource -ModuleName 'SqlServerDsc'

node $AllNodes.NodeName
{
WSManConfig Config
{
IsSingleInstance = 'Yes'
MaxEnvelopeSizeKb = 600
}

User 'CreateReportingServicesServiceAccount'
{
Ensure = 'Present'
Expand Down
Loading