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.

Revisions

  1. davops created this gist Aug 3, 2016.
    18 changes: 18 additions & 0 deletions Start-SQLSync.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    $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}
    }