Skip to content

Commit

Permalink
[#569] CR comments addressed.
Browse files Browse the repository at this point in the history
  • Loading branch information
bozho committed Jun 9, 2017
1 parent 8a771af commit a9b7894
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 55 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
the target node, which complicated deployments in non-domain scenarios. Using `PsDscRunAsCredential` solves
this problems for us.

- Fixed virtual directory creation for SQL Server 2016.
- Fixed virtual directory creation for SQL Server 2016 (issue #569).
- Added unit tests (issue #295).

- Changes to xSQLServerDatabase
Expand Down
155 changes: 101 additions & 54 deletions DSCResources/MSFT_xSQLServerRSConfig/MSFT_xSQLServerRSConfig.psm1
Original file line number Diff line number Diff line change
@@ -1,93 +1,124 @@
$currentPath = Split-Path -Parent $MyInvocation.MyCommand.Path
Write-Debug -Message "CurrentPath: $currentPath"
Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) `
-ChildPath 'xSQLServerHelper.psm1') `
-Force

# Load Common Code
Import-Module $currentPath\..\..\xSQLServerHelper.psm1 -Verbose:$false -ErrorAction Stop


<#
.SYNOPSIS
Gets the SQL Reporting Services initialization status.
.PARAMETER InstanceName
Name of the SQL Server Reporting Services instance to be configured.
.PARAMETER RSSQLServer
Name of the SQL Server to host the Reporting Service database.
.PARAMETER RSSQLInstanceName
Name of the SQL Server instance to host the Reporting Service database.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[parameter(Mandatory = $true)]
[Parameter(Mandatory = $true)]
[System.String]
$InstanceName,

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

[parameter(Mandatory = $true)]
[Parameter(Mandatory = $true)]
[System.String]
$RSSQLInstanceName
)

if(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS" -Name $InstanceName -ErrorAction SilentlyContinue)
$instanceNamesRegistryKey = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS'

if ( Get-ItemProperty -Path $instanceNamesRegistryKey -Name $InstanceName -ErrorAction SilentlyContinue )
{
$InstanceKey = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS" -Name $InstanceName).$InstanceName
$SQLVersion = ((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$InstanceKey\Setup" -Name "Version").Version).Split(".")[0]
$instanceId = (Get-ItemProperty -Path $instanceNamesRegistryKey -Name $InstanceName).$InstanceName
$sqlVersion = ((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instanceId\Setup" -Name 'Version').Version).Split('.')[0]

$RSConfig = Get-WmiObject -Class MSReportServer_ConfigurationSetting -Namespace "root\Microsoft\SQLServer\ReportServer\RS_$InstanceName\v$SQLVersion\Admin"
if($RSConfig.DatabaseServerName.Contains("\"))
$reportingServicesConfiguration = Get-WmiObject -Class MSReportServer_ConfigurationSetting -Namespace "root\Microsoft\SQLServer\ReportServer\RS_$InstanceName\v$sqlVersion\Admin"
if ( $reportingServicesConfiguration.DatabaseServerName.Contains('\') )
{
$RSSQLServer = $RSConfig.DatabaseServerName.Split("\")[0]
$RSSQLInstanceName = $RSConfig.DatabaseServerName.Split("\")[1]
$RSSQLServer = $reportingServicesConfiguration.DatabaseServerName.Split('\')[0]
$RSSQLInstanceName = $reportingServicesConfiguration.DatabaseServerName.Split('\')[1]
}
else
{
$RSSQLServer = $RSConfig.DatabaseServerName
$RSSQLInstanceName = "MSSQLSERVER"
$RSSQLServer = $reportingServicesConfiguration.DatabaseServerName
$RSSQLInstanceName = 'MSSQLSERVER'
}
$IsInitialized = $RSConfig.IsInitialized

$isInitialized = $reportingServicesConfiguration.IsInitialized
}
else
{
{
throw New-TerminatingError -ErrorType SSRSNotFound -FormatArgs @($InstanceName) -ErrorCategory ObjectNotFound
}

$returnValue = @{
InstanceName = $InstanceName
RSSQLServer = $RSSQLServer
RSSQLInstanceName = $RSSQLInstanceName
IsInitialized = $IsInitialized
IsInitialized = $isInitialized
}

$returnValue
}

<#
.SYNOPSIS
Initializes SQL Reporting Services.
.PARAMETER InstanceName
Name of the SQL Server Reporting Services instance to be configured.
.PARAMETER RSSQLServer
Name of the SQL Server to host the Reporting Service database.
.PARAMETER RSSQLInstanceName
Name of the SQL Server instance to host the Reporting Service database.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[Parameter(Mandatory = $true)]
[System.String]
$InstanceName,

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

[parameter(Mandatory = $true)]
[Parameter(Mandatory = $true)]
[System.String]
$RSSQLInstanceName
)

if(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS" -Name $InstanceName -ErrorAction SilentlyContinue)
$instanceNamesRegistryKey = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS'

if ( Get-ItemProperty -Path -Name $InstanceName -ErrorAction SilentlyContinue )
{
# smart import of the SQL module
Import-SQLPSModule

$InstanceKey = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS" -Name $InstanceName).$InstanceName
$SQLVersion = [int]((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$InstanceKey\Setup" -Name "Version").Version).Split(".")[0]
if($InstanceName -eq "MSSQLSERVER")
$instanceId = (Get-ItemProperty -Path $instanceNamesRegistryKey -Name $InstanceName).$InstanceName
$sqlVersion = [System.Int32]((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instanceId\Setup" -Name 'Version').Version).Split('.')[0]

if ( $InstanceName -eq 'MSSQLSERVER' )
{
$RSServiceName = "ReportServer"
$RSVirtualDirectory = "ReportServer"
$RMVirtualDirectory = "Reports"
$RSDatabase = "ReportServer"
$RSServiceName = 'ReportServer'
$RSVirtualDirectory = 'ReportServer'
$RMVirtualDirectory = 'Reports'
$RSDatabase = 'ReportServer'
}
else
{
Expand All @@ -96,71 +127,87 @@ function Set-TargetResource
$RMVirtualDirectory = "Reports_$InstanceName"
$RSDatabase = "ReportServer`$$InstanceName"
}
if($RSSQLInstanceName -eq "MSSQLSERVER")

if ( $RSSQLInstanceName -eq 'MSSQLSERVER' )
{
$RSConnection = "$RSSQLServer"
$reportingServicesConnnection = "$RSSQLServer"
}
else
{
$RSConnection = "$RSSQLServer\$RSSQLInstanceName"
$reportingServicesConnnection = "$RSSQLServer\$RSSQLInstanceName"
}
$Language = (Get-WMIObject -Class Win32_OperatingSystem -Namespace root/cimv2 -ErrorAction SilentlyContinue).OSLanguage
$RSConfig = Get-WmiObject -Class MSReportServer_ConfigurationSetting -Namespace "root\Microsoft\SQLServer\ReportServer\RS_$InstanceName\v$SQLVersion\Admin"
if($RSConfig.VirtualDirectoryReportServer -ne $RSVirtualDirectory)

$language = (Get-WMIObject -Class Win32_OperatingSystem -Namespace root/cimv2 -ErrorAction SilentlyContinue).OSLanguage
$reportingServicesConfiguration = Get-WmiObject -Class MSReportServer_ConfigurationSetting -Namespace "root\Microsoft\SQLServer\ReportServer\RS_$InstanceName\v$sqlVersion\Admin"

if ( $reportingServicesConfiguration.VirtualDirectoryReportServer -ne $RSVirtualDirectory )
{
$null = $RSConfig.SetVirtualDirectory("ReportServerWebService",$RSVirtualDirectory,$Language)
$null = $RSConfig.ReserveURL("ReportServerWebService","http://+:80",$Language)
$null = $reportingServicesConfiguration.SetVirtualDirectory('ReportServerWebService',$RSVirtualDirectory,$language)
$null = $reportingServicesConfiguration.ReserveURL('ReportServerWebService','http://+:80',$language)
}
if($RSConfig.VirtualDirectoryReportManager -ne $RMVirtualDirectory)

if ( $reportingServicesConfiguration.VirtualDirectoryReportManager -ne $RMVirtualDirectory )
{
# SSRS Web Portal application name changed in SQL Server 2016
# https://docs.microsoft.com/en-us/sql/reporting-services/breaking-changes-in-sql-server-reporting-services-in-sql-server-2016
$virtualDirectoryName = if ($SQLVersion -ge 13) { 'ReportServerWebApp' } else { 'ReportManager'}
$null = $RSConfig.SetVirtualDirectory($virtualDirectoryName,$RMVirtualDirectory,$Language)
$null = $RSConfig.ReserveURL($virtualDirectoryName,"http://+:80",$Language)
$virtualDirectoryName = if ($sqlVersion -ge 13) { 'ReportServerWebApp' } else { 'ReportManager'}
$null = $reportingServicesConfiguration.SetVirtualDirectory($virtualDirectoryName,$RMVirtualDirectory,$language)
$null = $reportingServicesConfiguration.ReserveURL($virtualDirectoryName,'http://+:80',$language)
}
$RSCreateScript = $RSConfig.GenerateDatabaseCreationScript($RSDatabase,$Language,$false)

$RSCreateScript = $reportingServicesConfiguration.GenerateDatabaseCreationScript($RSDatabase,$language,$false)

# Determine RS service account
$RSSvcAccountUsername = (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq $RSServiceName}).StartName
$RSRightsScript = $RSConfig.GenerateDatabaseRightsScript($RSSvcAccountUsername,$RSDatabase,$false,$true)
$RSRightsScript = $reportingServicesConfiguration.GenerateDatabaseRightsScript($RSSvcAccountUsername,$RSDatabase,$false,$true)

Invoke-Sqlcmd -ServerInstance $RSConnection -Query $RSCreateScript.Script
Invoke-Sqlcmd -ServerInstance $RSConnection -Query $RSRightsScript.Script
$null = $RSConfig.SetDatabaseConnection($RSConnection,$RSDatabase,2,"","")
$null = $RSConfig.InitializeReportServer($RSConfig.InstallationID)
Invoke-Sqlcmd -ServerInstance $reportingServicesConnnection -Query $RSCreateScript.Script
Invoke-Sqlcmd -ServerInstance $reportingServicesConnnection -Query $RSRightsScript.Script
$null = $reportingServicesConfiguration.SetDatabaseConnection($reportingServicesConnnection,$RSDatabase,2,'','')
$null = $reportingServicesConfiguration.InitializeReportServer($reportingServicesConfiguration.InstallationID)
}

if(!(Test-TargetResource @PSBoundParameters))
if ( !(Test-TargetResource @PSBoundParameters) )
{
throw New-TerminatingError -ErrorType TestFailedAfterSet -ErrorCategory InvalidResult
}
}

<#
.SYNOPSIS
Tests the SQL Reporting Services initialization status.
.PARAMETER InstanceName
Name of the SQL Server Reporting Services instance to be configured.
.PARAMETER RSSQLServer
Name of the SQL Server to host the Reporting Service database.
.PARAMETER RSSQLInstanceName
Name of the SQL Server instance to host the Reporting Service database.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[Parameter(Mandatory = $true)]
[System.String]
$InstanceName,

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

[parameter(Mandatory = $true)]
[Parameter(Mandatory = $true)]
[System.String]
$RSSQLInstanceName
)

$result = (Get-TargetResource @PSBoundParameters).IsInitialized

$result
}


Export-ModuleMember -Function *-TargetResource

0 comments on commit a9b7894

Please sign in to comment.