
Tag : powershell,SQL server agent jobs
Ibland behöver man i exempelvis ett AAG kopiera jobb som ändrats eller skapats senaste tiden.
Jag använder mig av dba tools till det först tar jag fram jobben på båda maskinerna och jämför dem.
Import-Module -Name SqlServer
Import-Module -Name DbaTools
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | Out-Null
$SourceServer = "server1"
$DestServer = "server2"
$Date = (Get-Date).AddDays(-60)
$JobSource = Get-DbaAgentJob -SqlInstance $SourceServer | Select-Object Name
$JobsDest = Get-DbaAgentJob -SqlInstance $DestServer | Select-Object -ExpandProperty Name
$JobsToCopy = Get-DbaAgentJob -SqlInstance $SourceServer | Where-Object { $.DateLastModified -gt $Date -and $.Name -notlike "ITDRIFT" } | Select-Object Name
$NewJobs = $JobSource | Where-Object { -not $JobsDest.Contains($.Name) -and $.Name -notlike "ITDRIFT" }
Copy-DbaAgentJob -Source $SourceServer -Destination $DestServer -Job $JobsToCopy.Name -Force
Copy-DbaAgentJob -Source $SourceServer -Destination $DestServer -Job $Newjobs.Name -Force