cEPRSDBFeatures.psm1

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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
enum Ensure
{
    Present
    Absent
}

[DscResource()]
class cEPRSDBSnapShot
{
    [DscProperty(Key)]
    [String]$ServerName

    [DscProperty(Mandatory=$true)]
    [String]$DatabaseName

    [DscProperty(Mandatory=$true)]
    [String]$SnapshotName

    [DscProperty(Mandatory=$true)]
    [String]$Logfile

    [cEPRSDBSnapShot]Get()
    {
        Write-Verbose "Getting database connection..."
        $qdbname = SQLCMD -S $this.ServerName -d $this.DatabaseName -Q "set nocount on; select db_name();"
        $dbname = $this.DatabaseName
        $snapshot = $this.SnapshotName
        $logfilepath = $this.Logfile
    
        if($qdbname -Contains $this.DatabaseName)
        {
            $dbname = ""
        }
    
        Write-Verbose "Getting detected values..."
        $returnValue = @{
            ServerName = $this.ServerName
            DatabaseName = $dbname
            SnapshotName = $this.SnapshotName
            Logfile = $logfilepath
        }
        return $this
    }
    [bool]Test()
    {
        #Write-Verbose "Use this cmdlet to deliver information about command processing."

        #Write-Debug "Use this cmdlet to write debug information while troubleshooting."

        [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMo") | Out-Null
        [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMoExtended") | Out-Null
        [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMoEnum") | Out-Null
        [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null

        Write-Verbose "Connecting to $($this.ServerName) server- START" | Out-File $this.LogFile -Append
        $srvr = new-object ("Microsoft.SqlServer.Management.Smo.Server") "$($this.ServerName)"
        Write-Verbose "Connecting to $($this.ServerName) server- END" | Out-File $this.LogFile -Append

        Write-Verbose "Checking for existence of DB $($this.DatabaseName) in $($this.ServerName) server- START" | Out-File $($this.Logfile) -Append
        #$CheckDB=$srvr.Databases | where {$_.name -eq "$DatabaseName"}
        $database = $srvr.Databases[$this.DatabaseName]  
        Write-Verbose "Checking for existence of DB $($this.DatabaseName) in $($this.ServerName) server- END" | Out-File $($this.Logfile) -Append
        $DBName=$database.Name
        Write-Verbose "DB name is $DBName" | Out-File $this.Logfile -Append

        if($database.Name)
        {
            $result = $false
        }   
        else
        {
            Write-Verbose "Database $($this.DatabaseName) doesnot exist" 
            $result = $true
        }
        Write-Verbose "$result"
        return $result
    }
    Set()
    {
        Write-Verbose "Executing the SQL file on Server: $($this.ServerName) in Database: $($this.DatabaseName)."        
        New-Item -Path $this.Logfile -ItemType File -Force -Verbose
        Write-Verbose "After File create"
        CreateDB_Snapshot -ServerName $this.ServerName -DatabaseName $this.DatabaseName -DBSnapshotName $this.SnapshotName -LogFile $this.Logfile
    }
    
}

Function CreateDB_Snapshot
{
param($ServerName, $DatabaseName, $DBSnapshotName, $LogFile)
     Try
     {
          [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMo") | Out-Null
          [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMoExtended") | Out-Null
          [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMoEnum") | Out-Null
          [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null 

          Write-Verbose "Connecting to $ServerName server- START" | Out-File $LogFile -Append
          $srvr = new-object ("Microsoft.SqlServer.Management.Smo.Server") "$ServerName"
          Write-Verbose "Connecting to $ServerName server- END" | Out-File $LogFile -Append

          Write-Verbose "Checking for existence of DB $DatabaseName in $ServerName server- START" | Out-File $LogFile -Append
          $database=$srvr.Databases | where {$_.name -eq "$DatabaseName"}
          #$database = $srvr.Databases[$DatabaseName]
          Write-Verbose "Checking for existence of DB $DatabaseName in $ServerName server- END" | Out-File $LogFile -Append
          $DBName=$database.Name
          Write-Verbose "DB name is $DBName" | Out-File $LogFile -Append

          
             Write-Verbose "------------------------------------------------------------------" | Out-File $LogFile -Append
             Write-Verbose  "Creating '$DBSnapshotName' snapshot on '$DatabaseName' database ." | Out-File $LogFile -Append
             
                $snapshot = New-Object ("Microsoft.SqlServer.Management.Smo.Database") ($ServerName,$DBSnapshotName)
                $snapshot.DatabaseSnapshotBaseName=$database.Name            

                #Add all filegroups from the base database to snapshot
                foreach ($filegroup in $database.FileGroups) {
                 $newfg = New-Object ("Microsoft.SqlServer.Management.Smo.FileGroup") ($snapshot,$filegroup.Name)
                 Write-Verbose  "newfg = $newfg"
                 $snapshot.FileGroups.Add($newfg)
                }            

                #Add all datafiles from each filegroup of the base database to snapshot with a new datafile name
                #.ss as the datafile extension for the snapshot file
                #By default, the snapshot files will reside in the same folder as base DB data files
                foreach ($filegroup in $database.FileGroups) {
                 foreach ($datafile in $filegroup.Files) {
                  $newDataFile = New-Object ("Microsoft.SqlServer.Management.Smo.DataFile") ($snapshot.FileGroups[$filegroup.Name],$datafile.Name,"$($database.PrimaryFilePath)\$($datafile.Name).ss")
                  $newDataFile.FileName
                  
                  $newDataFilename=$newDataFile.FileName
                   Write-Verbose  "newDataFile = $newDataFilename"
                  $snapshot.FileGroups[$filegroup.Name].Files.Add($newDataFile)
                 }
                }            

                #Create the snapshot
                Write-Verbose  "Creating '$DBSnapshotName' snapshot-START." | Out-File $LogFile -Append
                $snapshot.Create("$DBSnapshotName") | Out-File $LogFile -Append
                Write-Verbose  "Creating '$DBSnapshotName' snapshot-END." | Out-File $LogFile -Append

             Write-Verbose "------------------------------------------------------------------" | Out-File $LogFile -Append
             Write-Verbose "Creating DB Snapshot finished." | Out-File $LogFile -Append
             
          
     }
     Catch
     {
         [System.Exception]
         Write-Error $_.Exception.Message | Out-File $LogFile -Append
     }
     Finally
     {
         Write-Verbose "Execution completed!" | Out-File $LogFile -Append
     }
}

[DscResource()]
class cEPRSRestoreSnapshot
{
    [DscProperty(Key)]
    [String]$ServerName

    [DscProperty(Mandatory=$true)]
    [String]$DatabaseName

    [DscProperty(Mandatory=$true)]
    [String]$SnapShotName

    [DscProperty(Mandatory=$true)]
    [String]$LogFile

    [cEPRSRestoreSnapshot]Get()
    {
        Write-Verbose "Getting database connection..."
        $qdbname = SQLCMD -S $this.ServerName -d $this.DatabaseName -Q "set nocount on; select db_name();"
        $dbname = $this.DatabaseName
        $snapshot = $this.SnapShotName
        $logfilepath = $this.LogFile
    
        if($qdbname -Contains $this.DatabaseName)
        {
            $dbname = ""
        }
        $this.DatabaseName = $dbname
        return $this
    }
    [bool]Test()
    {
        $result=$false
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
        $srvr = new-object ("Microsoft.SqlServer.Management.Smo.Server") "$($this.ServerName)"
        $CheckDB=$srvr.Databases | where {$_.name -eq "$($this.DatabaseName)"}

        if($CheckDB.Name -ne "$($this.DatabaseName)")
        {
        Write-Error "$($this.DatabaseName) Database not found in server $($this.ServerName)!"
        $result = $true
        }
    
        return $result

    }
    Set()
    {
        Write-Verbose "Executing the SQL file on Server: $($this.ServerName) in Database: $($this.DatabaseName)."        
        New-Item -Path $this.LogFile -itemtype "file" -Force
    
        RestoreDB_Snapshot -ServerName $this.ServerName -DatabaseName $this.DatabaseName -DBSnapshotName $this.SnapShotName -LogFile $this.SnapShotName
    }
}

Function RestoreDB_Snapshot
{
    param($ServerName, $DatabaseName, $DBSnapshotName, $LogFile)
     Try
     {
          [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
          $srvr = new-object ("Microsoft.SqlServer.Management.Smo.Server") "$ServerName"
          $CheckDB=$srvr.Databases | where {$_.name -eq "$DatabaseName"}
          if($CheckDB.Name)
          {
             Write-Verbose "Restoring '$DatabaseName' database by '$DBSnapshotName' snapshot." 
             
             $db = $srvr.Databases["$DatabaseName"]
             $SQLScript= "RESTORE DATABASE $DatabaseName FROM DATABASE_SNAPSHOT = '$DBSnapshotName'"
             $db.ExecuteNonQuery($SQLScript) | Out-File $LogFile -Append

             Write-Verbose "Restoration finished." 
             
          }
          else
          {
               Write-Verbose "Database '$DBSnapshotName' does not exists!" | Out-File $LogFile -Append
          }
     }
     Catch
     {
         [System.Exception]
         Write-Error  $_.Exception.Message 
     }
     Finally
     {
         Write-Verbose "Execution completed!" 
     }
}

[DSCResource()]

class cEPRSKillDBSessions
{

    [DscProperty(Key)] [String] $ServerName
    [DscProperty(Key)] [String] $DatabaseName
        

    [cEPRSKillDBSessions] Get()
    {
       try
       {
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
        $SQlSvr1 = New-Object "Microsoft.SqlServer.Management.Smo.Server" "$($this.ServerName)"
        $DesiredRule=$SQlSvr1.Databases | where {$_.name -eq "$($this.DatabaseName)"}
        $Presence = if($DesiredRule){"Present"}else{"Absent"}
          

        $output = @{
                Ensure = $Presence;
                ServerName = $this.ServerName;
                DatabaseName = $this.DatabaseName;
                }

        return $output
        }
        Finally
        {
           $this.DesiredRule.Dispose() 
        }
    }
    
    [bool] Test()
    {
       $Result = $false
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
        $SQlSvr1 = New-Object "Microsoft.SqlServer.Management.Smo.Server" "$($this.ServerName)"
        Write-Verbose "$($this.ServerName); $($this.DatabaseName)" -Verbose
        $DesiredRule=$SQlSvr1.Databases | where {$_.name -eq "$($this.DatabaseName)"}
        $Presence = if($DesiredRule){"Present"}else{"Absent"}
        
        if($Presence -eq $this.Ensure)
        {
            $Result = $false
        }
        else
        {
            $Result = $true
        }
        return $Result
    }
    
    
    [void] Set()
    {
        try
        {
            # Killing the existing DB sessions
            [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
            $SQlSvr1 = New-Object "Microsoft.SqlServer.Management.Smo.Server" "$($this.ServerName)"
            $SQlSvr1.KillAllprocesses("$($this.DatabaseName)")
        }
        Catch [system.exception]
        {        
            Write-Verbose $_.exception.message    -Verbose
        }
        
        }
                                                                    
     
}

[DSCResource()]
class cEPRSRestoreDBFromBackup
{
    [DscProperty(Key)]
    [String]
    $SQLServer

    [DscProperty(Mandatory = $true)]
    [String]
    $SQLDatabase

    [DscProperty(Mandatory = $true)]
    [String]
    $BakfilePath
        
    [DscProperty(Mandatory = $true)]
    [bool]
    $TrustedConnection

    [DscProperty(Mandatory = $false)]
    [String]
    $SQLusername
        
    [DscProperty(Mandatory = $false)]
    [String]
    $SQLpassword


    

    [cEPRSRestoreDBFromBackup] Get()
    {
        Write-Verbose "Getting database connection..."
        $qdbname = SQLCMD -S $this.SQLServer -d $this.SQLDatabase -Q "set nocount on; select db_name();"
        $dbname = $this.SQLDatabase

        $backfile = $this.BakfilePath
    
        if($qdbname -Contains $this.SQLDatabase)
        {
            $dbname = ""
        }
    

        $returnValue = @{
            ServerName = $this.SQLServer
            DatabaseName = $dbname
            BackupFile = $backfile

        }
        return $this
    }

    [bool] Test()
    {
        $result=$false
        [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
        $srvr = new-object ("Microsoft.SqlServer.Management.Smo.Server") "$($this.SQLServer)"
        $CheckDB=$srvr.Databases | where {$_.name -eq "$($this.SQLDatabase)"}
        
        $BakExists = Test-Path "$($this.BakfilePath)" -ErrorAction SilentlyContinue

        if($BakExists -eq $true)
        {
            $result = $false
        }
        else
        {
            $result = $true
        }
        return $result
    }

    Set()
    {
        Write-Verbose "Restoring Database $($this.SQLDatabase) on $($this.SQLServer) from $($this.BakfilePath)"
        Restore-SQLdatabase -SQLServer $this.SQLServer -SQLDatabase $this.SQLDatabase -BakfilePath $this.BakfilePath -TrustedConnection $this.TrustedConnection -SQLusername "$($this.SQLusername)" -SQLpassword $this.SQLpassword
    }
}
Function Restore-SQLdatabase
{
param(
    $SQLServer,
    $SQLDatabase,
    $BakfilePath,
    $TrustedConnection,
    $SQLusername,
    $SQLPassword
)
    Try
    {
        if ($TrustedConnection -eq $false)
        {
            if($SQLUsername -eq "")
            {
                write-warning "The SQL Username variable must be defined, if 'TrustedConnection' is false. Use '-SQLusername' to define username."
                break
            }
            if($SQLPassword -eq "")
            {
                write-warning "The SQL Password variable must be defined, if 'TrustedConnection' is false. Use '-SQLPassword' to define username."
                break
            }
        }

     

        $SQLConn = New-Object System.Data.SQLClient.SQLConnection

        #checks for a trusted SQL connection
        if($TrustedConnection -eq $false)
        {
            $UserNameSplit = $SQLusername.split("\")
            $UserName = $UserNameSplit[1]
            $Domain = $UserNameSplit[0]
            $Testcredentials=ValidateUserCredentials -Domain $Domain -Username $UserName -Password $SQLPassword
            if($Testcredentials.IsValid -eq "False")
            {   
                Write-Error "User credentials are not correct. Please check the values provided."
                exit -1
            }
            #Using an SQL account for login
            $SQLConn.ConnectionString = "Data Source=$SQLServer;User ID=$SQLusername;password=$SQLpassword;Initial Catalog=master;Integrated Security=True;Trusted_Connection=True;"
        }
        Else
        {
            #Using a trusted connection
            $SQLConn.ConnectionString = "Server=$SQLServer; Trusted_Connection=True"
        }
        Write-verbose "Attempting to connect to the Specified SQL server:" 
        try{
            $SQLConn.Open()
            Write-verbose "Success" 
            }
        catch{
                Write-warning "An exception was caught while attempting to open the SQL connection, please confirm the login details are correct and try again."
                Break
            }
      
        $SQLCmd = New-Object System.Data.SQLClient.SQLCommand
        $SQLcmd = $SQLconn.CreateCommand()
        $sqlcmd.commandtimeout=0
        $SQLcmd.CommandText="ALTER DATABASE $SQLDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE RESTORE DATABASE $SQLDatabase FROM DISK = '$BakfilePath' WITH REPLACE"
        $starttime = Get-date
        try{
                $SQLcmd.Executenonquery() | out-null
                $result="Success"
                Write-Verbose "Database restoration finished!" 
            }
        catch{
                write-warning "An Exception was caught while restoring the database!"
                write-warning "$_"
                write-warning "attempting to recover the database"
                $SQLcmd.CommandText="ALTER DATABASE $SQLDatabase SET MULTI_USER"
                $SQLcmd.Executenonquery() | out-null
                $result="Failed"
            }
        finally{
                $SQLconn.close()
                $timetaken=[math]::round(((get-date) - $starttime).totalseconds,0)
                $report=new-object PSObject -Property @{
                SQLServer=$SQLserver;
                Database=$sqlDatabase;
                Result=$result;
                Timetaken="$timetaken Seconds";}
                }
        Return $Report
    }
    Catch
    {
        [System.Exception]
        Write-Verbose $_.Exception.Message 
    }
    Finally
    {
        Write-Verbose "Execution completed!" 
       
    }
}

Function ValidateUserCredentials
{
param($Domain, $Username, $Password)
    Try
    {
         Add-Type -AssemblyName System.DirectoryServices.AccountManagement
         $ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
         $pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($ct, $Domain)
         New-Object PSObject -Property @{
         UserName = $Username;
         IsValid = $pc.ValidateCredentials($Username, $Password,"Negotiate").ToString()
         }
        
    }
    Catch
    {
         [System.Exception]
         Write-Host $_.Exception.Message
    }
    Finally
    {
         "User credentials validation finished."
         [System.Console]::WriteLine("User credentials validation finished.")
    }
    
}

[DSCResource()]
class cEPRSExecuteSql
{

[DscProperty(Key)] [String] $ServerName
[DscProperty(Key)] [String] $DatabaseName
[DscProperty(Key)] [String] $SQLFile
[DscProperty(Key)] [String] $Logfile
[DscProperty()] [String] $Variables

[cEPRSExecuteSql] Get()
{
    Write-Verbose "Getting database connection..." -Verbose
    $qdbname= SQLCMD -S $this.ServerName -d $this.DatabaseName -Q "set nocount on; select db_name();"
    $dbname=$this.DatabaseName 
    $sqlfilepath=$this.SQLFile
    $logfilepath=$this.Logfile
            
     if($qdbname -Contains $this.DatabaseName)
        {
            $dbname = ""
        }
    Write-Verbose "Getting SQL file path..." -Verbose

        if(-not(Test-Path -Path "$($this.SQLFile)" -Include *.sql))
        {
            $sqlfilepath = ""
        }
    Write-Verbose "Getting detected values..." -Verbose

        $returnValue = @{
            ServerName = $this.ServerName
            DatabaseName = $dbname
            SQLFile = $sqlfilepath
            Logfile = $logfilepath
        }

        return $returnValue
    }

    set()
    {
 
        Write-Verbose "Executing the SQL file on Server: $($this.ServerName) in Database: $($this.DatabaseName)."
        New-Item -Path $this.Logfile -itemtype "file" -Force
        $executable = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'

        if($this.Variables -eq '' -or $this.Variables -eq $null)
        {
            Write-Verbose "if part"

            $arglist = "SQLCMD.EXE -S $($this.ServerName) -d $($this.DatabaseName) -E -b -i `"$($this.SQLFile)`" | Out-File -FilePath `"$($this.Logfile)`""
            Write-Verbose "arglist: $arglist"
            $proc = Start-Process $executable -ArgumentList "$arglist" -Wait -NoNewWindow -PassThru
                if($proc.HasExited)
                {
                    Write-Verbose "SQLCMD.EXE execution process exited."
                } 
        }
        else
        {
            Write-Verbose "else part"
            Write-Verbose "Variables: this.Variables."
            $arglistVar = "SQLCMD.EXE -S $($this.ServerName) -d $($this.DatabaseName) -E -b -i `"$($this.SQLFile)`" -v $($this.Variables) | Out-File -FilePath `"$($this.Logfile)`""
            Write-Verbose "arglistVar: $arglistVar"
            $proc = Start-Process $executable -ArgumentList "$arglistVar" -Wait -NoNewWindow -PassThru
            if($proc.HasExited)
            {
                Write-Verbose "SQLCMD.EXE execution process exited."
            }
        }

            Write-Verbose "Execution completed. Examine the log file, $($this.Logfile)"
      
    }


    [bool] Test()
    {
        $result = $false
        Write-Verbose "Verifying execution..." -Verbose
        if(Test-Path -Path "$($this.Logfile)")
        {
            $wordsToFind  = {"error","fail","aborted","rolled back","stopped"}
            $log = Get-Content $this.Logfile
            foreach($word in $wordsToFind)
            {
                $containsWord = $log | %{$_ -like $word}
            }

            If($this.containsWord -contains $true)
            {
                Write-Verbose "SQL file failed to execute successfully. Examine the log file, $($this.Logfile)"
                return $false
            }        
        }
    
        Write-Verbose "Verification completed. ...." -Verbose
        Write-Verbose "$result" -Verbose
        return $result
    }
}