diff --git a/scripts/windows/dsc/PrepareSQLServer-AGDB.ps1 b/scripts/windows/dsc/PrepareSQLServer-AGDB.ps1 new file mode 100644 index 000000000..701db20b3 --- /dev/null +++ b/scripts/windows/dsc/PrepareSQLServer-AGDB.ps1 @@ -0,0 +1,127 @@ +configuration SQLServerDBDsc +{ + param + ( + [Parameter(Mandatory)] + [String]$DomainName, + + [String]$DomainNetbiosName=(Get-NetBIOSName -DomainName $DomainName), + + [Parameter(Mandatory)] + [System.Management.Automation.PSCredential]$Admincreds, + + [Parameter(Mandatory)] + [System.Management.Automation.PSCredential]$SQLServiceCreds, + + [Parameter(Mandatory=$true)] + [String]$ClusterName, + + [Parameter(Mandatory=$true)] + [String]$ClusterOwnerNode, + + [Parameter(Mandatory=$true)] + [String]$ClusterIP, + + [Parameter(Mandatory=$true)] + [String]$witnessStorageBlobEndpoint, + + [Parameter(Mandatory=$true)] + [String]$witnessStorageAccountKey, + + [Int]$RetryCount=20, + [Int]$RetryIntervalSec=30 + ) + + Import-DscResource -ModuleName PSDesiredStateConfiguration, xSmbShare, xComputerManagement, xNetworking, xActiveDirectory, xFailoverCluster, SqlServer, SqlServerDsc + [System.Management.Automation.PSCredential]$DomainCreds = New-Object System.Management.Automation.PSCredential ("${DomainNetbiosName}\$($Admincreds.UserName)", $Admincreds.Password) + + $ipcomponents = $ClusterIP.Split('.') + $ipcomponents[3] = [convert]::ToString(([convert]::ToInt32($ipcomponents[3])) + 1) + $ipdummy = $ipcomponents -join "." + $ClusterNameDummy = "c" + $ClusterName + + $suri = [System.uri]$witnessStorageBlobEndpoint + $uricomp = $suri.Host.split('.') + + $witnessStorageAccount = $uriComp[0] + $witnessEndpoint = $uricomp[-3] + "." + $uricomp[-2] + "." + $uricomp[-1] + + $computerName = $env:COMPUTERNAME + $domainUserName = $DomainCreds.UserName.ToString() + + Node localhost + { + if ($ClusterOwnerNode -eq $env:COMPUTERNAME) + { + File BackupDirectory + { + Ensure = "Present" + Type = "Directory" + DestinationPath = "F:\Backup" + # DependsOn = '[SqlAGReplica]AddReplica' + } + + xSMBShare DBBackupShare + { + Name = "DBBackup" + Path = "F:\Backup" + Ensure = "Present" + FullAccess = $DomainCreds.UserName + Description = "Backup share for SQL Server" + DependsOn = "[File]BackupDirectory" + } + + SqlDatabase Create_Database + { + Ensure = 'Present' + ServerName = $env:COMPUTERNAME + InstanceName = 'MSSQLSERVER' + Name = 'Ha-Sample' + PsDscRunAsCredential = $DomainCreds + DependsOn = "[xSMBShare]DBBackupShare" + } + + SqlAGDatabase AddDatabaseToAG + { + AvailabilityGroupName = $ClusterName + BackupPath = "\\" + $env:COMPUTERNAME + "\DBBackup" + DatabaseName = 'Ha-Sample' + InstanceName = 'MSSQLSERVER' + ServerName = $env:COMPUTERNAME + Ensure = 'Present' + ProcessOnlyOnActiveNode = $true + PsDscRunAsCredential = $DomainCreds + DependsOn = "[SqlDatabase]Create_Database" + } + } + + LocalConfigurationManager + { + RebootNodeIfNeeded = $true + } + } +} + +function Get-NetBIOSName +{ + [OutputType([string])] + param( + [string]$DomainName + ) + + if ($DomainName.Contains('.')) { + $length=$DomainName.IndexOf('.') + if ( $length -ge 16) { + $length=15 + } + return $DomainName.Substring(0,$length) + } + else { + if ($DomainName.Length -gt 15) { + return $DomainName.Substring(0,15) + } + else { + return $DomainName + } + } +} \ No newline at end of file diff --git a/scripts/windows/dsc/PrepareSQLServer.ps1 b/scripts/windows/dsc/PrepareSQLServer.ps1 index b7f7708ba..440c5a30b 100644 --- a/scripts/windows/dsc/PrepareSQLServer.ps1 +++ b/scripts/windows/dsc/PrepareSQLServer.ps1 @@ -335,7 +335,6 @@ configuration SQLServerPrepareDsc DependsOn = "[SqlAGListener]AvailabilityGroupListener" PsDscRunAsCredential = $DomainCreds } - } else { xWaitForCluster WaitForCluster { @@ -375,26 +374,26 @@ configuration SQLServerPrepareDsc DependsOn = "[Script]JoinExistingCluster" } - # Create a DatabaseMirroring endpoint - SqlServerEndpoint HADREndpoint - { - EndPointName = 'HADR' - Ensure = 'Present' - Port = 5022 - ServerName = $env:COMPUTERNAME - InstanceName = 'MSSQLSERVER' - DependsOn = "[SqlAlwaysOnService]EnableAlwaysOn" - } - + # Create a DatabaseMirroring endpoint + SqlServerEndpoint HADREndpoint + { + EndPointName = 'HADR' + Ensure = 'Present' + Port = 5022 + ServerName = $env:COMPUTERNAME + InstanceName = 'MSSQLSERVER' + DependsOn = "[SqlAlwaysOnService]EnableAlwaysOn" + } - SqlWaitForAG WaitForAG - { - Name = $ClusterName - RetryIntervalSec = 20 - RetryCount = 30 - PsDscRunAsCredential = $DomainCreds - DependsOn = "[SqlServerEndpoint]HADREndpoint","[SqlServerRole]AddDomainAdminAccountToSysAdmin" - } + + SqlWaitForAG WaitForAG + { + Name = $ClusterName + RetryIntervalSec = 20 + RetryCount = 30 + PsDscRunAsCredential = $DomainCreds + DependsOn = "[SqlServerEndpoint]HADREndpoint","[SqlServerRole]AddDomainAdminAccountToSysAdmin" + } # Add the availability group replica to the availability group SqlAGReplica AddReplica @@ -412,69 +411,8 @@ configuration SQLServerPrepareDsc DependsOn = "[SqlWaitForAG]WaitForAG" } - File BackupDirectory - { - Ensure = "Present" - Type = "Directory" - DestinationPath = "F:\Backup" - DependsOn = '[SqlAGReplica]AddReplica' - } - - xSMBShare DBBackupShare - { - Name = "DBBackup" - Path = "F:\Backup" - Ensure = "Present" - FullAccess = $DomainCreds.UserName - Description = "Backup share for SQL Server" - DependsOn = "[File]BackupDirectory" - } - - SqlDatabase Create_Database - { - Ensure = 'Present' - ServerName = $env:COMPUTERNAME - InstanceName = 'MSSQLSERVER' - Name = 'Ha-Sample' - PsDscRunAsCredential = $DomainCreds - DependsOn = "[xSMBShare]DBBackupShare" - } - - Script BackupDB - { - SetScript = - { - import-module sqlps; - $container = "\\" + $env:COMPUTERNAME + "\DBBackup"; - $FileName = 'Ha-Sample.bak'; - $database = 'Ha-Sample'; - $BackupFile = $container + '/' + $FileName; - $serverInstance = $env:COMPUTERNAME + '/' + 'MSSQLSERVER' - - Backup-SqlDatabase -ServerInstance $serverInstance –Database $database -BackupFile $BackupFile; - - } - TestScript = { return $false } - GetScript = { @{ Result = ("") } } - DependsOn = "[SqlDatabase]Create_Database" - PsDscRunAsCredential = $DomainCreds - } - - SqlAGDatabase AddDatabaseToAG - { - AvailabilityGroupName = $ClusterName - BackupPath = "\\" + $env:COMPUTERNAME + "\DBBackup" - DatabaseName = 'Ha-Sample' - InstanceName = 'MSSQLSERVER' - ServerName = $env:COMPUTERNAME - Ensure = 'Present' - ProcessOnlyOnActiveNode = $true - PsDscRunAsCredential = $DomainCreds - DependsOn = "[Script]BackupDB" - } } - LocalConfigurationManager { RebootNodeIfNeeded = $true diff --git a/scripts/windows/dsc/PrepareSQLServer.ps1.zip b/scripts/windows/dsc/PrepareSQLServer.ps1.zip index d171d0d0b..572ed2439 100644 Binary files a/scripts/windows/dsc/PrepareSQLServer.ps1.zip and b/scripts/windows/dsc/PrepareSQLServer.ps1.zip differ diff --git a/virtual-machines/n-tier-windows/n-tier-windows.json b/virtual-machines/n-tier-windows/n-tier-windows.json index 7ed7d803a..a15648e75 100644 --- a/virtual-machines/n-tier-windows/n-tier-windows.json +++ b/virtual-machines/n-tier-windows/n-tier-windows.json @@ -448,6 +448,46 @@ } } ] + }, + { + "vms": [ + "sql-vm1" + ], + "extensions": [ + { + "name": "AOSetup", + "publisher": "Microsoft.Powershell", + "type": "DSC", + "typeHandlerVersion": "2.7", + "autoUpgradeMinorVersion": true, + "settings": { + "modulesURL": "https://raw.githubusercontent.com/mspnp/reference-architectures/master/scripts/windows/dsc/PrepareSQLServer.ps1.zip", + "configurationFunction": "PrepareSqlServer-AGDB.ps1\\SQLServerDBDsc", + "properties": { + "domainName": "contoso.com", + "Admincreds": { + "userName": "adminUser", + "password": "privateSettingsRef:adminPassword" + }, + "sqlServiceCreds": { + "userName": "sqlservicetestuser", + "password": "privateSettingsRef:sqlServerServiceAccountPassword" + }, + "ClusterName": "AOCluster", + "ClusterOwnerNode": "sql1", + "ClusterIP": "10.0.3.30", + "witnessStorageBlobEndPoint": "https://[replace-with-storageaccountname].blob.core.windows.net", + "witnessStorageAccountKey": "[replace-with-storagekey]" + } + }, + "protectedSettings": { + "items": { + "adminPassword": "testPassw0rd!23", + "sqlServerServiceAccountPassword": "AweS0me@SQLServicePW" + } + } + } + ] } ] },