From 09fe0b6cdfec4dd4a49b0b110c980b1d5d0dcfad Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Sun, 21 Aug 2016 08:40:47 +0200 Subject: [PATCH] Changes to xSQLAlias I introduced a bug when doing code changes. Now it creates the key correctly before creating the alias on a new system. --- .../MSFT_xSQLAlias/MSFT_xSQLAlias.psm1 | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.psm1 b/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.psm1 index b826b3c530..ace2f0ecc2 100644 --- a/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.psm1 +++ b/DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.psm1 @@ -97,25 +97,29 @@ function Set-TargetResource # Logic based on ensure value Present if ($Ensure -eq 'Present') { - if (Test-Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo') + Write-Debug -Message 'Check if value requires changing' + + $currentValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue + if ($null -ne $currentValue -and $itemValue -ne $currentValue) { - Write-Debug -Message 'Check if value requires changing' - $currentValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue - if ($null -ne $currentValue -and $itemValue -ne $currentValue) + if ($PSCmdlet.ShouldProcess($Name,"Changing the client alias (64-bit)")) { - if ($PSCmdlet.ShouldProcess($Name,"Changing the client alias (64-bit)")) - { - Write-Debug -Message 'Set-ItemProperty' - Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue - } + Write-Debug -Message 'Set-ItemProperty' + Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue } - elseif ($null -eq $currentValue) + } + elseif ($null -eq $currentValue) + { + if ($PSCmdlet.ShouldProcess($Name,"Create client alias (64-bit)")) { - if ($PSCmdlet.ShouldProcess($Name,"Create client alias (64-bit)")) + if (!(Test-Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo')) { - Write-Debug -Message 'New-ItemProperty' - New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue | Out-Null + Write-Debug -Message 'New-Item' + New-Item -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' | Out-Null } + + Write-Debug -Message 'New-ItemProperty' + New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue | Out-Null } } @@ -124,25 +128,28 @@ function Set-TargetResource if ((Get-Wmiobject -Class win32_OperatingSystem).OSArchitecture -eq '64-bit') { Write-Debug -Message 'Is 64Bit' - if (Test-Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo') + Write-Debug -Message 'Check if value requires changing' + $currentValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue + if ($null -ne $currentValue -and $itemValue -ne $currentValue) { - Write-Debug -Message 'Check if value requires changing' - $currentValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue - if ($null -ne $currentValue -and $itemValue -ne $currentValue) + if ($PSCmdlet.ShouldProcess($Name,"Changing the client alias (32-bit)")) { - if ($PSCmdlet.ShouldProcess($Name,"Changing the client alias (32-bit)")) - { - Write-Debug -Message 'Set-ItemProperty' - Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue - } + Write-Debug -Message 'Set-ItemProperty' + Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue } - elseif ($null -eq $currentValue) + } + elseif ($null -eq $currentValue) + { + if ($PSCmdlet.ShouldProcess($Name,"Create client alias (32-bit)")) { - if ($PSCmdlet.ShouldProcess($Name,"Create client alias (32-bit)")) + if (!(Test-Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo')) { - Write-Debug -Message 'New-ItemProperty' - New-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue + Write-Debug -Message 'New-Item' + New-Item -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' | Out-Null } + + Write-Debug -Message 'New-ItemProperty' + New-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue } } }