Public/Get-UnityMetricQueryResult.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
Function Get-UnityMetricQueryResult {

  <#
      .SYNOPSIS
      A set of values for one or more metrics for a given period of time.
      .DESCRIPTION
      A set of values for one or more metrics for a given period of time.
      You need to have an active session with the array.
      .NOTES
      Written by Erwan Quelin under MIT licence - https://github.com/equelin/Unity-Powershell/blob/master/LICENSE
      .LINK
      https://github.com/equelin/Unity-Powershell
      .PARAMETER Session
      Specifies an UnitySession Object.
      .PARAMETER queryId
      Queries ID or object.
      .EXAMPLE
      Get-UnityMetricQueryResult -queryId 5

      Retrieve informations about query who's ID is 5.
  #>


  [CmdletBinding()]
  Param (
    [Parameter(Mandatory = $false,HelpMessage = 'EMC Unity Session')]
    $session = ($global:DefaultUnitySession | where-object {$_.IsConnected -eq $true}),
    [Parameter(Mandatory = $true,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True,HelpMessage = 'Queries ID or object')]
    [Object[]]$queryId
  )

  Begin {
    Write-Debug -Message "[$($MyInvocation.MyCommand)] Executing function"

    #Initialazing variables
    $ResultCollection = @()
    $URI = '/api/types/metricQueryResult/instances' #URI for the ressource (example: /api/types/lun/instances)
    $TypeName = 'UnityMetricQueryResult'
  }

  Process {
    Foreach ($sess in $session) {

      Write-Debug -Message "Processing Session: $($sess.Server) with SessionId: $($sess.SessionId)"

      If ($Sess.TestConnection()) {

          Foreach ($Query in $QueryID) {

            # Determine input and convert to object if necessary

            Write-Verbose "Input object type is $($Query.GetType().Name)"
            Switch -wildcard ($Query.GetType().Name)
            {
              "UnityMetricRealTimeQuery" {
                $ObjectID = $Query.id
              }

              "Int*" {
                If ($Object = Get-UnityMetricRealTimeQuery -Session $Sess -ID $Query -ErrorAction SilentlyContinue) {
                  $ObjectID = $Object.id
                } else {
                  Throw "This query does not exist"
                }
              }
            }

            $Filter = 'queryId EQ '+$ObjectID

            #Building the URL from Object Type.
            $URL = Get-URLFromObjectType -Session $sess -URI $URI -TypeName $TypeName -Compact -Filter $Filter

            Write-Verbose "URL: $URL"

            #Sending the request
            $request = Send-UnityRequest -uri $URL -Session $Sess -Method 'GET'

            #Formating the result. Converting it from JSON to a Powershell object
            $Results = ($request.content | ConvertFrom-Json).entries.content

            #Building the result collection (Add ressource type)
            If ($Results) {
              
              $ResultCollection = ConvertTo-Hashtable -Data $Results

              Foreach ($Result in $ResultCollection) {

                # Instantiate object
                $Object = New-Object -TypeName $TypeName -Property $Result

                # Output results
                $Object
              } # End Foreach ($Result in $ResultCollection)
            } # End If ($Results)
        } # End Foreach ($Query in $QueryID) {
      } # End If ($Sess.TestConnection())
    } # End Foreach ($sess in $session)
  } # End Process
} # End Function