Skip to content

Commit 0739722

Browse files
committed
Add WithExtendedProperties parameter to Get-SqlDscManagedComputerService function
1 parent ad72918 commit 0739722

File tree

5 files changed

+315
-89
lines changed

5 files changed

+315
-89
lines changed

source/Public/Get-SqlDscInstalledComponent.ps1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function Get-SqlDscInstalledComponent
4646

4747
Assert-ElevatedUser -ErrorAction 'Stop'
4848

49-
$serviceComponent = Get-SqlDscInstalledService -ServerName $ServerName
49+
$serviceComponent = Get-SqlDscManagedComputerService -ServerName $ServerName -WithExtendedProperties
5050

5151
$installedComponents = @()
5252

@@ -57,7 +57,16 @@ function Get-SqlDscInstalledComponent
5757
Feature = $null
5858
}
5959

60-
switch ($currentServiceComponent.ServiceType)
60+
$serviceType = if ($currentServiceComponent.ManagedServiceType)
61+
{
62+
$currentServiceComponent.ManagedServiceType
63+
}
64+
else
65+
{
66+
$currentServiceComponent.Type
67+
}
68+
69+
switch ($serviceType)
6170
{
6271
# TODO: Add a Test-command for the path HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL16.SQL2022\ConfigurationState\SQL_FullText_Adv
6372
'9'

source/Public/Get-SqlDscInstalledService.ps1

Lines changed: 0 additions & 86 deletions
This file was deleted.

source/Public/Get-SqlDscManagedComputerService.ps1

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
.PARAMETER ServiceType
2020
Specifies one or more service types to return the services for.
2121
22+
.PARAMETER WithExtendedProperties
23+
Specifies that extended properties should be added to each returned service
24+
object. This includes ManagedServiceType, ServiceExecutableVersion,
25+
ServiceStartupType, and ServiceInstanceName properties. Note that retrieving
26+
the executable version may take additional time.
27+
2228
.EXAMPLE
2329
Get-SqlDscManagedComputer | Get-SqlDscManagedComputerService
2430
@@ -45,6 +51,13 @@
4551
4652
Returns all the managed computer service objects for instance SQL2022.
4753
54+
.EXAMPLE
55+
Get-SqlDscManagedComputerService -WithExtendedProperties
56+
57+
Returns all the managed computer service objects for the current node
58+
with extended properties (ManagedServiceType, ServiceExecutableVersion,
59+
ServiceStartupType, and ServiceInstanceName) added to each service object.
60+
4861
.OUTPUTS
4962
`[Microsoft.SqlServer.Management.Smo.Wmi.Service[]]`
5063
#>
@@ -71,7 +84,11 @@ function Get-SqlDscManagedComputerService
7184
[Parameter()]
7285
[ValidateSet('DatabaseEngine', 'SQLServerAgent', 'Search', 'IntegrationServices', 'AnalysisServices', 'ReportingServices', 'SQLServerBrowser', 'NotificationServices')]
7386
[System.String[]]
74-
$ServiceType
87+
$ServiceType,
88+
89+
[Parameter()]
90+
[System.Management.Automation.SwitchParameter]
91+
$WithExtendedProperties
7592
)
7693

7794
begin
@@ -125,6 +142,39 @@ function Get-SqlDscManagedComputerService
125142
$_.Name -match ('\${0}$' -f $InstanceName)
126143
}
127144
}
145+
146+
if ($WithExtendedProperties.IsPresent)
147+
{
148+
foreach ($service in $serviceObject)
149+
{
150+
$convertedType = $service.Type | ConvertFrom-ManagedServiceType -ErrorAction 'SilentlyContinue'
151+
152+
$service | Add-Member -MemberType 'NoteProperty' -Name 'ManagedServiceType' -Value $convertedType -Force
153+
154+
$fileProductVersion = $null
155+
156+
$serviceExecutablePath = (($service.PathName -replace '"') -split ' -')[0]
157+
158+
if ((Test-Path -Path $serviceExecutablePath))
159+
{
160+
$fileProductVersion = [System.Version] (Get-FileVersionInformation -FilePath $serviceExecutablePath).ProductVersion
161+
}
162+
163+
$service | Add-Member -MemberType 'NoteProperty' -Name 'ServiceExecutableVersion' -Value $fileProductVersion -Force
164+
165+
$serviceStartupType = $service.StartMode | ConvertFrom-ServiceStartMode
166+
167+
$service | Add-Member -MemberType 'NoteProperty' -Name 'ServiceStartupType' -Value $serviceStartupType -Force
168+
169+
# Get InstanceName from the service name if it exists.
170+
$serviceInstanceName = if ($service.Name -match '\$(.*)$')
171+
{
172+
$Matches[1]
173+
}
174+
175+
$service | Add-Member -MemberType 'NoteProperty' -Name 'ServiceInstanceName' -Value $serviceInstanceName -Force
176+
}
177+
}
128178
}
129179

130180
return $serviceObject

