Skip to content

Instantly share code, notes, and snippets.

@davops
Created August 3, 2016 14:05
Show Gist options
  • Save davops/2b35e4f970b139334d0afb961c167b0c to your computer and use it in GitHub Desktop.
Save davops/2b35e4f970b139334d0afb961c167b0c to your computer and use it in GitHub Desktop.
Starts the replication synchronization in SQL for a given subscription using PowerShell. It's the equivalent of "Start Synchronization" in the GUI of Replication Monitor or starting the job that corresponds to that particular subscription. The names of the jobs it finds are written to a file.
$serverName = "SQL1" ###name of the SQL instance, not the server name
$jobQuery = "select jobs.name from msdb.dbo.sysjobs jobs (nolock)
join msdb.dbo.syscategories categories (nolock)
on jobs.category_id = categories.category_id
where jobs.name like '$servername-YourReplicationName%' AND categories.name = 'REPL-Merge'"###Used for merge replication but can be changed
$jobQuery = (Invoke-Sqlcmd -ServerInstance $serverName -Query $jobQuery | Select-Object -Property name).name
$jobQuery | Out-File c:\logs\database\replication__sync_log.txt -Append
foreach ($row in $jobQuery)
{
Write-Host "The script will start syncrhonization for $row. Is this correct?."
$answer = (Read-Host -Prompt "Press 1 to continue. Press 2 to exit.").Trim()
if($answer -eq "1"){
$query = "USE msdb EXEC dbo.sp_start_job N'" + $row + "'"
Invoke-Sqlcmd -ServerInstance $serverName -Query $query
}
else{exit}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment