WiP/Get-vROpsResourceKind.WiP.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
112
113
114
115
116
117
118
119
120
121
Function Get-vROpsResourceKind
{
    <#
        .Synopsis
            Collects ResourceKinds from vROps via REST API.
 
        .DESCRIPTION
            Collects ResourceKinds from vROps Operations Manager server via REST API.
 
        .PARAMETER OMServer
            FQDN or IP address of Operations Manager server to connect to.
 
        .PARAMETER AuthToken
            Authorisation Token that has been generated previously, either via Connect-vROpsRASession or another method.
 
        .PARAMETER AuthResource
            Authorisation Resource object that has been generated previously, either via Connect-vROpsRASession or another method.
 
        .EXAMPLE
            $AuthToken = (Connect-vROpsRASession -OMServer vROpsOMServer.CentralIndustrial.eu -Credentials $OMCreds).Token
            Get-vROpsResourceKind -OMServer vROpsOMServer.CentralIndustrial.eu -AuthToken $AuthToken
 
        .EXAMPLE
            $AuthResource = Connect-vROpsRASession -OMServer 10.11.12.13 -Credentials $OMCreds -UseUntrustedSSLCertificates
            Get-vROpsResourceKind -OMServer 10.11.12.13 -AuthResource $AuthToken
 
        .EXAMPLE
            $OMserver = '10.11.12.13'
            $AuthToken = Connect-vROpsRASession -OMServer $OMserver -Credentials $OMCreds -AuthSource "CentralIndustrial"
            Get-vROpsResourceKind -OMServer $OMserver -AuthToken $AuthToken.Token
 
        .OUTPUTS
            This will output a list of ResourceKindKeys available to the environment.
 
        .Notes
            .NOTES
            Version: 0.1
            Author: Lars Panzerbjørn
            Creation Date: 2019.11.25
            Purpose/Change: Initial script development
 
    #>

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory,ParameterSetName="Token")]
        [Parameter(Mandatory,ParameterSetName="Object")]
        [ValidateNotNullOrEmpty()]
        [string]$OMServer,

        [Parameter(Mandatory,ParameterSetName="Token")]
        [ValidateNotNullOrEmpty()]
        [PSObject]$AuthToken,

        [Parameter(Mandatory,ParameterSetName="Object")]
        [ValidateNotNullOrEmpty()]
        [PSObject]$AuthResource,

        [Parameter(ParameterSetName="Token")]
        [Parameter(ParameterSetName="Object")]
        [ValidateSet('JSON', 'XML')]
        [string]$Type="JSON"
    )

    Begin
    {
        IF ($PSCmdlet.ParameterSetName -eq "Object") {$AuthToken = $AuthResource.Token}
        $Authorization = "vRealizeOpsToken $AuthToken"
        IF ($Type -eq "JSON") {$RestType = 'application/json'}
        IF ($Type -eq "XML") {$RestType = "application/xml"}
        $Headers = @{Authorization=$Authorization}
        $InvokeRestMethodSplat = @{
            Headers = $Headers
            Method = "GET"
        }
        IF (!([string]::IsNullOrEmpty($RestType)))
        {
            $Headers.Accept = $RestType
            $InvokeRestMethodSplat.ContentType = $RestType
        }
        $BaseURL = "https://" + $OMserver + "/suite-api/api/"
        $PageSize = "5000" #Maximum is 10000
        $Page = 0
        $Resourcelist = @()
    }
    Process
    {
        Try
        {
            DO
            {
                Write-Verbose "Page $($Page)"
                $ResourcesURL = $BaseURL + "resources/?page=$page&pageSize=$PageSize" #A stylistic choice was made here to highlight the BaseURL. Page and PageSize weren't so important to highlight.
                $Resources = Invoke-RestMethod @InvokeRestMethodSplat -Uri $ResourcesURL
                $Resourcelist += $Resources.resourceList.resourceKey.resourceKindKey
                $Page++
            }
            UNTIL (($Resources.links.href | Select -first 1) -eq ($Resources.links.href | Select -last 1))
            $Resourcelist = $Resourcelist | Sort-Object | Get-Unique
        }
        Catch [System.Net.WebException]
        {
            IF (($PSItem | Get-ErrorInfo).Exception -eq 'The remote server returned an error: (401) Unauthorized.')
            {
                Write-Warning "Failed to login. The remote server returned an error: (401) Unauthorized."
            }
        }
        Catch
        {
            Write-Warning "Failed to get resources.
                Exception: $(($PSItem | Get-ErrorInfo).Exception)
                Reason: $(($PSItem | Get-ErrorInfo).Reason)
                Fullname: $(($PSItem | Get-ErrorInfo).Fullname)
            "

        }
    }
    End
    {
        Return $Resourcelist
    }
}