-
Notifications
You must be signed in to change notification settings - Fork 1
/
New-DCNMObject.ps1
68 lines (64 loc) · 2.24 KB
/
New-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
64
65
66
67
68
function New-DCNMObject {
<#
.SYNOPSIS
Post a new object to the REST API
.DESCRIPTION
This cmdlet will invoke a REST post against the DCNM API containing custom data
.EXAMPLE
$uri = https://dcnm.dcloud.cisco.com/rest/interface
New-DCNMObject -uri $uri -object ($body | ConvertTo-Json)
.PARAMETER uri
Resource location
.PARAMETER object
JSON data
.PARAMETER AuthAccessToken
Session Authentication Access Token
/#>
param
(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]$uri,
[Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true)]
[string]$object,
[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/json" ; 'Accept' = "application/json"}
try {
if ($body) {
if ($IsCore -eq $true) {
$response = Invoke-RestMethod -SkipCertificateCheck -Method Post -Uri $uri -Headers $headers -Body $object
} else { $response = Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Body $object -UseBasicParsing}
} else {
if ($IsCore -eq $true) {
$response = Invoke-RestMethod -SkipCertificateCheck -Method Post -Uri $uri -Headers $headers
} else { $response = Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -UseBasicParsing}
}
} catch {
Write-Host $_.Exception.Message -ForegroundColor Yellow
Write-Host $_.ErrorDetails.Message -ForegroundColor Yellow
}
}
End {
$Global:DCNM_RESPONSE = $response ; $response
}
}