Private/Get-UnityItemByKey.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
<#
    .SYNOPSIS
#>


Function Get-UnityItemByKey {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $True,HelpMessage = 'EMC Unity Session')]
        $session,
        [Parameter(Mandatory = $True,HelpMessage = 'URI')]
        [String]$URI,
        [Parameter(Mandatory = $True,HelpMessage = 'Typename')]
        [String]$Typename,
        [Parameter(Mandatory = $True,HelpMessage = 'Key')]
        [String]$Key,
        [Parameter(Mandatory = $False,HelpMessage = 'Value')]
        [String[]]$Value,
        [parameter(Mandatory = $False, HelpMessage = 'Ressource Filter')]
        [string]$Filter
    )

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

    Process {

      If ($Session.TestConnection()) {

        #Building the URL from Object Type.
        If ($Filter) {
            $URL = Get-URLFromObjectType -Session $Session -URI $URI -TypeName $TypeName -Compact -Filter $Filter
        } else {
            $URL = Get-URLFromObjectType -Session $Session -URI $URI -TypeName $TypeName -Compact
        }
        
        Write-Debug -Message "[$($MyInvocation.MyCommand)] URL: $URL"

        #Sending the request
        $request = Send-UnityRequest -uri $URL -Session $Session -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) {

            $ResultsFiltered = @()

            If (-not $Value) {
                $Value = '*'
            }
          
            # Results filtering
            $ResultsFiltered += Find-FromFilter -Parameter $Key -Filter $Value -Data $Results

            If ($ResultsFiltered) {
            
                $ResultCollection = ConvertTo-Hashtable -Data $ResultsFiltered

                Foreach ($Result in $ResultCollection) {

                    # Instantiate and output object
                    New-Object -TypeName $TypeName -Property $Result

                } # End Foreach ($Result in $ResultCollection)
            } # End If ($ResultsFiltered)
        } # End If ($Results)
    } # End If ($Sess.TestConnection())

  }
    
}