示例#1
0
        private void copyJobs(Server destserver, bool disableonsource, bool disableondest, bool dropdest)
        {
            JobCollection destjobs = destserver.JobServer.Jobs;

            foreach (Job job in sourceserver.JobServer.Jobs)
            {
                string     jobname = job.Name;
                ItemToCopy item    = itemsToCopy.Find(x => x.Name == jobname);
                if (item.IsChecked)
                {
                    if (!DBChecks.LoginExists(destserver, job.OwnerLoginName))
                    {
                        showOutput.displayOutput(string.Format("[Job: {0}] Owner {1} doesn't exist ion destination. Skipping. ", jobname, job.OwnerLoginName));
                        continue;
                    }

                    foreach (JobStep jobstep in job.JobSteps)
                    {
                        if (!DBChecks.DatabaseExists(destserver, jobstep.DatabaseName))
                        {
                            showOutput.displayOutput(string.Format("[Job: {0}] Database {1} doesn't exist on destination. Skipping. ", jobname, jobstep.DatabaseName));
                            continue;
                        }

                        if (!string.IsNullOrEmpty(jobstep.ProxyName) && !DBChecks.ProxyExists(destserver, jobstep.ProxyName))
                        {
                            showOutput.displayOutput(string.Format("[Job: {0}] Proxy {1} doesn't exist on destination. Skipping. ", jobname, jobstep.ProxyName));
                            continue;
                        }
                    }

                    if (!string.IsNullOrEmpty(job.OperatorToEmail) && !DBChecks.OperatorExists(destserver, job.OperatorToEmail))
                    {
                        showOutput.displayOutput(string.Format("[Job: {0}] Operator {1} doesn't exist on destination. Skipping. ", jobname, job.OperatorToEmail));
                        continue;
                    }

                    if (!string.IsNullOrEmpty(job.OperatorToNetSend) && !DBChecks.OperatorExists(destserver, job.OperatorToNetSend))
                    {
                        showOutput.displayOutput(string.Format("[Job: {0}] Operator {1} doesn't exist on destination. Skipping. ", jobname, job.OperatorToNetSend));
                        continue;
                    }

                    if (!string.IsNullOrEmpty(job.OperatorToPage) && !DBChecks.OperatorExists(destserver, job.OperatorToPage))
                    {
                        showOutput.displayOutput(string.Format("[Job: {0}] Operator {1} doesn't exist on destination. Skipping. ", jobname, job.OperatorToPage));
                        continue;
                    }

                    if (DBChecks.JobExists(destserver, jobname))
                    {
                        if (!dropdest)
                        {
                            showOutput.displayOutput(string.Format("Job: {0} already exists on destination server. Skipping. ", jobname));
                            continue;
                        }
                        else
                        {
                            try
                            {
                                destserver.JobServer.Jobs[jobname].Drop();
                                destserver.JobServer.Refresh();
                            }
                            catch (Exception ex)
                            {
                                showOutput.displayOutput(ex.Message);
                                continue;
                            }
                        }
                    }

                    try
                    {
                        StringCollection sql = job.Script();
                        for (int i = 0; i < sql.Count; i++)
                        {
                            sql[i] = sql[i].Replace(sourceserver.Name, destserver.Name);
                        }

                        destserver.ConnectionContext.ExecuteNonQuery(sql);

                        if (disableondest)
                        {
                            DBFunctions.ChangeJobStatus(destserver, jobname, false);
                        }

                        if (disableonsource)
                        {
                            DBFunctions.ChangeJobStatus(sourceserver, jobname, false);
                        }

                        showOutput.displayOutput(string.Format("Copied job {0} to {1}", jobname, destserver.Name));
                    }
                    catch (Exception ex)
                    {
                        showOutput.displayOutput(string.Format("Failed to copy job {0} to {1}", jobname, destserver.Name));
                        showOutput.displayOutput(ex.Message);
                        continue;
                    }
                }
            }
        }