Common.psm1

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 2c1a2f98-e781-403c-9c81-85b736767abc
 
.AUTHOR RaviCKolandaiswamy
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Basic Functions for calling various APIs
 
#>
 

#Get public and private function definition files.

#check PS version for this, PS 6 and above use -SkipCertificateCheck for Invoke-RestMethod
if ($PSVersionTable.PSVersion.Major -lt 6)
{
    #Ignore SSL errors
    If ($Null -eq ([System.Management.Automation.PSTypeName]'TrustAllCertsPolicy').Type) {
        Add-Type -Debug:$False @"
            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
}


#Enable TLS, TLS1.1, TLS1.2 in this session if they are available
IF([Net.SecurityProtocolType]::Tls) {[Net.ServicePointManager]::SecurityProtocol=[Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls}
IF([Net.SecurityProtocolType]::Tls11) {[Net.ServicePointManager]::SecurityProtocol=[Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls11}
IF([Net.SecurityProtocolType]::Tls12) {[Net.ServicePointManager]::SecurityProtocol=[Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12}

function Invoke-RestApiRequest {
    [CmdletBinding(DefaultParameterSetName = 'EndpointName')]
    param (
         [Parameter(ParameterSetName = 'Endpoint')]
          $url,
         [Parameter(ParameterSetName = 'Endpoint')]
          $user,
         [Parameter(ParameterSetName = 'Endpoint')]
          $pass,

          [Parameter(ParameterSetName = 'EndpointName')]
          $endpointName,

          [Parameter(ParameterSetName = 'EndpointName')]
          [Parameter(ParameterSetName = 'Endpoint')]
          $relativeUrl,

          [Parameter(ParameterSetName = 'EndpointName')]
          [Parameter(ParameterSetName = 'Endpoint')]
          $jsonBody,

          [Parameter(ParameterSetName = 'EndpointName')]
          [Parameter(ParameterSetName = 'Endpoint')]
          $method = 'GET'
    )
    
    if ($PSCmdlet.ParameterSetName -eq 'EndpointName') 
    {
        $connection = Get-ApiConnection $endpointName
        $url = $connection.ApiEndpointUrl
        $user = $connection.ApiUser
        $pass = $connection.ApiKey
    } 

    $decodedCreds =[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))
    $pair = "$($user):$($decodedCreds)"
    $encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
    $basicAuthValue = "Basic $encodedCreds"
    $Headers = @{
        Authorization = $basicAuthValue
    }

    $params = @{
        Uri         = $url.TrimEnd('/') + '/' + $relativeUrl.TrimStart('/')
        Headers     = $Headers
        Method      = $method
        Body        = $jsonBody
        ContentType = 'application/json'
    }
    Write-Output "URL: $($params.Uri)"
    return Invoke-RestMethod @params -SkipCertificateCheck
}    

function Add-ApiConnection {
    $name = Read-Host 'API Connection Name'
    $url = Read-Host 'API Endpoint Url'
    $user = Read-Host 'API User'
    $key = Read-Host "API Key" -AsSecureString
    $enc = $key | ConvertFrom-SecureString 
    $apiConnection = @{
        Name = $name
        ApiEndpointUrl = $url
        ApiUser = $user
        ApiKey = $enc
    }
    $json = ConvertTo-Json $apiConnection
    $json | Out-File "$name.dat"
}

function Get-ApiConnection {
    param (
        $name
        )
    
        if (!(Test-Path $name)) {
           $fileName = "$name.dat"
        } else {
            $fileName = $name
        }

        if (!(Test-Path $fileName)) {
            throw "Local Endpoint data file does not exist for the given connection name: $fileName"
        }
    $connectionData = Get-Content $fileName | ConvertFrom-Json
    $url = $connectionData.psobject.Properties['ApiEndpointUrl'].Value
    $user = $connectionData.psobject.Properties['ApiUser'].Value
    $enc = $connectionData.psobject.Properties['ApiKey'].Value
    $key = $enc | ConvertTo-SecureString

    $connection = @{
        Name = $name
        ApiEndpointUrl = $url
        ApiUser = $user
        ApiKey = $key
    }
    return $connection
}
# SIG # Begin signature block
# MIIOCwYJKoZIhvcNAQcCoIIN/DCCDfgCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUjuMCWd2Vmu/ujpe88IWiI+O7
# 0l+gggtCMIIFWjCCBEKgAwIBAgIQDO0lEK+T1uQiVg3lGt++5DANBgkqhkiG9w0B
# AQsFADB9MQswCQYDVQQGEwJHQjEbMBkGA1UECBMSR3JlYXRlciBNYW5jaGVzdGVy
# MRAwDgYDVQQHEwdTYWx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