Skip to content

Commit

Permalink
Changes to xSQLAlias
Browse files Browse the repository at this point in the history
Fixed a bug if the alias did not exists then Get-ItemProperty would throw an error.
Fixed a bug so that the logic to create a new alias is triggered. If the alias did not exist it was the change-logic that created the alias, not the create-logic.
Fixed a bug in the create-logic that throw an error if the key ConnectTo already existed. Removed that New-Item since we already test for the existence of the key.
Also some style changes.

Changes to xSQLAlias
Improved testing and added a test for removal of alias
Bug found with the test, create-logic was called when it shouldn't

Aligned schemas with README.md
Also added add text that explains the use of the credential parameter (issue from dsccommunity#58)
  • Loading branch information
johlju committed Sep 11, 2016
1 parent b9de91a commit e235fe2
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function Set-TargetResource
# First two nodes will account for Syncronous Automatic Failover, Any additional will be Asyncronous
try
{
$nodes = Get-ClusterNode -cluster $sql.ClusterName -Verbose:$false | elect-Object -ExpandProperty name
$nodes = Get-ClusterNode -cluster $sql.ClusterName -Verbose:$false | Select-Object -ExpandProperty name
$syncNodes = $nodes | Select-Object -First 2
$asyncNodes = $nodes | Select-Object -Skip 2
$availabilityGroup = New-Object -typename Microsoft.SqlServer.Management.Smo.AvailabilityGroup -ArgumentList $SQL, $AvailabilityGroupName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@

[ClassVersion("1.0.0.0"), FriendlyName("xSQLAOGroupEnsure")]
class MSFT_xSQLAOGroupEnsure : OMI_BaseResource
{
[Key, ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
[Key] String AvailabilityGroupName;
[Write] String AvailabilityGroupNameListener;
[Write] String AvailabilityGroupNameIP[];
[Write] String AvailabilityGroupSubMask[];
[Write] Uint32 AvailabilityGroupPort;
[Write, ValueMap{"None","ReadOnly","ReadIntent"}, Values{"None","ReadOnly","ReadIntent"}] String ReadableSecondary;
[Write, ValueMap{"Primary","Secondary"}, Values{"Primary","Secondary"}] String AutoBackupPreference;
[Write] Uint32 BackupPriority;
[Write] Uint32 EndPointPort;
[Write] String SQLServer;
[Write] String SQLInstanceName;
[Required, EmbeddedInstance("MSFT_Credential"), Description("Credential to be used to Grant Permissions in SQL.")] String SetupCredential;
[Key, Description("Determines whether the availability group should be added or removed."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
[Key, Description("Name for availability group.")] String AvailabilityGroupName;
[Write, Description("Listener name for availability group.")] String AvailabilityGroupNameListener;
[Write, Description("List of IP addresses associated with listener.")] String AvailabilityGroupNameIP[];
[Write, Description("Network subnetmask for listener.")] String AvailabilityGroupSubMask[];
[Write, Description("Port availability group should listen on.")] Uint32 AvailabilityGroupPort;
[Write, Description("Mode secondaries should operate under (None, ReadOnly, ReadIntent)."), ValueMap{"None","ReadOnly","ReadIntent"}, Values{"None","ReadOnly","ReadIntent"}] String ReadableSecondary;
[Write, Description("Where backups should be backed up from (Primary, Secondary)."), ValueMap{"Primary","Secondary"}, Values{"Primary","Secondary"}] String AutoBackupPreference;
[Write, Description("The percentage weight for backup prority (default 50).")] Uint32 BackupPriority;
[Write, Description("he TCP port for the SQL AG Endpoint (default 5022).")] Uint32 EndPointPort;
[Write, Description("The SQL Server for the database.")] String SQLServer;
[Write, Description("The SQL instance for the database.")] String SQLInstanceName;
[Required, EmbeddedInstance("MSFT_Credential"), Description("Credential to be used to Grant Permissions on SQL Server, set this to $null to use Windows Authentication.")] String SetupCredential;
};

103 changes: 59 additions & 44 deletions DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function Get-TargetResource

if ($null -ne (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue))
{
$itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name
$itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue

$returnValue.Name = $Name
$itemConfig = $itemValue."$Name" -split ','
Expand Down Expand Up @@ -97,49 +97,59 @@ function Set-TargetResource
# Logic based on ensure value Present
if ($Ensure -eq 'Present')
{
if ($PSCmdlet.ShouldProcess("'$Name'","Replace the Client Alias"))
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)
{

# Update the registry
if (Test-Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo')
if ($PSCmdlet.ShouldProcess($Name,"Changing the client alias (64-bit)"))
{
Write-Debug -Message 'Check if value requires changing'
$currentValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name"
if ($itemValue -ne $currentValue)
{
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
}
else
}
elseif ($null -eq $currentValue)
{
if ($PSCmdlet.ShouldProcess($Name,"Create client alias (64-bit)"))
{
Write-Debug -Message 'New-Item'
New-Item -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' | Out-Null
if (!(Test-Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo'))
{
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
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue | Out-Null
}
}

Write-Debug -Message 'Check OSArchitecture'
# If this is a 64 bit machine also update Wow6432Node
if ((Get-Wmiobject -Class win32_OperatingSystem).OSArchitecture -eq '64-bit')
Write-Debug -Message 'Check OSArchitecture'
# If this is a 64 bit machine also update Wow6432Node
if ((Get-Wmiobject -Class win32_OperatingSystem).OSArchitecture -eq '64-bit')
{
Write-Debug -Message 'Is 64Bit'
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 'Is 64Bit'
if (Test-Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo')
if ($PSCmdlet.ShouldProcess($Name,"Changing the client alias (32-bit)"))
{
Write-Debug -Message 'Check if value requires changing'
$currentValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name"
if ($itemValue -ne $currentValue)
{
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
}
else
}
elseif ($null -eq $currentValue)
{
if ($PSCmdlet.ShouldProcess($Name,"Create client alias (32-bit)"))
{
Write-Debug -Message 'New-Item'
New-Item -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo'
if (!(Test-Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo'))
{
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
New-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -Value $itemValue
}
}
}
Expand All @@ -148,20 +158,25 @@ function Set-TargetResource
# Logic based on ensure value Absent
if ($Ensure -eq 'Absent')
{
if ($PSCmdlet.ShouldProcess("'$Name'","Remove the Client Alias (if exists)"))
# If the base path doesn't exist then we don't need to do anything
if (Test-Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo')
{
# If the base path doesn't exist then we don't need to do anything
if (Test-Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo')

if ($PSCmdlet.ShouldProcess($Name,"Remove the client alias (64-bit)"))
{
Write-Debug -Message 'Remove-ItemProperty'
Remove-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name"
Remove-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name
}

Write-Debug -Message 'Check OSArchitecture'

Write-Debug -Message 'Check OSArchitecture'
# If this is a 64 bit machine also update Wow6432Node
if ((Get-Wmiobject -Class win32_OperatingSystem).OSArchitecture -eq '64-bit' -and (Test-Path -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo'))
# If this is a 64 bit machine also update Wow6432Node
if ((Get-Wmiobject -Class win32_OperatingSystem).OSArchitecture -eq '64-bit' -and (Test-Path -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo'))
{
if ($PSCmdlet.ShouldProcess($Name,"Remove the client alias (34-bit)"))
{
Write-Debug -Message 'Remove-ItemProperty Wow6432Node'
Remove-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name"
Remove-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name
}
}
}
Expand Down Expand Up @@ -203,12 +218,12 @@ function Test-TargetResource
if (Test-Path -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo')
{
Write-Debug -Message 'Alias registry container exists'
if ($null -ne (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" -ErrorAction SilentlyContinue))
if ($null -ne (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue))
{
Write-Debug -Message 'Existing alias found'
if ($Ensure -eq 'Present')
{
$itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name"
$itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue

$itemConfig = $itemValue."$Name" -split ','

Expand Down Expand Up @@ -250,10 +265,10 @@ function Test-TargetResource
if ((Get-Wmiobject -Class win32_OperatingSystem).OSArchitecture -eq '64-bit')
{
Write-Debug -Message 'Wow6432Node'
if ($null -ne (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name" -ErrorAction SilentlyContinue))
if ($null -ne (Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue))
{
Write-Debug -Message 'Existing alias found'
$itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name "$Name"
$itemValue = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' -Name $Name -ErrorAction SilentlyContinue

$itemConfig = $itemValue."$Name" -split ','

Expand Down
2 changes: 1 addition & 1 deletion DSCResources/MSFT_xSQLAlias/MSFT_xSQLAlias.schema.mof
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ class MSFT_xSQLAlias : OMI_BaseResource
[Key, Description("The SQL Server you are aliasing (the real SQL Server name).")] String ServerName;
[Write, Description("The TCP port SQL is listening on.")] Sint32 TCPPort;
[Read, Description("Named Pipes name from the Get-TargetResource method.")] String PipeName;
[Write, Description("Determines if Alias is to be Present or Absent."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
[Write, Description("Determines whether the alias should be added or removed."), ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
};
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,19 @@ Please check out common DSC Resources [contributing guidelines](https://github.c
* **SQLInstance**: The SQL instance for the database

###xSQLAOGroupEnsure
* **Ensure**: (key) An enumerated value that describes if Availability Group is to be present or absent.
* **AvailabilityGroupName** (key) Name for availability group
* **AvailabilityGroupNameListener** Listener name for availability group
* **AvailabilityGroupNameIP** List of IP addresses associated with listener
* **AvailabilityGroupSubMask** Network subnetmask for listener
* **AvailabilityGroupPort** Port availability group should listen on
* **ReadableSecondary** Mode secondaries should operate under (None, ReadOnly, ReadIntent)
* **AutoBackupPreference** Where backups should be backed up from (Primary,Secondary)
* **BackupPriority** The percentage weight for backup prority (default 50)
* **EndPointPort** The TCP port for the SQL AG Endpoint (default 5022)
* **SQLServer**: The SQL Server for the database
* **SQLInstance**: The SQL instance for the database
* **SetupCredential**: (Required) Credential to be used to Grant Permissions on SQL Server
* **Ensure**: (Key) Determines whether the availability group should be added or removed.
* **AvailabilityGroupName** (Key) Name for availability group.
* **AvailabilityGroupNameListener** Listener name for availability group.
* **AvailabilityGroupNameIP** List of IP addresses associated with listener.
* **AvailabilityGroupSubMask** Network subnetmask for listener.
* **AvailabilityGroupPort** Port availability group should listen on.
* **ReadableSecondary** Mode secondaries should operate under (None, ReadOnly, ReadIntent).
* **AutoBackupPreference** Where backups should be backed up from (Primary, Secondary).
* **BackupPriority** The percentage weight for backup prority (default 50).
* **EndPointPort** The TCP port for the SQL AG Endpoint (default 5022).
* **SQLServer**: The SQL Server for the database.
* **SQLInstance**: The SQL instance for the database.
* **SetupCredential**: (Required) Credential to be used to Grant Permissions on SQL Server, set this to $null to use Windows Authentication.

###xSQLServerAOJoin
* **Ensure**: (key) An enumerated value that describes if Replica is to be present or absent from availability group
Expand Down Expand Up @@ -330,7 +330,7 @@ Please check out common DSC Resources [contributing guidelines](https://github.c
* **Variable**: Creates a sqlcmd scripting variable for use in the sqlcmd script, and sets a value for the variable.

### xSqlAlias
* **Ensure**: Determines if Alias is to be Present or Absent.
* **Ensure**: Determines whether the alias should be added or removed.
* **Name**: (Key) The name of Alias (e.g. svr01\inst01).
* **ServerName**: (Key) The SQL Server you are aliasing (the real SQL Server name).
* **Protocol**: Protocol to use when connecting. Valid values are TCP or NP. NP is Named Pipes.
Expand Down
Loading

0 comments on commit e235fe2

Please sign in to comment.