|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Gets the RsReportServer.config configuration file as an XML object. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Gets the RsReportServer.config configuration file for SQL Server |
| 7 | + Reporting Services (SSRS) or Power BI Report Server (PBIRS) as an |
| 8 | + XML document object. This allows programmatic access to configuration |
| 9 | + settings using standard XML navigation or XPath queries. |
| 10 | +
|
| 11 | + The configuration file path is automatically determined from the |
| 12 | + instance's setup configuration in the registry when using the |
| 13 | + `InstanceName` or `SetupConfiguration` parameter. Alternatively, a direct |
| 14 | + file path can be specified using the `Path` parameter. |
| 15 | +
|
| 16 | + .PARAMETER InstanceName |
| 17 | + Specifies the name of the Reporting Services instance. This is typically |
| 18 | + 'SSRS' for SQL Server Reporting Services or 'PBIRS' for Power BI Report |
| 19 | + Server. This parameter is mandatory when not passing a configuration object |
| 20 | + or a direct path. |
| 21 | +
|
| 22 | + .PARAMETER SetupConfiguration |
| 23 | + Specifies the configuration object from `Get-SqlDscRSSetupConfiguration`. |
| 24 | + This can be piped from `Get-SqlDscRSSetupConfiguration`. The object must |
| 25 | + have a `ConfigFilePath` property containing the path to the configuration |
| 26 | + file. This parameter accepts pipeline input. |
| 27 | +
|
| 28 | + .PARAMETER Path |
| 29 | + Specifies the direct path to the RsReportServer.config file. Use this |
| 30 | + parameter to read configuration files from non-standard locations or |
| 31 | + backup copies. |
| 32 | +
|
| 33 | + .EXAMPLE |
| 34 | + Get-SqlDscRSConfigFile -InstanceName 'SSRS' |
| 35 | +
|
| 36 | + Returns the rsreportserver.config file content as an XML object for |
| 37 | + the SSRS instance. |
| 38 | +
|
| 39 | + .EXAMPLE |
| 40 | + Get-SqlDscRSConfigFile -InstanceName 'PBIRS' |
| 41 | +
|
| 42 | + Returns the rsreportserver.config file content as an XML object for |
| 43 | + the Power BI Report Server instance. |
| 44 | +
|
| 45 | + .EXAMPLE |
| 46 | + Get-SqlDscRSSetupConfiguration -InstanceName 'SSRS' | Get-SqlDscRSConfigFile |
| 47 | +
|
| 48 | + Gets the setup configuration for SSRS and pipes it to Get-SqlDscRSConfigFile |
| 49 | + to retrieve the configuration file as XML. |
| 50 | +
|
| 51 | + .EXAMPLE |
| 52 | + Get-SqlDscRSConfigFile -Path 'C:\Backup\rsreportserver.config' |
| 53 | +
|
| 54 | + Reads the configuration file from the specified path. |
| 55 | +
|
| 56 | + .EXAMPLE |
| 57 | + $config = Get-SqlDscRSConfigFile -InstanceName 'SSRS' |
| 58 | + $config.Configuration.Service.IsSchedulingService |
| 59 | +
|
| 60 | + Gets the config file and accesses the scheduling service setting directly |
| 61 | + using dot notation. |
| 62 | +
|
| 63 | + .EXAMPLE |
| 64 | + $config = Get-SqlDscRSConfigFile -InstanceName 'SSRS' |
| 65 | + $config.SelectSingleNode('//Authentication/AuthenticationTypes') |
| 66 | +
|
| 67 | + Uses XPath to query the authentication types configuration section. |
| 68 | +
|
| 69 | + .EXAMPLE |
| 70 | + $config = Get-SqlDscRSConfigFile -InstanceName 'SSRS' |
| 71 | + $config.SelectNodes('//Extension[@Name]') | ForEach-Object { $_.Name } |
| 72 | +
|
| 73 | + Uses XPath to list all extension names defined in the configuration. |
| 74 | +
|
| 75 | + .EXAMPLE |
| 76 | + $config = Get-SqlDscRSConfigFile -InstanceName 'SSRS' |
| 77 | + $config.Configuration.URLReservations.Application | |
| 78 | + Where-Object { $_.Name -eq 'ReportServerWebService' } | |
| 79 | + Select-Object -ExpandProperty URLs |
| 80 | +
|
| 81 | + Gets the URL reservations for the Report Server Web Service application. |
| 82 | +
|
| 83 | + .EXAMPLE |
| 84 | + $config = Get-SqlDscRSConfigFile -InstanceName 'SSRS' |
| 85 | + $smtpServer = $config.SelectSingleNode('//RSEmailDPConfiguration/SMTPServer') |
| 86 | + if ($smtpServer) { $smtpServer.InnerText } |
| 87 | +
|
| 88 | + Uses XPath to retrieve the SMTP server configuration for email delivery. |
| 89 | +
|
| 90 | + .INPUTS |
| 91 | + `System.Object` |
| 92 | +
|
| 93 | + Accepts the setup configuration object from `Get-SqlDscRSSetupConfiguration` via |
| 94 | + pipeline. |
| 95 | +
|
| 96 | + .OUTPUTS |
| 97 | + `System.Xml.XmlDocument` |
| 98 | +
|
| 99 | + Returns the configuration file content as an XML document object. |
| 100 | +
|
| 101 | + .NOTES |
| 102 | + For more information about the RsReportServer.config configuration file, |
| 103 | + see the Microsoft documentation: |
| 104 | + https://learn.microsoft.com/en-us/sql/reporting-services/report-server/rsreportserver-config-configuration-file |
| 105 | +#> |
| 106 | +function Get-SqlDscRSConfigFile |
| 107 | +{ |
| 108 | + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the examples use pipeline input and XPath the rule cannot validate.')] |
| 109 | + [CmdletBinding(DefaultParameterSetName = 'ByInstanceName')] |
| 110 | + [OutputType([System.Xml.XmlDocument])] |
| 111 | + param |
| 112 | + ( |
| 113 | + [Parameter(Mandatory = $true, ParameterSetName = 'ByInstanceName')] |
| 114 | + [System.String] |
| 115 | + $InstanceName, |
| 116 | + |
| 117 | + [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'ByConfiguration')] |
| 118 | + [System.Object] |
| 119 | + $SetupConfiguration, |
| 120 | + |
| 121 | + [Parameter(Mandatory = $true, ParameterSetName = 'ByPath')] |
| 122 | + [System.String] |
| 123 | + $Path |
| 124 | + ) |
| 125 | + |
| 126 | + process |
| 127 | + { |
| 128 | + $configFilePath = $null |
| 129 | + |
| 130 | + switch ($PSCmdlet.ParameterSetName) |
| 131 | + { |
| 132 | + 'ByInstanceName' |
| 133 | + { |
| 134 | + Write-Verbose -Message ($script:localizedData.Get_SqlDscRSConfigFile_GettingConfigFile -f $InstanceName) |
| 135 | + |
| 136 | + $setupConfiguration = Get-SqlDscRSSetupConfiguration -InstanceName $InstanceName |
| 137 | + |
| 138 | + if (-not $setupConfiguration) |
| 139 | + { |
| 140 | + $errorMessage = $script:localizedData.Get_SqlDscRSConfigFile_InstanceNotFound -f $InstanceName |
| 141 | + |
| 142 | + $errorRecord = New-ErrorRecord -Exception (New-InvalidOperationException -Message $errorMessage -PassThru) -ErrorId 'GSRSCF0001' -ErrorCategory 'ObjectNotFound' -TargetObject $InstanceName |
| 143 | + |
| 144 | + $PSCmdlet.ThrowTerminatingError($errorRecord) |
| 145 | + } |
| 146 | + |
| 147 | + if ([System.String]::IsNullOrEmpty($setupConfiguration.ConfigFilePath)) |
| 148 | + { |
| 149 | + $errorMessage = $script:localizedData.Get_SqlDscRSConfigFile_ConfigFilePathNotFound -f $InstanceName |
| 150 | + |
| 151 | + $errorRecord = New-ErrorRecord -Exception (New-InvalidOperationException -Message $errorMessage -PassThru) -ErrorId 'GSRSCF0002' -ErrorCategory 'ObjectNotFound' -TargetObject $InstanceName |
| 152 | + |
| 153 | + $PSCmdlet.ThrowTerminatingError($errorRecord) |
| 154 | + } |
| 155 | + |
| 156 | + $configFilePath = $setupConfiguration.ConfigFilePath |
| 157 | + } |
| 158 | + |
| 159 | + 'ByConfiguration' |
| 160 | + { |
| 161 | + Write-Verbose -Message ($script:localizedData.Get_SqlDscRSConfigFile_GettingConfigFile -f $SetupConfiguration.InstanceName) |
| 162 | + |
| 163 | + if ([System.String]::IsNullOrEmpty($SetupConfiguration.ConfigFilePath)) |
| 164 | + { |
| 165 | + $errorMessage = $script:localizedData.Get_SqlDscRSConfigFile_ConfigFilePathNotFound -f $SetupConfiguration.InstanceName |
| 166 | + |
| 167 | + $errorRecord = New-ErrorRecord -Exception (New-InvalidOperationException -Message $errorMessage -PassThru) -ErrorId 'GSRSCF0002' -ErrorCategory 'ObjectNotFound' -TargetObject $SetupConfiguration.InstanceName |
| 168 | + |
| 169 | + $PSCmdlet.ThrowTerminatingError($errorRecord) |
| 170 | + } |
| 171 | + |
| 172 | + $configFilePath = $SetupConfiguration.ConfigFilePath |
| 173 | + } |
| 174 | + |
| 175 | + 'ByPath' |
| 176 | + { |
| 177 | + Write-Verbose -Message ($script:localizedData.Get_SqlDscRSConfigFile_ReadingFromPath -f $Path) |
| 178 | + |
| 179 | + if (-not (Test-Path -Path $Path -PathType 'Leaf')) |
| 180 | + { |
| 181 | + $errorMessage = $script:localizedData.Get_SqlDscRSConfigFile_FileNotFound -f $Path |
| 182 | + |
| 183 | + $errorRecord = New-ErrorRecord -Exception (New-InvalidOperationException -Message $errorMessage -PassThru) -ErrorId 'GSRSCF0004' -ErrorCategory 'ObjectNotFound' -TargetObject $Path |
| 184 | + |
| 185 | + $PSCmdlet.ThrowTerminatingError($errorRecord) |
| 186 | + } |
| 187 | + |
| 188 | + $configFilePath = $Path |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + Write-Verbose -Message ($script:localizedData.Get_SqlDscRSConfigFile_FoundConfigFile -f $configFilePath) |
| 193 | + |
| 194 | + try |
| 195 | + { |
| 196 | + [xml] $configXml = Get-Content -Path $configFilePath -Raw -Force -ErrorAction 'Stop' |
| 197 | + } |
| 198 | + catch |
| 199 | + { |
| 200 | + $errorMessage = $script:localizedData.Get_SqlDscRSConfigFile_FailedToReadConfigFile -f $configFilePath, $_.Exception.Message |
| 201 | + |
| 202 | + $errorRecord = New-ErrorRecord -Exception (New-InvalidOperationException -Message $errorMessage -ErrorRecord $_ -PassThru) -ErrorId 'GSRSCF0003' -ErrorCategory 'ReadError' -TargetObject $configFilePath |
| 203 | + |
| 204 | + $PSCmdlet.ThrowTerminatingError($errorRecord) |
| 205 | + } |
| 206 | + |
| 207 | + return $configXml |
| 208 | + } |
| 209 | +} |
0 commit comments