Scripts/ProtectionJob/Stop-CohesityProtectionJob.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function Stop-CohesityProtectionJob {
    <#
        .SYNOPSIS
        Cancels a running protection job.
        .DESCRIPTION
        The Stop-CohesityProtectionJob function is used to stop the protection job.
        Cancels a running protection job.
        .NOTES
        Published by Cohesity
        .LINK
        https://cohesity.github.io/cohesity-powershell-module/#/README
        .EXAMPLE
        Stop-CohesityProtectionJob -Id 78773 -JobRunId 85510
    #>

    [OutputType('System.Object')]
    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "High")]
    Param(
        [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "ById")]
        [ValidateRange(1, [long]::MaxValue)]
        [long]$Id,
        [Parameter(Mandatory = $true, ParameterSetName = "ByName")]
        [ValidateNotNullOrEmpty()]
        [string]$Name,
        [Parameter(Mandatory = $false)]
        [switch]$StopArchivalJob,
        [Parameter(Mandatory = $false)]
        [switch]$StopCloudSpinJob,
        [Parameter(Mandatory = $false)]
        [switch]$StopReplicationJob,
        [Parameter(Mandatory = $false)]
        [long]$JobRunId
    )

    Begin {
        if (-not (Test-Path -Path "$HOME/.cohesity")) {
            throw "Failed to authenticate. Please connect to the Cohesity Cluster using 'Connect-CohesityCluster'"
        }
        $cohesitySession = Get-Content -Path $HOME/.cohesity | ConvertFrom-Json
        $cohesityServer = $cohesitySession.ClusterUri
        $cohesityToken = $cohesitySession.Accesstoken.Accesstoken
    }

    Process {
        $currentProtectionJob = $null
        if ($Name) {
            $job = Get-CohesityProtectionJob -Names $Name
            if (-not $job) {
                Write-Output "Job name '$Name' not found"
                return
            }
            $Id = $job.Id
            $currentProtectionJob = $job
        }
        if (-not $JobRunId) {
            $job = Get-CohesityProtectionJob -Ids $Id
            if (-not $job) {
                Write-Output "Job id '$Id' not found"
                return
            }
            $JobRunId = $job.LastRun.BackupRun.JobRunId
            $currentProtectionjob = $job
        }
        if ($PSCmdlet.ShouldProcess($Id)) {
            $copyRun = $null;

            if ($StopArchivalJob.IsPresent) {
                $copyRun = $currentProtectionjob.LastRun.CopyRun | where-object { $_.Target.Type -eq "kArchival" }
                if (-not $copyRun) {
                    Write-Output "Could not find the archival task."
                    return
                }
            }
            if ($StopCloudSpinJob.IsPresent) {
                $copyRun = $currentProtectionjob.LastRun.CopyRun | where-object { $_.Target.Type -eq "kCloudDeploy" }
                if (-not $copyRun) {
                    Write-Output "Could not find the cloud deploy task."
                    return
                }
            }
            if ($StopReplicationJob.IsPresent) {
                $copyRun = $currentProtectionjob.LastRun.CopyRun | where-object { $_.Target.Type -eq "kRemote" }
                if (-not $copyRun) {
                    Write-Output "Could not find the replication task."
                    return
                }
            }

            $payload = [Cohesity.Model.CancelProtectionJobRunParam]::new()
            if ($copyRun) {
                $taskUid = [Cohesity.Model.UniversalId]::new()
                $taskUid.Id = $copyRun.TaskUid.Id
                $taskUid.clusterId = $copyRun.TaskUid.clusterId
                $taskUid.clusterIncarnationId = $copyRun.TaskUid.clusterIncarnationId
                # When the non-local job is being stopped
                # The copy task uid and job id would be used for Archival, Cloud spin and Remote targets
                $payload.CopyTaskUid = $taskUid
            }
            else {
                # When the local job is being stopped
                $payload.JobRunId = $JobRunId
            }

            $cohesityHeaders = @{'Authorization' = 'Bearer ' + $cohesityToken }
            $url = '/irisservices/api/v1/public/protectionRuns/cancel/' + $Id
            $cohesityUrl = $cohesityServer + $url
            $payloadJson = $payload | ConvertTo-Json -Depth 100
            $resp = Invoke-RestApi -Method Post -Uri $cohesityUrl -Headers $cohesityHeaders -Body $payloadJson
            $resp | Out-Null
            Write-Output "Protection Job Run cancelled."
        }
    }

    End {
    }
}