Functions/Connect-vROpsRASession.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
Function Connect-vROpsRASession{
    <#
        .Synopsis
            Connect to vROps Rest API Session.
 
        .DESCRIPTION
            Connect to vROps Operations Manager server Rest API Session.
 
        .PARAMETER OMServer
            FQDN or IP address of server to connect to.
 
        .PARAMETER Credentials
            Credentials of an account that has access.
 
        .PARAMETER AuthSource
            This is the authoritative source.
 
        .PARAMETER UseUntrustedSSLCertificates
            Use this if you have untrusted certificates in your environment.
 
        .EXAMPLE
            $AuthToken = Connect-vROpsRASession -OMServer vROpsOMServer.CentralIndustrial.eu -Credentials $OMCreds
 
        .EXAMPLE
            $AuthToken = Connect-vROpsRASession -OMServer 10.11.12.13 -Credentials $OMCreds -UseUntrustedSSLCertificates
 
        .EXAMPLE
            $AuthToken = Connect-vROpsRASession -OMServer 10.11.12.13 -Credentials $OMCreds -AuthSource "CentralIndustrial"
 
        .OUTPUTS
            Function will return an authentication token that will be used as an Auth token in other functions
 
        .Notes
            .NOTES
            Author: Lars Panzerbjørn
            Creation Date: 2019.11.21
            Purpose/Change: Initial script development
 
    #>

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

        [Parameter(Mandatory,ParameterSetName="UsernamePwd")]
        [ValidateNotNullOrEmpty()]
        [string]$UserName,

        [Parameter(Mandatory,ParameterSetName="UsernamePwd")]
        [ValidateNotNullOrEmpty()]
        [string]$Password,

        [Parameter(Mandatory,ParameterSetName="Credentials")]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSCredential]$Credentials,

        [Parameter(ParameterSetName="Credentials")]
        [Parameter(ParameterSetName="UsernamePwd")]
        [switch]$UseUntrustedSSLCertificates,

        [Parameter(ParameterSetName="Credentials")]
        [Parameter(ParameterSetName="UsernamePwd")]
        [switch]$UseTLS12,

        [Parameter(ParameterSetName="Credentials")]
        [Parameter(ParameterSetName="UsernamePwd")]
        [ValidateNotNullOrEmpty()]
        [string]$AuthSource
    )

    Begin{
        Try{
            IF ($UseTLS12){
                [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
            }
        }
        Catch{
            $PSItem | Get-ErrorInfo
        }

        Try{
            IF (($UseUntrustedSSLCertificates) -and ($PSVersionTable.PSVersion.Major -eq 5)){
                #Allow untrusted SSL Certs
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
    }
}
"@

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
            }
        }
        Catch{
            $PSItem | Get-ErrorInfo
        }
        #Creating the body for the payload that will be used
        $JsonContentType = 'application/json'
        IF ($PSCmdlet.ParameterSetName -eq "UserNamePwd"){
            Write-Verbose "ParameterSetName UsernamePwd"
            $Body = @{
                username = $UserName
                password = $Password
            }
        }
        IF ($PSCmdlet.ParameterSetName -eq "Credentials"){
            Write-Verbose "ParameterSetName is Credentials"
            $Body = @{
                username = $Credentials.UserName;
                password = $Credentials.GetNetworkCredential().Password
            }
        }
        IF (!([string]::IsNullOrEmpty($Authsource))){
            $Body.authSource = $Authsource
        }
        ## Construct url
        $Uri = "https://$OMserver/suite-api/api/auth/token/acquire"
        Write-Verbose "Uri is $($Uri)"
        $Headers = @{accept=$JsonContentType}
        $Body = $Body | ConvertTo-Json
        $AuthResponseSplat = @{
            Method = "Post"
            Uri = $Uri
            Body = $Body
            ContentType = $JsonContentType
        }
    }
    Process{
        Try{
            $AuthResponse = Invoke-RestMethod @AuthResponseSplat -Headers $Headers -ErrorAction STOP
        }
        Catch [System.Net.WebException]{
            IF (($PSItem|Get-ErrorInfo).Exception -eq "Unable to connect to the remote server"){
                Write-Warning "You are unable to connect to the remote server."
                Write-warning "$(($PSItem|Get-ErrorInfo).Exception)"
                Write-warning "$(($PSItem|Get-ErrorInfo).Testing)"
                Return "$(($PSItem|Get-ErrorInfo).Exception)"
            }
            ELSEIF (($PSItem|Get-ErrorInfo).Exception -eq 'The remote server returned an error: (401) Unauthorized.'){
                Write-Warning "You are unauthorised to connect to the remote server."
                Write-warning "$(($PSItem|Get-ErrorInfo).Exception)"
                Write-warning "$(($PSItem|Get-ErrorInfo).Testing)"
                Return "$(($PSItem|Get-ErrorInfo).Exception)"
            }
            ELSE{
                Write-Warning "You are not allowing untrusted SSL certs. Good, you shouldn't. Please try again using the -UseUntrustedSSLCertificates switch.`n Or even better, fix your certs ;-`)"
                Write-warning "$(($PSItem|Get-ErrorInfo).Exception)"
                Write-warning "$(($PSItem|Get-ErrorInfo).Testing)"
                Return "$(($PSItem|Get-ErrorInfo).Exception)"
            }
        }
        Catch [System.NullReferenceException]{
            Write-Warning "Object reference not set to an instance of an object."
            Write-warning "$(($PSItem|Get-ErrorInfo).Exception)"
            Write-warning "$(($PSItem|Get-ErrorInfo).Testing)"
            Return "$(($PSItem|Get-ErrorInfo).Exception)"
        }
        Catch{
            Write-Warning "Something Happened"
            Write-warning "$(($PSItem|Get-ErrorInfo).Exception)"
            Write-warning "$(($PSItem|Get-ErrorInfo).Testing)"
            Return "$(($PSItem|Get-ErrorInfo).Exception)"
        }
    }
    End{
        Return $AuthResponse
    }
}