Skip to content

Commit f050637

Browse files
author
Shawn Melton
authored
Merge pull request #3 from ctrlbold/development
Refresh development fork
2 parents 5b04b68 + 9289134 commit f050637

16 files changed

+1422
-581
lines changed

dbatools.psd1

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
RootModule = 'dbatools.psm1'
1212

1313
# Version number of this module.
14-
ModuleVersion = '0.8.5.2'
14+
ModuleVersion = '0.8.5.52'
1515

1616
# ID used to uniquely identify this module
1717
GUID = '9d139310-ce45-41ce-8e8b-d76335aa1789'
@@ -133,7 +133,10 @@
133133
'Get-DbaDatabaseFreespace',
134134
'Get-DbaClusterActiveNode',
135135
'Test-DbaDatabaseOwner',
136-
'Set-DbaDatabaseOwner'
136+
'Set-DbaDatabaseOwner',
137+
'Test-DbaJobOwner',
138+
'Set-DbaJobOwner',
139+
'Test-DbaVirtualLogFile'
137140
)
138141

139142
# Cmdlets to export from this module

functions/Copy-SqlAlert.ps1

+63-9
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,12 @@ Shows what would happen if the command were executed using force.
132132
{
133133
try
134134
{
135-
Write-Verbose "Dropping Alert $alertname"
136-
$destserver.JobServer.Alerts[$alertname].Drop()
135+
Write-Verbose "Dropping Alert $alertname on $destserver"
136+
#$destserver.JobServer.Alerts[$alertname].Drop()
137+
138+
$sql = "EXEC msdb.dbo.sp_delete_alert @name = N'$($alert.name)';"
139+
Write-Verbose $sql
140+
$destserver.ConnectionContext.ExecuteNonQuery($sql) | Out-Null
137141
}
138142
catch
139143
{
@@ -142,7 +146,7 @@ Shows what would happen if the command were executed using force.
142146
}
143147
}
144148
}
145-
149+
# job is created here.
146150
If ($Pscmdlet.ShouldProcess($destination, "Creating Alert $alertname"))
147151
{
148152
try
@@ -163,12 +167,6 @@ Shows what would happen if the command were executed using force.
163167
$destserver.JobServer.Alerts.Refresh()
164168
$destserver.JobServer.Jobs.Refresh()
165169

166-
$newalert = $destserver.JobServer.Alerts[$alertname]
167-
$notifications = $alert.EnumNotifications()
168-
$newnotifications = $newalert.EnumNotifications()
169-
$job = $alert.JobId
170-
$jobname = $alert.JobName
171-
172170
# Super workaround but it works
173171
if ($alert.JobId -ne '00000000-0000-0000-0000-000000000000')
174172
{
@@ -191,6 +189,62 @@ Shows what would happen if the command were executed using force.
191189
}
192190
}
193191
}
192+
193+
194+
$newalert = $destserver.JobServer.Alerts[$alertname]
195+
$notifications = $alert.EnumNotifications()
196+
$newnotifications = $newalert.EnumNotifications()
197+
$job = $alert.JobId
198+
$jobname = $alert.JobName
199+
200+
If ($Pscmdlet.ShouldProcess($destination, "Moving Notifications $alertname"))
201+
{
202+
try
203+
{
204+
foreach ($notify in $notifications)
205+
# cant add them this way, we need to modify the existing one or give all options that are supported.
206+
{
207+
$nm = @()
208+
if ($notify.UseNetSend -eq $true)
209+
{
210+
write-verbose "Adding net send"
211+
$nm += "NetSend"
212+
}
213+
214+
if ($notify.UseEmail -eq $true)
215+
{
216+
write-verbose "Adding email"
217+
$nm += "NotifyEmail"
218+
}
219+
220+
if ($notify.UsePager -eq $true)
221+
{
222+
write-verbose "Adding pager"
223+
$nm += "Pager"
224+
}
225+
$nml = $nm -join ", "
226+
227+
$newalert.AddNotification($notify.OperatorName, [Microsoft.SqlServer.Management.Smo.Agent.NotifyMethods]$nml) # concat the notify methods together
228+
}
229+
}
230+
catch
231+
{
232+
$e = $_.Exception
233+
$line = $_.InvocationInfo.ScriptLineNumber
234+
$msg = $e.Message
235+
236+
if ($e -like '*The specified @operator_name (''*'') does not exist*')
237+
{
238+
Write-Warning "One or more operators for this alert are not configured and will not be added to this alert."
239+
Write-Warning "Please run Copy-SqlOperator if you would like to move operators to destination server."
240+
}
241+
else
242+
{
243+
Write-Error "caught exception: $e at $line : $msg"
244+
}
245+
}
246+
}
247+
194248
}
195249
}
196250