tests/Integration/Commands/Get-SqlDscManagedComputerService.Integration.Tests.ps1

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,132 @@ Describe 'Get-SqlDscManagedComputerService' -Tag @('Integration_SQL2017', 'Integ
191191
$firstService.Parent.Name | Should -Be $script:mockServerName
192192
}
193193
}
194+
195+
Context 'When validating extended properties with WithExtendedProperties parameter' {
196+
It 'Should return services with all extended properties added' {
197+
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -WithExtendedProperties -ErrorAction 'Stop'
198+
199+
$result | Should -Not -BeNullOrEmpty
200+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
201+
202+
# Verify each service has all extended properties
203+
foreach ($service in $result)
204+
{
205+
$service.PSObject.Properties.Name | Should -Contain 'ManagedServiceType'
206+
$service.PSObject.Properties.Name | Should -Contain 'ServiceExecutableVersion'
207+
$service.PSObject.Properties.Name | Should -Contain 'ServiceStartupType'
208+
$service.PSObject.Properties.Name | Should -Contain 'ServiceInstanceName'
209+
210+
# If the service is a SQL Server service, verify it has a converted type
211+
if ($service.Type -eq 'SqlServer')
212+
{
213+
$service.ManagedServiceType | Should -Be 'DatabaseEngine'
214+
}
215+
elseif ($service.Type -eq 'SqlBrowser')
216+
{
217+
$service.ManagedServiceType | Should -Be 'SQLServerBrowser'
218+
}
219+
elseif ($service.Type -eq 'SqlAgent')
220+
{
221+
$service.ManagedServiceType | Should -Be 'SqlServerAgent'
222+
}
223+
224+
# Verify ServiceStartupType is normalized
225+
if ($service.StartMode -eq 'Auto')
226+
{
227+
$service.ServiceStartupType | Should -Be 'Automatic'
228+
}
229+
}
230+
}
231+
232+
It 'Should have ServiceInstanceName for named instances' {
233+
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -InstanceName $script:mockInstanceName -WithExtendedProperties -ErrorAction 'Stop'
234+
235+
if ($result)
236+
{
237+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
238+
239+
# All returned services should have ServiceInstanceName property
240+
foreach ($service in $result)
241+
{
242+
$service.PSObject.Properties.Name | Should -Contain 'ServiceInstanceName'
243+
$service.ServiceInstanceName | Should -Be $script:mockInstanceName
244+
}
245+
}
246+
}
247+
248+
It 'Should have ServiceExecutableVersion populated when executable exists' {
249+
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ServiceType 'DatabaseEngine' -WithExtendedProperties -ErrorAction 'Stop'
250+
251+
$result | Should -Not -BeNullOrEmpty
252+
253+
# At least one service should have a version
254+
$servicesWithVersion = $result | Where-Object -FilterScript { $null -ne $_.ServiceExecutableVersion }
255+
$servicesWithVersion | Should -Not -BeNullOrEmpty
256+
257+
# Verify version is of correct type
258+
foreach ($service in $servicesWithVersion)
259+
{
260+
$service.ServiceExecutableVersion | Should -BeOfType ([System.Version])
261+
}
262+
}
263+
264+
Context 'When filtering by ServiceType with WithExtendedProperties' {
265+
It 'Should return only Database Engine services with all extended properties' {
266+
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ServiceType 'DatabaseEngine' -WithExtendedProperties -ErrorAction 'Stop'
267+
268+
$result | Should -Not -BeNullOrEmpty
269+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
270+
271+
# All returned services should be of type SqlServer and have all extended properties
272+
foreach ($service in $result)
273+
{
274+
$service.Type | Should -Be 'SqlServer'
275+
$service.PSObject.Properties.Name | Should -Contain 'ManagedServiceType'
276+
$service.ManagedServiceType | Should -Be 'DatabaseEngine'
277+
$service.PSObject.Properties.Name | Should -Contain 'ServiceExecutableVersion'
278+
$service.PSObject.Properties.Name | Should -Contain 'ServiceStartupType'
279+
$service.PSObject.Properties.Name | Should -Contain 'ServiceInstanceName'
280+
}
281+
}
282+
}
283+
284+
Context 'When using managed computer object with WithExtendedProperties' {
285+
BeforeAll {
286+
$script:managedComputerObjectForExtended = Get-SqlDscManagedComputer -ServerName $script:mockServerName -ErrorAction 'Stop'
287+
}
288+
289+
It 'Should return services with all extended properties from pipeline' {
290+
$result = $script:managedComputerObjectForExtended | Get-SqlDscManagedComputerService -WithExtendedProperties -ErrorAction 'Stop'
291+
292+
$result | Should -Not -BeNullOrEmpty
293+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
294+
295+
# Verify each service has all extended properties
296+
foreach ($service in $result)
297+
{
298+
$service.PSObject.Properties.Name | Should -Contain 'ManagedServiceType'
299+
$service.PSObject.Properties.Name | Should -Contain 'ServiceExecutableVersion'
300+
$service.PSObject.Properties.Name | Should -Contain 'ServiceStartupType'
301+
$service.PSObject.Properties.Name | Should -Contain 'ServiceInstanceName'
302+
}
303+
}
304+
}
305+
}
306+
307+
Context 'When not using WithExtendedProperties parameter' {
308+
It 'Should not add extended properties to service objects' {
309+
$result = Get-SqlDscManagedComputerService -ServerName $script:mockServerName -ErrorAction 'Stop'
310+
311+
$result | Should -Not -BeNullOrEmpty
312+
$result | Should -BeOfType ([Microsoft.SqlServer.Management.Smo.Wmi.Service])
313+
314+
# Verify extended properties are NOT added
315+
$firstService = $result | Select-Object -First 1
316+
$firstService.PSObject.Properties.Name | Should -Not -Contain 'ManagedServiceType'
317+
$firstService.PSObject.Properties.Name | Should -Not -Contain 'ServiceExecutableVersion'
318+
$firstService.PSObject.Properties.Name | Should -Not -Contain 'ServiceStartupType'
319+
$firstService.PSObject.Properties.Name | Should -Not -Contain 'ServiceInstanceName'
320+
}
321+
}
194322
}

0 commit comments

Comments
 (0)