Scripts/Utility/get-cohesityprotectionsourcesummary.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
function Get-CohesityProtectionSourceSummary {
    <#
        .SYNOPSIS
        Get the summary of protection sources.
        .DESCRIPTION
        The Get-CohesityProtectionSourceSummary is used to get the summary of protected and unprotected sources, for example the VMs, databases, volumes, physical servers etc.
        .EXAMPLE
        Get-CohesityProtectionSourceSummary
        .EXAMPLE
        Get-CohesityProtectionSourceSummary -BasicSummary:$true
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false)]
        [switch]$BasicSummary
    )

    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
        $cohesityCluster = $cohesitySession.ClusterUri
        $cohesityToken = $cohesitySession.Accesstoken.Accesstoken
    }

    Process {
        $clusterConfig = Get-CohesityClusterConfiguration
        $cohesityClusterURL = $cohesityCluster + '/irisservices/api/v1/public/dashboard?clusterId=' + $clusterConfig.id
        $cohesityHeaders = @{'Authorization' = 'Bearer ' + $cohesityToken }
        $resp = Invoke-RestApi -Method Get -Uri $cohesityClusterURL -Headers $cohesityHeaders
        if ($resp) {
            if($true -eq $BasicSummary) {
                $result = ($resp.dashboard.protectedObjects | Select-Object -ExpandProperty objectsProtected)
                $summary = @{
                    envType = "Total"
                    protectedCount = $resp.dashboard.protectedObjects.protectedCount
                    protectedSizeBytes = $resp.dashboard.protectedObjects.protectedSizeBytes
                    unprotectedCount = $resp.dashboard.protectedObjects.unprotectedCount
                    unprotectedSizeBytes = $resp.dashboard.protectedObjects.unprotectedSizeBytes
                }
                $result += $summary
                ($result  |Select-Object envType,protectedCount, `
                @{Name="protected size(GB)"; Expression={[math]::round($_.protectedSizeBytes/1GB, 2)}}, `
                unprotectedCount, @{Name="unprotected size(GB)"; Expression={[math]::round($_.unprotectedSizeBytes/1GB, 2)}})
            } else {
                $resp
            }
        }
        else {
            $errorMsg = "Protection source summary : Failed to get"
            Write-Output $errorMsg
            CSLog -Message $errorMsg
        }
    }

    End {

    }
}