Skip to content

Commit

Permalink
SqlServiceAccount: Corrected issue with detecting service names (dscc…
Browse files Browse the repository at this point in the history
…ommunity#947)

- Changes to SqlServiceAccount
  - Default services are now properly detected
    ([issue dsccommunity#930](dsccommunity#930)).
  • Loading branch information
nabrond authored and johlju committed Dec 22, 2017
1 parent f8e885c commit 65c1dc2
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
- Changes to SqlAlias
- Fixed issue where exception was thrown if reg keys did not exist
([issue #949](https://github.com/PowerShell/SqlServerDsc/issues/949)).
- Changes to SqlServiceAccount
- Default services are now properly detected
([issue #930](https://github.com/PowerShell/SqlServerDsc/issues/930)).

## 10.0.0.0

Expand Down
99 changes: 86 additions & 13 deletions DSCResources/MSFT_SqlServiceAccount/MSFT_SqlServiceAccount.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -276,22 +276,12 @@ function Get-ServiceObject
# Connect to SQL WMI
$managedComputer = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $ServerName

# Change the regex pattern for a default instance
if ($InstanceName -ieq 'MSSQLServer')
{
$serviceNamePattern = '^MSSQLServer$'
}
else
{
$serviceNamePattern = ('\${0}$' -f $InstanceName)
}

# Get the proper enum value
$serviceTypeFilter = ConvertTo-ManagedServiceType -ServiceType $ServiceType
# Get the service name for the specified instance and type
$serviceNameFilter = Get-SqlServiceName -InstanceName $InstanceName -ServiceType $ServiceType

# Get the Service object for the specified instance/type
$serviceObject = $managedComputer.Services | Where-Object -FilterScript {
($_.Type -eq $serviceTypeFilter) -and ($_.Name -imatch $serviceNamePattern)
$_.Name -eq $serviceNameFilter
}

return $serviceObject
Expand Down Expand Up @@ -365,3 +355,86 @@ function ConvertTo-ManagedServiceType

return $serviceTypeValue -as [Microsoft.SqlServer.Management.Smo.Wmi.ManagedServiceType]
}

<#
.SYNOPSIS
Gets the name of a service based on the instance name and type.
.PARAMETER InstanceName
Name of the SQL instance.
.PARAMETER ServiceType
Type of service to be named. Must be one of the following:
DatabaseEngine, SQLServerAgent, Search, IntegrationServices, AnalysisServices, ReportingServices, SQLServerBrowser, NotificationServices.
.EXAMPLE
Get-SqlServiceName -InstanceName 'MSSQLSERVER' -ServiceType ReportingServices
#>
function Get-SqlServiceName
{
[CmdletBinding()]
param
(
[Parameter()]
[System.String]
$InstanceName = 'MSSQLSERVER',

[Parameter(Mandatory = $true)]
[ValidateSet('DatabaseEngine', 'SQLServerAgent', 'Search', 'IntegrationServices', 'AnalysisServices', 'ReportingServices', 'SQLServerBrowser', 'NotificationServices')]
[System.String]
$ServiceType
)

# Base path in the registry for service name definitions
$serviceRegistryKey = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Services'

# The value grabbed varies for a named vs default instance
if ($InstanceName -eq 'MSSQLSERVER')
{
$propertyName = 'Name'
$returnValue = '{0}'
}
else
{
$propertyName = 'LName'
$returnValue = '{0}{1}'
}

# Map the specified type to a ManagedServiceType
$managedServiceType = ConvertTo-ManagedServiceType -ServiceType $ServiceType

# Get the required naming property
$serviceTypeDefinition = Get-ChildItem -Path $serviceRegistryKey | Where-Object -FilterScript {
$_.GetValue('Type') -eq ($managedServiceType -as [int])
}

# Ensure we got a service definition
if ($serviceTypeDefinition)
{
# Multiple definitions found (thank you SSRS!)
if ($serviceTypeDefinition.Count -gt 0)
{
$serviceNamingScheme = $serviceTypeDefinition | ForEach-Object -Process {
$_.GetValue($propertyName)
} | Select-Object -Unique
}
else
{
$serviceNamingScheme = $serviceTypeDefinition.GetValue($propertyName)
}
}
else
{
$errorMessage = $script:localizedData.UnknownServiceType -f $ServiceType
New-InvalidArgumentException -Message $errorMessage -ArgumentName 'ServiceType'
}

if ([String]::IsNullOrEmpty($serviceNamingScheme))
{
$errorMessage = $script:localizedData.NotInstanceAware -f $ServiceType
New-InvalidResultException -Message $errorMessage
}

# Build the name of the service and return it
return ($returnValue -f $serviceNamingScheme, $InstanceName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ ConvertFrom-StringData @'
RestartingService = Restarting '{0}' and any dependent services.
ServiceNotFound = The {0} service on {1}\\{2} could not be found.
SetServiceAccountFailed = Unable to set the service account for {0} on {1}. Message {2}
UnknownServiceType = Unknown or unsupported service type '{0}' specified!
NotInstanceAware = Service type '{0}' is not instance aware.
'@
Loading

0 comments on commit 65c1dc2

Please sign in to comment.