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

  <#
      .SYNOPSIS
      Historical values for requested metrics.
      .DESCRIPTION
      Historical values for requested metrics.
      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 Path
      Stat path for the metric. A stat path identifies the metric's location in the stats namespace.
      .PARAMETER Count
      Specifies the number of samples to display.
      .EXAMPLE
      Get-UnityMetricValue -Path 'sp.*.cpu.summary.utilization'

      Retrieves information about metrics who's path is 'sp.*.cpu.summary.utilization'
  #>


  [CmdletBinding()]
  Param (
    [Parameter(Mandatory = $false,HelpMessage = 'EMC Unity Session')]
    $session = ($global:DefaultUnitySession | where-object {$_.IsConnected -eq $true}),
    [Parameter(Mandatory = $false,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True,HelpMessage = 'Stat path for the metric')]
    [Object[]]$Path,
    [Parameter(Mandatory = $false,HelpMessage = 'Specifies the number of samples to display')]
    [Int64]$Count = 20
  )

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

    #Initialazing variables
    $ResultCollection = @()
    $URI = '/api/types/metricValue/instances' #URI
    $TypeName = 'UnityMetricValue'
  }

  Process {
    Foreach ($sess in $session) {

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

      If ($Sess.TestConnection()) {

        Foreach ($P in $Path) {

          Write-Verbose "Processing path: $P"

          # Determine input and convert to object if necessary
          Write-Verbose "Input object type is $($P.GetType().Name)"
          Switch ($P.GetType().Name)
          {
            "UnityMetric" {$ObjectPath = $P.Path}
            "String" {$ObjectPath = $P}
          }

          Write-Verbose "Object path: $ObjectPath"

          $PathFilter = "filter=path EQ `"$ObjectPath`""
          $Page = '&page=1'
          $Results = @()

          While (($Results.count) -le $Count) {
            #Building the URL from Object Type.
            $URL = 'https://'+ $sess.Server + $URI +'?'+ $PathFilter + $Page
            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

            $Links = ($request.content | ConvertFrom-Json).links 

            $Page = ($links | Where-Object {$_.rel -eq 'next'}).href
          }

          #Building the result collection (Add ressource type)
          If ($Results) {

            $Results = $Results | Select-Object -First $Count
    
            $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 ($P in $Path)
      } # End If ($Sess.TestConnection())
    } # End Foreach ($sess in $session)
  } # End Process
} # End Function