Skip to content

Commit

Permalink
Merge pull request #4 from wsmelton/development
Browse files Browse the repository at this point in the history
Refresh branch from 08-18-16
  • Loading branch information
Shawn Melton authored Aug 19, 2016
2 parents acdd682 + f050637 commit c9ccb1d
Show file tree
Hide file tree
Showing 16 changed files with 1,422 additions and 581 deletions.
7 changes: 5 additions & 2 deletions dbatools.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
RootModule = 'dbatools.psm1'

# Version number of this module.
ModuleVersion = '0.8.5.2'
ModuleVersion = '0.8.5.52'

# ID used to uniquely identify this module
GUID = '9d139310-ce45-41ce-8e8b-d76335aa1789'
Expand Down Expand Up @@ -133,7 +133,10 @@
'Get-DbaDatabaseFreespace',
'Get-DbaClusterActiveNode',
'Test-DbaDatabaseOwner',
'Set-DbaDatabaseOwner'
'Set-DbaDatabaseOwner',
'Test-DbaJobOwner',
'Set-DbaJobOwner',
'Test-DbaVirtualLogFile'
)

# Cmdlets to export from this module
Expand Down
72 changes: 63 additions & 9 deletions functions/Copy-SqlAlert.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,12 @@ Shows what would happen if the command were executed using force.
{
try
{
Write-Verbose "Dropping Alert $alertname"
$destserver.JobServer.Alerts[$alertname].Drop()
Write-Verbose "Dropping Alert $alertname on $destserver"
#$destserver.JobServer.Alerts[$alertname].Drop()

$sql = "EXEC msdb.dbo.sp_delete_alert @name = N'$($alert.name)';"
Write-Verbose $sql
$destserver.ConnectionContext.ExecuteNonQuery($sql) | Out-Null
}
catch
{
Expand All @@ -142,7 +146,7 @@ Shows what would happen if the command were executed using force.
}
}
}

# job is created here.
If ($Pscmdlet.ShouldProcess($destination, "Creating Alert $alertname"))
{
try
Expand All @@ -163,12 +167,6 @@ Shows what would happen if the command were executed using force.
$destserver.JobServer.Alerts.Refresh()
$destserver.JobServer.Jobs.Refresh()

$newalert = $destserver.JobServer.Alerts[$alertname]
$notifications = $alert.EnumNotifications()
$newnotifications = $newalert.EnumNotifications()
$job = $alert.JobId
$jobname = $alert.JobName

# Super workaround but it works
if ($alert.JobId -ne '00000000-0000-0000-0000-000000000000')
{
Expand All @@ -191,6 +189,62 @@ Shows what would happen if the command were executed using force.
}
}
}


$newalert = $destserver.JobServer.Alerts[$alertname]
$notifications = $alert.EnumNotifications()
$newnotifications = $newalert.EnumNotifications()
$job = $alert.JobId
$jobname = $alert.JobName

If ($Pscmdlet.ShouldProcess($destination, "Moving Notifications $alertname"))
{
try
{
foreach ($notify in $notifications)
# cant add them this way, we need to modify the existing one or give all options that are supported.
{
$nm = @()
if ($notify.UseNetSend -eq $true)
{
write-verbose "Adding net send"
$nm += "NetSend"
}

if ($notify.UseEmail -eq $true)
{
write-verbose "Adding email"
$nm += "NotifyEmail"
}

if ($notify.UsePager -eq $true)
{
write-verbose "Adding pager"
$nm += "Pager"
}
$nml = $nm -join ", "

$newalert.AddNotification($notify.OperatorName, [Microsoft.SqlServer.Management.Smo.Agent.NotifyMethods]$nml) # concat the notify methods together
}
}
catch
{
$e = $_.Exception
$line = $_.InvocationInfo.ScriptLineNumber
$msg = $e.Message

if ($e -like '*The specified @operator_name (''*'') does not exist*')
{
Write-Warning "One or more operators for this alert are not configured and will not be added to this alert."
Write-Warning "Please run Copy-SqlOperator if you would like to move operators to destination server."
}
else
{
Write-Error "caught exception: $e at $line : $msg"
}
}
}

}
}

Expand Down
23 changes: 13 additions & 10 deletions functions/Copy-SqlJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Shows what would happen if the command were executed using force.
if ($missingdb.count -gt 0 -and $dbnames.count -gt 0)
{
$missingdb = ($missingdb | Sort-Object | Get-Unique) -join ", "
Write-Warning "Database(s) $missingdb doesn't exist on destination. Skipping."
Write-Warning "[Job: $jobname] Database(s) $missingdb doesn't exist on destination. Skipping."
continue
}

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

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

if ($destjobs.name -contains $job.name)
{
if ($force -eq $false)
{
Write-Warning "Job $jobname exists at destination. Use -Force to drop and migrate."
Write-Warning "[Job: $jobname] Job $jobname exists at destination. Use -Force to drop and migrate."
continue
}
else
Expand Down Expand Up @@ -176,19 +176,22 @@ Shows what would happen if the command were executed using force.
continue
}
}

If ($Pscmdlet.ShouldProcess($destination, "Creating Job $jobname"))
if ($DisableOnDestination)
{
if ($DisableOnDestination)
{
If ($Pscmdlet.ShouldProcess($destination, "Disabling $jobname"))
{
Write-Output "Disabling $jobname on $destination"
$destserver.JobServer.Jobs.Refresh()
$destserver.JobServer.Jobs[$job.name].IsEnabled = $False
$destserver.JobServer.Jobs[$job.name].Alter()
}
}

if ($DisableOnSource)
{
if ($DisableOnSource)
{
If ($Pscmdlet.ShouldProcess($source, "Disabling $jobname"))
{
Write-Output "Disabling $jobname on $source"
$job.IsEnabled = $false
$job.Alter()
Expand Down
2 changes: 1 addition & 1 deletion functions/Copy-SqlServerAgent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ Shows what would happen if the command were executed.

# All of these support whatif inside of them
Copy-SqlAgentCategory -Source $sourceserver -Destination $destserver -Force:$force
Copy-SqlAlert -Source $sourceserver -Destination $destserver -Force:$force -IncludeDefaults
Copy-SqlOperator -Source $sourceserver -Destination $destserver -Force:$force
Copy-SqlAlert -Source $sourceserver -Destination $destserver -Force:$force -IncludeDefaults
Copy-SqlProxyAccount -Source $sourceserver -Destination $destserver -Force:$force
Copy-SqlSharedSchedule -Source $sourceserver -Destination $destserver -Force:$force
Copy-SqlJob -Source $sourceserver -Destination $destserver -Force:$force -DisableOnDestination:$DisableJobsOnDestination -DisableOnSource:$DisableJobsOnSource
Expand Down
Loading

0 comments on commit c9ccb1d

Please sign in to comment.