快速,持续,稳定,傻瓜式
支持Mysql,Sqlserver数据同步

[mssql 跨数据库同步]DBA成长之路

请联系QQ:1793040 索取软件

param($souceSererIP='sourceServer',$targetServerIp='.')

Import-Module sqlps-DisableNameChecking
[system.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")|Out-Null
[system.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlEnum")|Out-Null
[system.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum")|Out-Null

[string] $script=''
$server=New-Object Microsoft.SqlServer.Management.Smo.Server($souceSererIP)
$dt=[System.DateTime]::Now.AddDays(-1).ToString("yyyy-MM-dd HH")+":00:00"


foreach($job in $server.JobServer.Jobs)
{
    if($job.Name -ne "test")
    {
        continue
    }
    if($job.DateLastModified -gt $dt)
    {
        $jobName=$job.Name
        $jobName=$jobName.Replace("/","_").Replace("'","_").Replace("[","_").Replace("]","_").Replace("(","_").Replace(")","_").Replace("&","_").Replace('"',"_").Replace("\","_").Replace(":","_").Replace("{","_").Replace("}","_").Replace("<","_").Replace(">","_").Replace("*","_").Replace("#","_")        
    }
    $nl="`n"
    $script='if  exists(select * from msdb.dbo.sysjobs where name=N'''+$jobName +''' )'+ $nl +'begin '+ $nl +'Exec msdb.dbo.sp_delete_job @job_name=N'''+$jobName+''' '+$nl+'end '+$nl +'GO '
    $script=$script+$nl+$job.Script()
    #$script
    try
    {
    Invoke-Sqlcmd-ServerInstance $targetServerIp-query $script-ErrorAction Stop
    }
    catch
    {
        $ErrorMessage=$_.Exception.Message
        $FailedItem=$_.Exception.ItemName
        ##send email if have issue
        #Send-MailMessage-From ExpensesBot@MyCompany.Com-To WinAdmin@MyCompany.Com-Subject "HR File Read Failed!"-SmtpServer EXCH01.AD.MyCompany.Com-Body "We failed to read file $FailedItem.The error message was $ErrorMessage"
         Break
    }
    $output='Execute success! Jobname:"{0}" from "{1}" to "{2}"' -f    $jobName,$souceSererIP,$targetServerIp
    Write-Host $output
}

相关推荐

咨询软件
 
QQ在线咨询
售前咨询热线
QQ1793040