-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPreDeploymentChecks.ps1
73 lines (61 loc) · 2.8 KB
/
PreDeploymentChecks.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
param(
[Parameter(Mandatory)][ValidateNotNullOrEmpty()]$SQLInstance,
[Parameter(Mandatory)][ValidateNotNullOrEmpty()]$Database,
[Parameter(Mandatory)][ValidateNotNullOrEmpty()]$Environment,
[Parameter(Mandatory)][ValidateNotNullOrEmpty()]$SourceDir
)
$errorCount = 0
$errorTypes = @()
Write-Output ""
Write-Output "***** PERFORMING PRE-DEPLOYMENT CHECKS AGAINST $SQLInstance.$Database *****"
Write-Output ""
Write-Output "Reading data from source files."
$usersFile = Join-Path -Path $SourceDir -ChildPath "users.json"
$sourceUsers = Get-Content $usersFile | ConvertFrom-Json
$roleMembersFile = Join-Path -Path $SourceDir -ChildPath "rolemembers_$Environment.json"
$sourceRoleMembers = Get-Content $roleMembersFile | ConvertFrom-Json
# Checking that logins exist for all users
Write-Output " "
Write-Output "*** CHECK: Do all the required LOGINS exist on $SQLInstance ***"
Write-Output "Reading LOGINS from $SQLInstance."
$dbLogins = Get-DbaLogin -SqlInstance $SQLInstance
[array]$requiredUsers = $sourceUsers | Where-Object -Property Environment -like -value $Environment
[array]$corruptUsers = $requiredUsers | Where-Object -Property Login -notin $dbLogins.Name
if ($corruptUsers.length -gt 0){
$msg = "Found " + $corruptUsers.length + " currupt user(s) on $SQLInstance. Please add the following LOGINS on the server: "
foreach ($corruptUser in $corruptUsers){
$msg = $msg + $corruptUser.Login + ", "
}
Write-Error $msg
$errorCount += 1
$errorTypes += " Missing " + $corruptUsers.length + " LOGINS on $SQLInstance."
}
else {
Write-Output "All required LOGINS found on $SQLInstance."
}
Write-Output " "
Write-Output "*** CHECK: Do all the required DEFAULT SCHEMAS exist on $SQLInstance.$Database ***"
Write-Warning "To do: Write this test. Not yet implemented."
Write-Output " "
Write-Output "*** CHECK: Do all the required ROLES exist on $SQLInstance.$Database ***"
Write-Output "Reading ROLES from $SQLInstance.$Database."
$dbRoles = Get-DbaDbRole -SqlInstance $SQLInstance -Database $Database
[array]$missingRoles = $sourceRoleMembers | Where-Object -Property Role -notin $dbRoles.Name
if ($missingRoles.length -gt 0){
$msg = "Found " + $missingRoles.length + " missing ROLE(S) on $SQLInstance.$Database. Please add the following ROLES to the database: "
foreach ($role in $missingRoles.Role){
$msg = $msg + $role + ", "
}
Write-Error $msg
$errorCount += 1
$errorTypes += " Missing " + $missingRoles.length + " ROLES on $SQLInstance.$Database."
}
else {
Write-Output "All required ROLES found on $SQLInstance."
}
# Throwing error if $errorCount > 0 to ensure DeplpoySecurity.ps1 stops before deployment
if($errorCount -gt 0){
$errorMsg = "Failed pre-deployment checks with $errorCount error(s)!:"
$errorMsg = $errorMsg + $errorTypes
throw $errorMsg
}