-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-DCNMObject.ps1
63 lines (61 loc) · 1.99 KB
/
Get-DCNMObject.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
function Get-DCNMObject {
<#
.SYNOPSIS
Retrieves a new object to the REST API
.DESCRIPTION
This cmdlet will invoke a REST get against the DCNM API path
.EXAMPLE
$uri = https://dcnm.dcloud.cisco.com/rest/control/fabrics
Get-DCNMObject -uri $uri
.PARAMETER uri
Resource location
.PARAMETER AuthAccessToken
Session Authentication Access Token
/#>
param
(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]$uri,
[Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
[string]$AuthToken="$Global:DCNMAuthToken"
)
Begin {
if ($PSEdition -eq 'Core') {$IsCore=$true} else {
$IsCore=$false
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
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls12'
}
}
Process {
$headers = @{ 'dcnm-token' = "$AuthToken" ; 'content-type' = "application/x-www-form-urlencoded" ; 'cache-control' = "no-cache"}
#try {
if ($IsCore -eq $true) {
$response = Invoke-RestMethod -SkipCertificateCheck -Method Get -Uri $uri -Headers $headers
} else { $response = Invoke-RestMethod -Method Get -Uri $uri -Headers $headers }
#} catch {
# $message = $_.Exception.Message
# if ($message -eq 'Invalid URI: The hostname could not be parsed.') {
# Write-Host $message -ForegroundColor Yellow
# New-DCNMAuthToken
# }
# if ($message -eq 'Response status code does not indicate success: 401 (Unauthorized).') {
# Write-Host $message -ForegroundColor Yellow
# New-DCNMAuthToken -DCNMHost $env:DCNMHost
# }
#}
}
End {
$response
}
}