Skip to content

Commit f583104

Browse files
authored
Add commands for Start/Stop Report Server (#2412)
1 parent 70c5846 commit f583104

18 files changed

Lines changed: 2063 additions & 1 deletion

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
### Added
99

10+
- Added public commands `Start-SqlDscRSWindowsService`, `Stop-SqlDscRSWindowsService`, `Start-SqlDscRSWebService`, and `Stop-SqlDscRSWebService` to manage Reporting Services Windows and web services using the `SetServiceState` WMI method.
1011
- SqlServerDsc
1112
- Added class `ReportServerUri` to represent URLs returned by the
1213
`GetReportServerUrls` CIM method on `MSReportServer_Instance`.

azure-pipelines.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,10 @@ stages:
605605
'tests/Integration/Commands/New-SqlDscRSEncryptionKey.Integration.Tests.ps1'
606606
'tests/Integration/Commands/Remove-SqlDscRSEncryptedInformation.Integration.Tests.ps1'
607607
# Group 9
608+
'tests/Integration/Commands/Stop-SqlDscRSWindowsService.Integration.Tests.ps1'
609+
'tests/Integration/Commands/Start-SqlDscRSWindowsService.Integration.Tests.ps1'
610+
'tests/Integration/Commands/Stop-SqlDscRSWebService.Integration.Tests.ps1'
611+
'tests/Integration/Commands/Start-SqlDscRSWebService.Integration.Tests.ps1'
608612
'tests/Integration/Commands/Uninstall-SqlDscReportingService.Integration.Tests.ps1'
609613
)
610614
name: test
@@ -708,6 +712,10 @@ stages:
708712
'tests/Integration/Commands/New-SqlDscRSEncryptionKey.Integration.Tests.ps1'
709713
'tests/Integration/Commands/Remove-SqlDscRSEncryptedInformation.Integration.Tests.ps1'
710714
# Group 9
715+
'tests/Integration/Commands/Stop-SqlDscRSWindowsService.Integration.Tests.ps1'
716+
'tests/Integration/Commands/Start-SqlDscRSWindowsService.Integration.Tests.ps1'
717+
'tests/Integration/Commands/Stop-SqlDscRSWebService.Integration.Tests.ps1'
718+
'tests/Integration/Commands/Start-SqlDscRSWebService.Integration.Tests.ps1'
711719
'tests/Integration/Commands/Uninstall-SqlDscPowerBIReportServer.Integration.Tests.ps1'
712720
)
713721
name: test
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<#
2+
.SYNOPSIS
3+
Gets the service state arguments for the SetServiceState method.
4+
5+
.DESCRIPTION
6+
A helper function that reads the current service state from the
7+
Reporting Services configuration CIM instance and returns the arguments
8+
needed for the SetServiceState WMI method. This function preserves the
9+
state of services that are not being changed.
10+
11+
The function reads the `IsWindowsServiceEnabled` and `IsWebServiceEnabled`
12+
properties from the configuration instance and applies the requested
13+
change.
14+
15+
.PARAMETER Configuration
16+
The CIM instance object that contains the Reporting Services configuration.
17+
This is typically obtained from `Get-SqlDscRSConfiguration`.
18+
19+
.PARAMETER EnableWindowsService
20+
If specified, sets the `EnableWindowsService` argument to `$true`.
21+
22+
.PARAMETER DisableWindowsService
23+
If specified, sets the `EnableWindowsService` argument to `$false`.
24+
25+
.PARAMETER EnableWebService
26+
If specified, sets the `EnableWebService` argument to `$true`.
27+
28+
.PARAMETER DisableWebService
29+
If specified, sets the `EnableWebService` argument to `$false`.
30+
31+
.INPUTS
32+
None.
33+
34+
.OUTPUTS
35+
System.Collections.Hashtable
36+
37+
Returns a hashtable with the following keys:
38+
- EnableWindowsService: Boolean value for the Windows service state.
39+
- EnableWebService: Boolean value for the web service state.
40+
- EnableReportManager: Boolean value (deprecated, always matches EnableWebService).
41+
42+
.EXAMPLE
43+
$config = Get-SqlDscRSConfiguration -InstanceName 'SSRS'
44+
Get-RSServiceState -Configuration $config -EnableWindowsService
45+
46+
Returns a hashtable with EnableWindowsService set to $true and the
47+
current state of EnableWebService preserved.
48+
49+
.EXAMPLE
50+
$config = Get-SqlDscRSConfiguration -InstanceName 'SSRS'
51+
Get-RSServiceState -Configuration $config -DisableWebService
52+
53+
Returns a hashtable with EnableWebService set to $false and the
54+
current state of EnableWindowsService preserved.
55+
#>
56+
function Get-RSServiceState
57+
{
58+
[CmdletBinding()]
59+
[OutputType([System.Collections.Hashtable])]
60+
param
61+
(
62+
[Parameter(Mandatory = $true)]
63+
[System.Object]
64+
$Configuration,
65+
66+
[Parameter()]
67+
[System.Management.Automation.SwitchParameter]
68+
$EnableWindowsService,
69+
70+
[Parameter()]
71+
[System.Management.Automation.SwitchParameter]
72+
$DisableWindowsService,
73+
74+
[Parameter()]
75+
[System.Management.Automation.SwitchParameter]
76+
$EnableWebService,
77+
78+
[Parameter()]
79+
[System.Management.Automation.SwitchParameter]
80+
$DisableWebService
81+
)
82+
83+
# Get the current state from the configuration instance.
84+
$currentWindowsServiceEnabled = $Configuration.IsWindowsServiceEnabled
85+
$currentWebServiceEnabled = $Configuration.IsWebServiceEnabled
86+
87+
Write-Debug -Message ($script:localizedData.Get_RSServiceState_CurrentState -f $currentWindowsServiceEnabled, $currentWebServiceEnabled)
88+
89+
# Initialize the result with current state.
90+
$windowsServiceState = $currentWindowsServiceEnabled
91+
$webServiceState = $currentWebServiceEnabled
92+
93+
# Apply the requested changes.
94+
if ($EnableWindowsService.IsPresent)
95+
{
96+
$windowsServiceState = $true
97+
}
98+
elseif ($DisableWindowsService.IsPresent)
99+
{
100+
$windowsServiceState = $false
101+
}
102+
103+
if ($EnableWebService.IsPresent)
104+
{
105+
$webServiceState = $true
106+
}
107+
elseif ($DisableWebService.IsPresent)
108+
{
109+
$webServiceState = $false
110+
}
111+
112+
Write-Debug -Message ($script:localizedData.Get_RSServiceState_NewState -f $windowsServiceState, $webServiceState)
113+
114+
# Return the arguments hashtable for SetServiceState.
115+
return @{
116+
EnableWindowsService = $windowsServiceState
117+
EnableWebService = $webServiceState
118+
# EnableReportManager is deprecated since SQL Server 2016 CU2, but we still need to pass it.
119+
EnableReportManager = $webServiceState
120+
}
121+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<#
2+
.SYNOPSIS
3+
Starts the Reporting Services web service.
4+
5+
.DESCRIPTION
6+
Starts the SQL Server Reporting Services or Power BI Report Server
7+
web service by calling the `SetServiceState` WMI method with
8+
`EnableWebService` set to `$true`.
9+
10+
This command preserves the current state of the Windows service. If the
11+
web service is already enabled, the command proceeds without error
12+
(idempotent behavior).
13+
14+
The configuration CIM instance can be obtained using the
15+
`Get-SqlDscRSConfiguration` command and passed via the pipeline.
16+
17+
.PARAMETER Configuration
18+
Specifies the `MSReportServer_ConfigurationSetting` CIM instance for
19+
the Reporting Services instance. This can be obtained using the
20+
`Get-SqlDscRSConfiguration` command. This parameter accepts pipeline
21+
input.
22+
23+
.PARAMETER Force
24+
If specified, suppresses the confirmation prompt.
25+
26+
.EXAMPLE
27+
Get-SqlDscRSConfiguration -InstanceName 'SSRS' | Start-SqlDscRSWebService
28+
29+
Starts the web service for the SSRS instance by piping the configuration
30+
from `Get-SqlDscRSConfiguration`.
31+
32+
.EXAMPLE
33+
$config = Get-SqlDscRSConfiguration -InstanceName 'SSRS'
34+
Start-SqlDscRSWebService -Configuration $config -Force
35+
36+
Starts the web service for the SSRS instance without confirmation.
37+
38+
.INPUTS
39+
`Microsoft.Management.Infrastructure.CimInstance`
40+
41+
Accepts MSReportServer_ConfigurationSetting CIM instance via pipeline.
42+
43+
.OUTPUTS
44+
None.
45+
#>
46+
function Start-SqlDscRSWebService
47+
{
48+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the examples use pipeline input the rule cannot validate.')]
49+
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
50+
[OutputType()]
51+
param
52+
(
53+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
54+
[System.Object]
55+
$Configuration,
56+
57+
[Parameter()]
58+
[System.Management.Automation.SwitchParameter]
59+
$Force
60+
)
61+
62+
process
63+
{
64+
if ($Force.IsPresent -and -not $Confirm)
65+
{
66+
$ConfirmPreference = 'None'
67+
}
68+
69+
$instanceName = $Configuration.InstanceName
70+
71+
Write-Verbose -Message ($script:localizedData.Start_SqlDscRSWebService_Starting -f $instanceName)
72+
73+
$descriptionMessage = $script:localizedData.Start_SqlDscRSWebService_ShouldProcessDescription -f $instanceName
74+
$confirmationMessage = $script:localizedData.Start_SqlDscRSWebService_ShouldProcessConfirmation -f $instanceName
75+
$captionMessage = $script:localizedData.Start_SqlDscRSWebService_ShouldProcessCaption
76+
77+
if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage))
78+
{
79+
$serviceStateArguments = Get-RSServiceState -Configuration $Configuration -EnableWebService
80+
81+
$invokeRsCimMethodParameters = @{
82+
CimInstance = $Configuration
83+
MethodName = 'SetServiceState'
84+
Arguments = $serviceStateArguments
85+
}
86+
87+
try
88+
{
89+
$null = Invoke-RsCimMethod @invokeRsCimMethodParameters
90+
}
91+
catch
92+
{
93+
$errorMessage = $script:localizedData.Start_SqlDscRSWebService_FailedToStart -f $instanceName
94+
95+
$exception = New-Exception -Message $errorMessage -ErrorRecord $_
96+
97+
$errorRecord = New-ErrorRecord -Exception $exception -ErrorId 'SSRSWBS0001' -ErrorCategory 'InvalidOperation' -TargetObject $Configuration
98+
99+
$PSCmdlet.ThrowTerminatingError($errorRecord)
100+
}
101+
}
102+
}
103+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<#
2+
.SYNOPSIS
3+
Starts the Reporting Services Windows service.
4+
5+
.DESCRIPTION
6+
Starts the SQL Server Reporting Services or Power BI Report Server
7+
Windows service by calling the `SetServiceState` WMI method with
8+
`EnableWindowsService` set to `$true`.
9+
10+
This command preserves the current state of the web service. If the
11+
Windows service is already enabled, the command proceeds without error
12+
(idempotent behavior).
13+
14+
The configuration CIM instance can be obtained using the
15+
`Get-SqlDscRSConfiguration` command and passed via the pipeline.
16+
17+
.PARAMETER Configuration
18+
Specifies the `MSReportServer_ConfigurationSetting` CIM instance for
19+
the Reporting Services instance. This can be obtained using the
20+
`Get-SqlDscRSConfiguration` command. This parameter accepts pipeline
21+
input.
22+
23+
.PARAMETER Force
24+
If specified, suppresses the confirmation prompt.
25+
26+
.EXAMPLE
27+
Get-SqlDscRSConfiguration -InstanceName 'SSRS' | Start-SqlDscRSWindowsService
28+
29+
Starts the Windows service for the SSRS instance by piping the configuration
30+
from `Get-SqlDscRSConfiguration`.
31+
32+
.EXAMPLE
33+
$config = Get-SqlDscRSConfiguration -InstanceName 'SSRS'
34+
Start-SqlDscRSWindowsService -Configuration $config -Force
35+
36+
Starts the Windows service for the SSRS instance without confirmation.
37+
38+
.INPUTS
39+
`Microsoft.Management.Infrastructure.CimInstance`
40+
41+
Accepts MSReportServer_ConfigurationSetting CIM instance via pipeline.
42+
43+
.OUTPUTS
44+
None.
45+
#>
46+
function Start-SqlDscRSWindowsService
47+
{
48+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the examples use pipeline input the rule cannot validate.')]
49+
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
50+
[OutputType()]
51+
param
52+
(
53+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
54+
[System.Object]
55+
$Configuration,
56+
57+
[Parameter()]
58+
[System.Management.Automation.SwitchParameter]
59+
$Force
60+
)
61+
62+
process
63+
{
64+
if ($Force.IsPresent -and -not $Confirm)
65+
{
66+
$ConfirmPreference = 'None'
67+
}
68+
69+
$instanceName = $Configuration.InstanceName
70+
71+
Write-Verbose -Message ($script:localizedData.Start_SqlDscRSWindowsService_Starting -f $instanceName)
72+
73+
$descriptionMessage = $script:localizedData.Start_SqlDscRSWindowsService_ShouldProcessDescription -f $instanceName
74+
$confirmationMessage = $script:localizedData.Start_SqlDscRSWindowsService_ShouldProcessConfirmation -f $instanceName
75+
$captionMessage = $script:localizedData.Start_SqlDscRSWindowsService_ShouldProcessCaption
76+
77+
if ($PSCmdlet.ShouldProcess($descriptionMessage, $confirmationMessage, $captionMessage))
78+
{
79+
$serviceStateArguments = Get-RSServiceState -Configuration $Configuration -EnableWindowsService
80+
81+
$invokeRsCimMethodParameters = @{
82+
CimInstance = $Configuration
83+
MethodName = 'SetServiceState'
84+
Arguments = $serviceStateArguments
85+
}
86+
87+
try
88+
{
89+
$null = Invoke-RsCimMethod @invokeRsCimMethodParameters
90+
}
91+
catch
92+
{
93+
$errorMessage = $script:localizedData.Start_SqlDscRSWindowsService_FailedToStart -f $instanceName
94+
95+
$exception = New-Exception -Message $errorMessage -ErrorRecord $_
96+
97+
$errorRecord = New-ErrorRecord -Exception $exception -ErrorId 'SSRSWS0001' -ErrorCategory 'InvalidOperation' -TargetObject $Configuration
98+
99+
$PSCmdlet.ThrowTerminatingError($errorRecord)
100+
}
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)