-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApplyDBOps.ps1
168 lines (150 loc) · 11.5 KB
/
ApplyDBOps.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
Param(
[parameter(Mandatory=$true, Position=0)]
[String]
$TargetServer
,
[parameter(Mandatory=$true, Position=1)]
[Boolean]
$PartialCodePush
,
[parameter(Mandatory=$true, Position=2)]
[Boolean]
$ExcludeCodeExecution ) #If $PartialCodePush is True, $ExcludeCodeExecution code section will always be skipped.
#$TargetServer="Computer447"
#$PartialCodePush = $true
#$ExcludeCodeExecution = $true
if ( Get-PSSnapin -Registered | where {$_.name -eq 'SqlServerProviderSnapin100'} )
{
if( !(Get-PSSnapin | where {$_.name -eq 'SqlServerProviderSnapin100'}))
{
Add-PSSnapin SqlServerProviderSnapin100 | Out-Null
} ;
if( !(Get-PSSnapin | where {$_.name -eq 'SqlServerCmdletSnapin100'}))
{
Add-PSSnapin SqlServerCmdletSnapin100 | Out-Null
}
}
else
{
if( !(Get-Module | where {$_.name -eq 'sqlps'}))
{
Import-Module 'sqlps' –DisableNameChecking
}
}
$error.clear()
$BasePath=""
$UserName = $null
$password = $null
IF(!($PartialCodePush)-or $PartialCodePush -like "$false")
{ $BasePath="C:\PowerShell\ITOps\DBOPS\code"}
IF($PartialCodePush -or $PartialCodePush -like "$true")
{ $BasePath="C:\PowerShell\ITOps\DBOPS\PartialCodePush"}
if ((Test-Path -path $BasePath) -ne $True)
{ Write-Output "$BaseInstallPath not exists. Cancelling..."
exit}
$dbopsExists = (Invoke-Sqlcmd -ServerInstance $TargetServer -Database "Master" -Query "select name from master.dbo.sysdatabases where name like 'DBOPS'" -ErrorAction SilentlyContinue)
if($error)
{ Write-Output "***** Error establishing connection."
#Write-host "***** Error establishing connection."
Write-Output "Using hardcoded values in script for AlternateCredentials."
#Write-host "Using hardcoded values in script for AlternateCredentials."
$error.clear()
$UserName = "jetcityjet2"
Write-Output "Using username: " $UserName
Write-Host "Using username: " $UserName
$dbopsExists = (Invoke-Sqlcmd -ServerInstance $TargetServer -Database "Master" -Query "select name from master.dbo.sysdatabases where name like 'DBOPS'" -user $UserName )##-password $password)
if($error -ne $null)
{
Write-Output "***** Could not connect to the server. Check security rights or network."
Write-Host "***** Could not connect to the server. Check security rights or network."
exit
}
}
if ($dbopsExists -eq $null)
{ Write-Output "***** Failed to find DBOps database on server $TargetServer. Creating database"
IF($userName -ne $null)
{Invoke-Sqlcmd -ServerInstance $TargetServer -Database "master" -Inputfile "C:\PowerShell\ITOps\DBOPS\code\DBOps\CreateDBOPSDatabase.sql" -user $UserName }##-password $password}
else {Invoke-Sqlcmd -ServerInstance $TargetServer -Database "master" -Inputfile "C:\PowerShell\ITOps\DBOPS\code\DBOps\CreateDBOPSDatabase.sql"}
}
Write-Output "Running object scripts"
Write-Host "Running object scripts"
# then install all the scripts
$PushOrderList = Get-Content C:\PowerShell\ITOps\DBOPS\ObjectPushOrderList.txt
foreach ($PushObjectType in $PushOrderList) ## This loop is to maintain the push order
{
Write-Output $PushObjectType
#foreach ($folder in $folders)
#{
$InstallPath = $BasePath+"\DBOps" + "\" + $PushObjectType
#$i = 0
IF(Test-Path $InstallPath)
{
Write-Output $InstallPath
$var = get-childitem $InstallPath | where {$_.FullName -like '*.sql'} # | Select FullName
#Write-Output $PushObjectType
#Write-Output $var.Fullname
foreach($InputFile in $var)
{
#$i= $i +1
#Write-Output $i
$InstallFile=$InstallPath+"\"+$inputfile
Write-Output $InstallFile
Write-Host $InstallFile
IF($userName -ne $null)
{Invoke-Sqlcmd -ServerInstance $TargetServer -Database "DBOPS" -Inputfile $InstallFile -user $UserName }##-password $password}
else {Invoke-Sqlcmd -ServerInstance $TargetServer -Database "DBOPS" -Inputfile $InstallFile}
if($error)
{
#Write-Output $error
$Error.Clear()
}
}
}else{ Write-Output "Nothing to push"
Write-Host "Nothing to push"}
#}
}
# Now Master Database
Write-Host "MASTER database objects."
Write-Output "MASTER database objects."
foreach ($PushObjectType in $PushOrderList) ## This loop is to maintain the push order
{
Write-Output $PushObjectType
#foreach ($folder in $folders)
#{
$InstallPath = $BasePath+"\Master" + "\" + $PushObjectType
#$i = 0
IF(Test-Path $InstallPath)
{
Write-Output $InstallPath
$var = get-childitem $InstallPath | where {$_.FullName -like '*.sql'} # | Select FullName
#Write-Output $PushObjectType
#Write-Output $var.Fullname
foreach($InputFile in $var)
{
#$i= $i +1
#Write-Output $i
$InstallFile=$InstallPath+"\"+$inputfile
Write-Output $InstallFile
#Write-Host $InstallFile
IF($userName -ne $null)
{Invoke-Sqlcmd -ServerInstance $TargetServer -Database "Master" -Inputfile $InstallFile -user $UserName }##-password $password}
else {Invoke-Sqlcmd -ServerInstance $TargetServer -Database "Master" -Inputfile $InstallFile}
if($error)
{
#Write-Output $error
$Error.Clear()
}
}
}else{ Write-Output "Nothing to push"}
#}
}
#Execute specific procs to configure a new server.
IF(($ExcludeCodeExecution -eq $true) -and ($PartialCodePush -eq $false))
{
Write-Output "Running prc_dba_configuremail"
Write-Host "Running prc_dba_configuremail"
IF($userName -ne $null)
{Invoke-Sqlcmd -ServerInstance $TargetServer -Database "DBOps" -Query "exec prc_dba_configuremail" -user $UserName } ##-password $password}
else {Invoke-Sqlcmd -ServerInstance $TargetServer -Database "DBOps" -Query "exec prc_dba_configuremail"}
}
$error.clear()