Examples/DSCFCISqlBuild.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#requires -Version 5
$computers = 'OHSQL9034N1','OHSQL9034N2','OHSQL9034N3'
$OutputPath = 'F:\DSCConfig'

Configuration FCISQL
{
    Import-DscResource â€“Module PSDesiredStateConfiguration
    Import-DscResource -Module xSQLServer
    Import-DscResource -Module xFailoverCluster

    Node $AllNodes.NodeName
    {

        # Set LCM to reboot if needed
        LocalConfigurationManager
        {
            DebugMode = "ForceModuleImport"
            RebootNodeIfNeeded = $true
        }

        WindowsFeature "NET-Framework-Core"
        {
            Ensure = "Present"
            Name = "NET-Framework-Core"
            Source = $Node.SourcePath + "\WindowsServer2012R2\sources\sxs"
        }

        WindowsFeature "Failover-Clustering"
        {
            Ensure = "Present"
            Name = "Failover-Clustering"
        }

       WindowsFeature RSATClusteringMgmt
       {
           Ensure = "Present"
           Name = "RSAT-Clustering-Mgmt"
       }

       WindowsFeature RSATClusteringPowerShell
       {
           Ensure = "Present"
           Name   = "RSAT-Clustering-PowerShell"
       }

       WindowsFeature RSATClusteringCmdInterface
       {
           Ensure = "Present"
           Name   = "RSAT-Clustering-CmdInterface"
       }

        xSQLServerFailoverClusterSetup "PrepareMSSQLSERVER"
        {
            DependsOn = @(
                "[WindowsFeature]NET-Framework-Core",
                "[WindowsFeature]Failover-Clustering"
            )
            Action = "Prepare"
            SourcePath = $Node.SourcePath
            SetupCredential = $Node.InstallerServiceAccount
            Features = $Node.Features
            InstanceName = $Node.InstanceName
            FailoverClusterNetworkName = $Node.FailoverClusterNetworkName
            SQLSvcAccount = $Node.InstallerServiceAccount
        }

        xSqlServerFirewall "FirewallMSSQLSERVER"
        {
            DependsOn = "[xSQLServerFailoverClusterSetup]PrepareMSSQLSERVER"
            SourcePath = $Node.SourcePath
            InstanceName = $Node.InstanceName
            Features = $Node.Features
        }

    If ($node.Role -eq "PrimaryServerNode")
    {
            xCluster "CLDBx"
            {
                DependsOn = @(
                    "[WindowsFeature]RSATClusteringMgmt",
                    "[WindowsFeature]RSATClusteringPowerShell"
                )
                Name = $Node.ClusterName
                StaticIPAddress = $Node.ClusterIPAddress
                DomainAdministratorCredential = $Node.InstallerServiceAccount
            }

            xClusterDisk "iSCSI"
            {
                Ensure = "Present"
                Number = 1

                DependsOn = "[xCluster]CLDBx"
            }

        }
        If ($node.Role -eq "ReplicaServerNode" )
        {
            xWaitForCluster waitForCluster
            {
                Name = $Node.ClusterName
                RetryIntervalSec = 10
                RetryCount = 20
            }

            xCluster joinCluster
            {
                Name = $Node.ClusterName
                StaticIPAddress = $Node.ClusterIPAddress
                DomainAdministratorCredential = $Node.InstallerServiceAccount

                DependsOn = "[xWaitForCluster]waitForCluster"
            }
        }
        If ($node.Role -eq "PrimaryServerNode")
        {

            WaitForAll "SqlPrep"
            {
                NodeName = @($computers)
                ResourceName = "[xSQLServerFailoverClusterSetup]PrepareMSSQLSERVER"
                PsDscRunAsCredential = $Node.InstallerServiceAccount
                RetryIntervalSec = 5
                RetryCount = 720
            }
            xSQLServerFailoverClusterSetup "CompleteMSSQLSERVER"
            {
                Action = "Complete"
                SourcePath = $Node.SourcePath
                SetupCredential = $Node.InstallerServiceAccount
                Features = $Node.Features
                InstanceName = $Node.InstanceName
                FailoverClusterNetworkName = $Node.FailoverClusterNetworkName
                InstallSQLDataDir = "D:\"
                FailoverClusterIPAddress = "10.0.75.60"
                SQLSvcAccount = $Node.SQLServiceAccount
                SQLSysAdminAccounts = $Node.AdminAccount

                PsDscRunAsCredential = $Node.InstallerServiceAccount

                DependsOn = @(
                    "[WaitForAll]SqlPrep",
                    "[xClusterDisk]iSCSI"
                )
            }
        }
    }
}

$InstallerServiceAccount = Get-Credential -UserName CORP\AutoSvc -Message "Credentials to Install SQL Server"

$firstComputer = $computers | Select-Object -First 1
$ConfigurationData = @{
    AllNodes = @(
        @{
            NodeName = "*"
            PSDscAllowPlainTextPassword = $true
            PSDscAllowDomainUser =$true
            NETPath = "\\ohdc9000\SQLAutoBuilds\SQL2014\WindowsServer2012R2\sources\sxs"
            SourcePath = "\\ohdc9000\SQLAutoBuilds\SQL2014\"
            InstallerServiceAccount = $InstallerServiceAccount
            SQLServiceAccount = $InstallerServiceAccount
            AdminAccount = "CORP\Administrator"
            ClusterName = "CLDBx6"
            ClusterIPAddress = "10.0.75.55"
            FailoverClusterNetworkName = "CLSCDB6"
        }
        )}

        ForEach ($computer in $computers) {

            if($firstComputer -eq $computer)
            {
                    $ConfigurationData.AllNodes += @{
                    NodeName        = $computer
                    InstanceName    = "MSSQLSERVER"
                    Features        = "SQLENGINE"
                    Role = "PrimaryServerNode"
                    }
            }
            else
            {
                    $ConfigurationData.AllNodes += @{
                    NodeName        = $computer
                    InstanceName    = "MSSQLSERVER"
                    Features        = "SQLENGINE"
                    Role = "ReplicaServerNode"
                    }
            }
           $Destination = "\\"+$computer+"\\c$\Program Files\WindowsPowerShell\Modules"
           if (Test-Path "$Destination\xFailoverCluster"){Remove-Item -Path "$Destination\xFailoverCluster" -Recurse -Force}
           if (Test-Path "$Destination\xSqlServer"){Remove-Item -Path "$Destination\xSqlServer"-Recurse -Force}
           Copy-Item 'C:\Program Files\WindowsPowerShell\Modules\xFailoverCluster' -Destination $Destination -Recurse -Force
           Copy-Item 'C:\Program Files\WindowsPowerShell\Modules\xSqlServer' -Destination $Destination -Recurse -Force
 }



FCISQL -ConfigurationData $ConfigurationData -OutputPath $OutputPath


Workflow StartConfigs
{
    param([string[]]$computers,
        [System.string] $Path)

    foreach â€“parallel ($Computer in $Computers)
    {

        Start-DscConfiguration -ComputerName $Computer -Path $Path -Verbose -Wait -Force
    }
}

StartConfigs -Computers $computers -Path $OutputPath