functions/Copy-SqlJob.ps1

+13-10
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Shows what would happen if the command were executed using force.
112112
if ($missingdb.count -gt 0 -and $dbnames.count -gt 0)
113113
{
114114
$missingdb = ($missingdb | Sort-Object | Get-Unique) -join ", "
115-
Write-Warning "Database(s) $missingdb doesn't exist on destination. Skipping."
115+
Write-Warning "[Job: $jobname] Database(s) $missingdb doesn't exist on destination. Skipping."
116116
continue
117117
}
118118

@@ -121,7 +121,7 @@ Shows what would happen if the command were executed using force.
121121
if ($missinglogin.count -gt 0)
122122
{
123123
$missinglogin = ($missinglogin | Sort-Object | Get-Unique) -join ", "
124-
Write-Warning "Login(s) $missinglogin doesn't exist on destination. Skipping."
124+
Write-Warning "[Job: $jobname] Login(s) $missinglogin doesn't exist on destination. Skipping."
125125
continue
126126
}
127127

@@ -131,15 +131,15 @@ Shows what would happen if the command were executed using force.
131131
if ($missingproxy.count -gt 0 -and $proxynames.count -gt 0)
132132
{
133133
$missingproxy = ($missingproxy | Sort-Object | Get-Unique) -join ", "
134-
Write-Warning "Proxy Account(s) $($proxynames[0]) doesn't exist on destination. Skipping."
134+
Write-Warning "[Job: $jobname] Proxy Account(s) $($proxynames[0]) doesn't exist on destination. Skipping."
135135
continue
136136
}
137137

138138
if ($destjobs.name -contains $job.name)
139139
{
140140
if ($force -eq $false)
141141
{
142-
Write-Warning "Job $jobname exists at destination. Use -Force to drop and migrate."
142+
Write-Warning "[Job: $jobname] Job $jobname exists at destination. Use -Force to drop and migrate."
143143
continue
144144
}
145145
else
@@ -176,19 +176,22 @@ Shows what would happen if the command were executed using force.
176176
continue
177177
}
178178
}
179-
180-
If ($Pscmdlet.ShouldProcess($destination, "Creating Job $jobname"))
179+
180+
if ($DisableOnDestination)
181181
{
182-
if ($DisableOnDestination)
183-
{
182+
If ($Pscmdlet.ShouldProcess($destination, "Disabling $jobname"))
183+
{
184184
Write-Output "Disabling $jobname on $destination"
185185
$destserver.JobServer.Jobs.Refresh()
186186
$destserver.JobServer.Jobs[$job.name].IsEnabled = $False
187187
$destserver.JobServer.Jobs[$job.name].Alter()
188188
}
189+
}
189190

190-
if ($DisableOnSource)
191-
{
191+
if ($DisableOnSource)
192+
{
193+
If ($Pscmdlet.ShouldProcess($source, "Disabling $jobname"))
194+
{
192195
Write-Output "Disabling $jobname on $source"
193196
$job.IsEnabled = $false
194197
$job.Alter()

functions/Copy-SqlServerAgent.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ Shows what would happen if the command were executed.
103103

104104
# All of these support whatif inside of them
105105
Copy-SqlAgentCategory -Source $sourceserver -Destination $destserver -Force:$force
106-
Copy-SqlAlert -Source $sourceserver -Destination $destserver -Force:$force -IncludeDefaults
107106
Copy-SqlOperator -Source $sourceserver -Destination $destserver -Force:$force
107+
Copy-SqlAlert -Source $sourceserver -Destination $destserver -Force:$force -IncludeDefaults
108108
Copy-SqlProxyAccount -Source $sourceserver -Destination $destserver -Force:$force
109109
Copy-SqlSharedSchedule -Source $sourceserver -Destination $destserver -Force:$force
110110
Copy-SqlJob -Source $sourceserver -Destination $destserver -Force:$force -DisableOnDestination:$DisableJobsOnDestination -DisableOnSource:$DisableJobsOnSource

0 commit comments

Comments
 (0)