This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Cancel individual jobs via Powershell

I am trying to automate clearing out various queued replications jobs over night to prevent the rollups for still being queued in the morning. I am able to pull the various information about the jobs I want to stop however I can seem to stop individual jobs via a power shell command.

Has anybody come across this issue or found a way round jobs holding each other up overnight when the nightly jobs are supposed to run?

Thnaks Jamie

  • Hi Jamie:
    Thank you for asking.
    This is a solved issue. I am swamped now so I do not have the time to get into specific details. To help you out I have pasted the code below. Copy it to a .ps1 file, make sure that there are no broken lines and run it via windows task scheduler on the core. No parameters are needed. Basically the queued rollups will be killed.
    Please let us know how it works for you. If you have specific questions I will probably be able to reply in details sometime next week.

     

    function do-restcallx{
    param(
    [Parameter(Mandatory=$true)]$uriend,
    $corename=$env:computername,
    $method="GET",
    $ContentType=$null,
    $credential=$null,
    $body=$null,
    $timeoutsec=3600,
    $portx="8006",
    [switch]$myerrormessage
    )
    $returnedobject=$null
    $uri="https://$($corename):$($portx)/apprecovery/api/core/$uriend"
    $xparams = @{"uri"=$uri;"Timeoutsec"=$timeoutsec}

    if($myerrormessage.IsPresent){
    $emessage = "`nERROR`nREST Call on -URI `"$uri`" with -Method $method has failed"
    }else{$emessage=$null}
    $xparams.Add("ErrorAction","STOP")
    $xparams.Add("Method",$method)
    if($credential){$xparams.Add("credential",$credential)}else{$xparams.Add("usedefaultcredentials",$true)}

    if($method -eq "GET"){
    try{
    #[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
    [xml]$returnedobject =  invoke-webrequest @xparams
    #[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
    return $returnedobject
    }catch {Write-Host "$emessage `r" -f RED;$xparams | ft ;return $null}
    }
    if($contenttype){$xparams.Add("contenttype",$contenttype)}

    if($body){$xparams.Add("body",$body)}
    try{
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
    [xml]$returnedobject = Invoke-RestMethod @xparams
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
    return $returnedobject
    }catch{Write-Host "$emessage `r" -f RED; ;"$($xparams | ft | out-string)" ;return $null}
    }

    function get-cjobs {
    param ([array]$agentids,[switch]$noagents,$days=$null,$hours=$null,$minstarttime=$null,$maxstarttime=$null,$jobstatus="Succeeded Failed Canceled",[array]$jobtypes=$null)
    if($noagents.IsPresent){$xmlagents=""}else{
    if($agentids.count -eq 0){return}
    $xmlagents=$null
    foreach($agentid in $agentids){
    $xmlagents += "<agentId xmlns=`"apprecovery.com/.../agentId>`n"
    }
    }
    $timespan=""
    if($minstarttime -ne $null -and $maxstarttime -ne $null){
    try{
    $timespan = @"
      <maxStartTime>$((get-date($maxstarttime)).tostring("yyyy-MM-ddTHH:mm:ss"))</maxStartTime>
      <minStartTime>$((get-date($minstarttime)).tostring("yyyy-MM-ddTHH:mm:ss"))</minStartTime>
    "@
    }catch{write-host "Error Parsing Min/Max StartTime";exit}
    }
    $today=get-date
    if ($days){
    $timespan = @"
      <maxStartTime>$(($today).tostring("yyyy-MM-ddTHH:mm:ss"))</maxStartTime>
      <minStartTime>$(($today.AddDays((-1)*[int]$days)).tostring("yyyy-MM-ddTHH:mm:ss"))</minStartTime>
    "@
      }
    if ($hours){
    $timespan = @"
      <maxStartTime>$(($today).tostring("yyyy-MM-ddTHH:mm:ss"))</maxStartTime>
      <minStartTime>$(($today.AddHours(-1*[int]$hours)).tostring("yyyy-MM-ddTHH:mm:ss"))</minStartTime>
    "@
      }
    $backgroundjobtype=$null
    if($jobtypes.count -gt 0 ){
    $jobtypes | ForEach-Object {$backgroundjobtype +=
    @"
    <BackgroundJobType xmlns="apprecovery.com/.../BackgroundJobType>
    "@
    }
    }
    $Body2=@"
    <?xml version="1.0" ?>
    <backgroundJobSearchParameters xmlns="schemas.datacontract.org/.../Replay.Core.Contracts.BackgroundJobs">
      <agentIds>
        $xmlagents
      </agentIds>
      <backgroundJobTypes>
      $backgroundjobtype
      </backgroundJobTypes>
      <jobKindFlags xmlns:i="www.w3.org/.../jobKindFlags>
      <jobStatusFlags xmlns:i="www.w3.org/.../jobStatusFlags>
    $timespan
      <summarySearchMethod>Regex</summarySearchMethod>
      <summarySearchOptions>All</summarySearchOptions>
      <summarySearchPattern>[\s\S]*</summarySearchPattern>
    </backgroundJobSearchParameters>
    "@
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
    try{
    $alljobsforagent = invoke-restmethod -URI "https://$($env:computername):8006/apprecovery/api/core/jobmgr/jobs/all" -Method "PUT" -usedefaultcredentials -Body $body2  -ContentType "application/xml" -ErrorAction stop
    }catch{[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null;"Error: Exiting";exit}
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
    return $alljobsforagent.jobs.job | sort-object -Property startTime -Descending
    }
    $currentjobs=get-cjobs -noagents -jobstatus "Queued"  | select-object status, summary,phase,id
    $xjobs=@()
    $xJobs = $currentjobs |where {$_.summary -like "Rolling*"}
    #$xJobs += $currentjobs |where {$_.summary -like "Archive*"}
    foreach($xjob in $xjobs){
    Write-host "$($xjob | ft | out-string)" -ForegroundColor Yellow
    Invoke-RestMethod -Uri "https://$($env:computername):8006/apprecovery/api/core/jobmgr/jobs/$($xjob.id)" -Method DELETE -UseDefaultCredentials
    }
  • Jamie -- looks that the code gets messed up in the forum display. If you cannot figure it out, try to befriend on the forum and send me an email address so I can send it to you as an attachment!
  • Hi Tudor

    Thanks for the quick reply we are actually looking to cancel the queued replications and about midnight to clear the queue to allow the rollups to run overnight. I have been having a look over the script and I imagine it would be very similar to allow the cancelation of the replications instead of the rollups

    Thanks Jamie
  • Yeah -- just add or change a line looking as
    $xJobs += $currentjobs |where {$_.summary -like "*Replica*"}

    (you can use a better filter as I do not remember the exact wording of replication jobs).
    I have a script that sets replication on a schedule with strict enforcement as well -- but this will be for next week :)