|
| 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 | +} |
0 commit comments