From ccb88efc427775118a97464476e12bbb54842db9 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sat, 30 Dec 2017 15:41:47 +0100 Subject: [PATCH] Changes to SqlServiceAccount - Added a read-only parameter ServiceAccountName so correctly returns the service account name as a string (issue #982). --- CHANGELOG.md | 2 ++ .../MSFT_SqlServiceAccount.psm1 | 14 +++++++------- .../MSFT_SqlServiceAccount.schema.mof | 1 + .../en-US/MSFT_SqlServiceAccount.strings.psd1 | 2 +- README.md | 5 +++++ Tests/Unit/MSFT_SqlServiceAccount.Tests.ps1 | 16 ++++++++-------- 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6856f23eac..8b4fdb02c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ ([issue #930](https://github.com/PowerShell/SqlServerDsc/issues/930)). - Made the description of parameter RestartService more descriptive ([issue #960](https://github.com/PowerShell/SqlServerDsc/issues/960)). + - Added a read-only parameter ServiceAccountName so correctly returns the + service account name as a string ([issue #982](https://github.com/PowerShell/SqlServerDsc/issues/982)). - Added integration tests ([issue #980](https://github.com/PowerShell/SqlServerDsc/issues/980)). - Changes to SqlSetup - Added parameter `ASServerMode` to support installing Analysis Services in diff --git a/DSCResources/MSFT_SqlServiceAccount/MSFT_SqlServiceAccount.psm1 b/DSCResources/MSFT_SqlServiceAccount/MSFT_SqlServiceAccount.psm1 index e835cce868..51e8c06064 100644 --- a/DSCResources/MSFT_SqlServiceAccount/MSFT_SqlServiceAccount.psm1 +++ b/DSCResources/MSFT_SqlServiceAccount/MSFT_SqlServiceAccount.psm1 @@ -65,12 +65,12 @@ function Get-TargetResource # Replace a domain of '.' with the value for $ServerName $serviceAccountName = $serviceObject.ServiceAccount -ireplace '^([\.])\\(.*)$', "$ServerName\`$2" - # Return a hashtable with the service information + # Return a hash table with the service information return @{ - ServerName = $ServerName - InstanceName = $InstanceName - ServiceType = $serviceObject.Type - ServiceAccount = $serviceAccountName + ServerName = $ServerName + InstanceName = $InstanceName + ServiceType = $serviceObject.Type + ServiceAccountName = $serviceAccountName } } @@ -99,7 +99,7 @@ function Get-TargetResource Forces the service account to be updated. .EXAMPLE - Test-TargetResource -ServerName $env:COMPUTERNAME -SQLInstaneName MSSQLSERVER -ServiceType DatabaseEngine -ServiceAccount $account + Test-TargetResource -ServerName $env:COMPUTERNAME -InstanceName MSSQLSERVER -ServiceType DatabaseEngine -ServiceAccount $account #> function Test-TargetResource @@ -172,7 +172,7 @@ function Test-TargetResource Forces the service account to be updated. .EXAMPLE - Set-TargetResource -ServerName $env:COMPUTERNAME -SQLInstaneName MSSQLSERVER -ServiceType DatabaseEngine -ServiceAccount $account + Set-TargetResource -ServerName $env:COMPUTERNAME -InstanceName MSSQLSERVER -ServiceType DatabaseEngine -ServiceAccount $account #> function Set-TargetResource { diff --git a/DSCResources/MSFT_SqlServiceAccount/MSFT_SqlServiceAccount.schema.mof b/DSCResources/MSFT_SqlServiceAccount/MSFT_SqlServiceAccount.schema.mof index 3be1e26c4f..01e3d22f3e 100644 --- a/DSCResources/MSFT_SqlServiceAccount/MSFT_SqlServiceAccount.schema.mof +++ b/DSCResources/MSFT_SqlServiceAccount/MSFT_SqlServiceAccount.schema.mof @@ -7,4 +7,5 @@ class MSFT_SqlServiceAccount : OMI_BaseResource [Required, EmbeddedInstance("MSFT_Credential"), Description("The service account that should be used when running the service.")] String ServiceAccount; [Write, Description("Determines whether the service is automatically restarted when a change to the configuration was needed.")] Boolean RestartService; [Write, Description("Forces the service account to be updated. Useful for password changes.")] Boolean Force; + [Read, Description("Returns the service account username for the service.")] String ServiceAccountName; }; diff --git a/DSCResources/MSFT_SqlServiceAccount/en-US/MSFT_SqlServiceAccount.strings.psd1 b/DSCResources/MSFT_SqlServiceAccount/en-US/MSFT_SqlServiceAccount.strings.psd1 index f65eed259e..9facb6f92b 100644 --- a/DSCResources/MSFT_SqlServiceAccount/en-US/MSFT_SqlServiceAccount.strings.psd1 +++ b/DSCResources/MSFT_SqlServiceAccount/en-US/MSFT_SqlServiceAccount.strings.psd1 @@ -2,7 +2,7 @@ ConvertFrom-StringData @' ForceServiceAccountUpdate = Force specified, skipping tests. With this configuration, Test-TargetResource will always return 'False'. - CurrentServiceAccount = Current service account is '{0}' for {1}\{2}. + CurrentServiceAccount = Current service account is '{0}' for {1}\\{2}. ConnectingToWmi = Connecting to WMI on '{0}'. UpdatingServiceAccount = Setting service account to '{0}' for service {1}. RestartingService = Restarting '{0}' and any dependent services. diff --git a/README.md b/README.md index 966e570722..63a8889ac0 100644 --- a/README.md +++ b/README.md @@ -1259,6 +1259,11 @@ Manage the service account for SQL Server services. Useful for password changes. This will cause `Set-TargetResource` to be run on each consecutive run. +#### Read-Only Properties from Get-TargetResource + +* **`[String]` ServiceAccountName** _(Read)_: Returns the service account username + for the service. + #### Examples * [Run service under a user account](/Examples/Resources/SqlServiceAccount/1-ConfigureServiceAccount-UserAccount.ps1) diff --git a/Tests/Unit/MSFT_SqlServiceAccount.Tests.ps1 b/Tests/Unit/MSFT_SqlServiceAccount.Tests.ps1 index 61c7358520..85cbe3cc80 100644 --- a/Tests/Unit/MSFT_SqlServiceAccount.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServiceAccount.Tests.ps1 @@ -668,7 +668,7 @@ try $testServiceInformation.ServerName | Should -Be $mockSqlServer $testServiceInformation.InstanceName | Should -Be $mockNamedInstance $testServiceInformation.ServiceType | Should -Be 'SqlServer' - $testServiceInformation.ServiceAccount | Should -Be $mockDesiredServiceAccountName + $testServiceInformation.ServiceAccountName | Should -Be $mockDesiredServiceAccountName # Ensure mocks were properly used Assert-MockCalled -CommandName Import-SQLPSModule -Scope It -Exactly -Times 1 @@ -705,7 +705,7 @@ try $currentState = Get-TargetResource @defaultGetTargetResourceParameters # Validate the service account - $currentState.ServiceAccount | Should -Be $mockLocalServiceAccountName + $currentState.ServiceAccountName | Should -Be $mockLocalServiceAccountName # Ensure mocks were properly used Assert-MockCalled -CommandName New-Object -ParameterFilter $mockNewObject_ParameterFilter -Scope It -Exactly -Times 1 @@ -900,10 +900,10 @@ try } It 'Should throw the correct exception if SetServiceAccount call fails' { - $newObjectParms = $mockNewObjectParameters_DefaultInstance.Clone() - $newObjectParms.MockWith = $mockNewObject_ManagedComputer_DefaultInstance_SetServiceAccountException + $newObjectParameters = $mockNewObjectParameters_DefaultInstance.Clone() + $newObjectParameters.MockWith = $mockNewObject_ManagedComputer_DefaultInstance_SetServiceAccountException - Mock @newObjectParms + Mock @newObjectParameters $setTargetResourceParameters = $defaultSetTargetResourceParameters.Clone() @@ -984,10 +984,10 @@ try } It 'Should throw the correct exception if SetServiceAccount call fails' { - $newObjectParms = $mockNewObjectParameters_NamedInstance.Clone() - $newObjectParms.MockWith = $mockNewObject_ManagedComputer_NamedInstance_SetServiceAccountException + $newObjectParameters = $mockNewObjectParameters_NamedInstance.Clone() + $newObjectParameters.MockWith = $mockNewObject_ManagedComputer_NamedInstance_SetServiceAccountException - Mock @newObjectParms + Mock @newObjectParameters $setTargetResourceParameters = $defaultSetTargetResourceParameters.Clone()