Scripts/ProtectionPolicy/new-cohesityprotectionpolicy.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
function New-CohesityProtectionPolicy {
    <#
        .SYNOPSIS
        Create a new protection policy.
        .DESCRIPTION
        The New-CohesityProtectionPolicy function is used to create a protection new protection policy.
        .NOTES
        Published by Cohesity
        .LINK
        https://cohesity.github.io/cohesity-powershell-module/#/README
        .EXAMPLE
        New-CohesityProtectionPolicy -PolicyName <string> -BackupInHours 14 -RetainInDays 25 -IncrementalSchedule INCREMENTAL-ONLY
        .EXAMPLE
        New-CohesityProtectionPolicy -PolicyName <string> -BackupInHours 14 -RetainInDays 25 -IncrementalSchedule INCREMENTAL-FULL
        .EXAMPLE
        New-CohesityProtectionPolicy -PolicyName <string> -BackupInHours 14 -RetainInDays 25 -VaultName <string>
    #>

    [CmdletBinding(SupportsShouldProcess = $True, ConfirmImpact = "High")]
    Param(
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]$PolicyName,
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [int]$BackupInHours,
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [int]$RetainInDays,
        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("INCREMENTAL-ONLY", "INCREMENTAL-FULL")]
        [string]$IncrementalSchedule = "INCREMENTAL-ONLY",
        [Parameter(Mandatory = $false)]
        $VaultName = $null
    )
    Begin {
        if (-not (Test-Path -Path "$HOME/.cohesity")) {
            throw "Failed to authenticate. Please connect to the Cohesity Cluster using 'Connect-CohesityCluster'"
        }
        $session = Get-Content -Path $HOME/.cohesity | ConvertFrom-Json

        $server = $session.ClusterUri

        $token = $session.Accesstoken.Accesstoken
    }

    Process {
        if ($PSCmdlet.ShouldProcess($PolicyName)) {
            $url = $server + '/irisservices/api/v1/public/protectionPolicies'

            $headers = @{'Authorization' = 'Bearer ' + $token }

            $snapshotArchivalCopyPolicies = $null
            if ($VaultName) {
                $resp = Get-CohesityVault -VaultName $VaultName
                if ($null -eq $resp) {
                    Write-Output "Vault (external target) name '$VaultName' does not exists"
                    return
                }
                # Except the vault info all the default values referenced from cohesity UI
                $snapshotArchivalCopyPolicies = @(
                    @{
                        copyPartial = $true
                        daysToKeep  = 90
                        multiplier  = 1
                        periodicity = "kDay"
                        target      = @{
                            vaultId   = $resp.Id
                            vaultName = $resp.Name
                            vaultType = "kCloud"
                        }
                    }
                )
            }
            $fullSchedule = $null
            if ("INCREMENTAL-FULL" -eq $IncrementalSchedule) {
                $fullSchedule = @{
                    periodicity   = "kDaily"
                    dailySchedule = @{
                        days = @()
                    }
                }
            }
            $payload = @{
                name                         = $PolicyName
                incrementalSchedulingPolicy  = @{
                    periodicity        = "kContinuous"
                    continuousSchedule = @{
                        # convert to minutes
                        backupIntervalMins = $BackupInHours * 60
                    }
                }
                daysToKeep                   = $RetainInDays
                fullSchedulingPolicy         = $fullSchedule
                snapshotArchivalCopyPolicies = $snapshotArchivalCopyPolicies
            }
            $payloadJson = $payload | ConvertTo-Json -Depth 100
            $resp = Invoke-RestApi -Method Post -Uri $url -Headers $headers -Body $payloadJson
            if ($resp) {
                $resp
            }
            else {
                $errorMsg = "Protection Policy : Failed to create"
                Write-Output $errorMsg
                CSLog -Message $errorMsg
            }
        }
    }
    End {
    }
}