forked from RobinBeismann/PowerShell-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-NetboxQuery.ps1
More file actions
137 lines (121 loc) · 4.38 KB
/
Invoke-NetboxQuery.ps1
File metadata and controls
137 lines (121 loc) · 4.38 KB
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
$api_base_url = https://netbox.contoso.com/api/
$nbToken = ""
function Invoke-NetboxQuery {
param (
[parameter(Mandatory = $true)]
$Uri,
[parameter(Mandatory = $false)]
$Method = "GET",
[parameter(Mandatory = $false)]
$Body,
[parameter(Mandatory = $false)]
$ForceSSL = $true,
[parameter(Mandatory = $false)]
$ApiBaseUrl = $global:api_base_url,
[parameter(Mandatory = $false)]
$ApiToken = $global:nbToken
)
begin{
# Create an empty array to store the result.
$QueryResults = [System.Collections.ArrayList]@()
$PreviousProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
$Encoding = [System.Text.Encoding]::UTF8
}
process{
# Check if the URI is already a fully featured URI
if(
!([regex]::Match($uri,"(http|https):\/\/.*\/api\/",[Text.RegularExpressions.RegexOptions]'IgnoreCase, CultureInvariant').Success)
){
if($Uri.StartsWith("/")){
$uri = $Uri.Substring(1)
}
$Uri = $ApiBaseUrl + $Uri
}
# If ForceSSL is set, overwrite any next page urls with https
if($ForceSSL){
$uri = $uri.Replace("http://","https://")
}
# Format headers.
$HeaderParams = @{
'Content-Type' = "application/json"
'Authorization' = "Token $ApiToken"
}
# Invoke REST method and fetch data until there are no pages left.
do {
# Build Parameters
$Params = @{
UseBasicParsing = $true
Method = $Method
ContentType = "application/json"
Uri = $Uri
Headers = $HeaderParams
ErrorAction = "Stop"
}
if($Body){
$Params.Body = [System.Text.Encoding]::UTF8.GetBytes(($Body | ConvertTo-Json))
}
# Get Result
try{
[Microsoft.PowerShell.Commands.WebResponseObject]$RawResult = Invoke-WebRequest @Params
$EncodedResult = $Encoding.GetString(
$RawResult.RawContentStream.ToArray()
)
$Results = ConvertFrom-Json -InputObject $EncodedResult
}catch{
$ErrorDetails = @{
Parameters = $Params
RawBody = $Body
ExactError = $_.ErrorDetails
} | ConvertTo-Json
Write-Error -ErrorAction Stop -Message "Error at Rest Request: $($_.Exception.Message)`n`n Error Details:`n$($ErrorDetails)"
}
# Add results to table
if (
$Results.results
) {
$null = $QueryResults.AddRange($Results.results)
# Add single result to table
}elseif(
$Results -and
!(
$Results.PSObject.Properties.Name.Contains("results") -or
$Results.PSObject.Properties.Name.Contains("count")
)
){
$null = $QueryResults.Add($Results)
}
# Process next page
if($Results.'next'){
# If ForceSSL is set, overwrite any next page urls with https
if($ForceSSL){
$uri = $Results.'next'.Replace("http://","https://")
}else{
$uri = $Results.'next'
}
}else{
$uri = $null
}
# Break out of loop if we got no next page
} until (!($uri))
}
end{
# Switch Progress Preference back
$ProgressPreference = $PreviousProgressPreference
# Return the result.
$QueryResults
}
}
# Get Devices
Invoke-NetboxQuery -Uri "dcim/devices/"
# New Cluster
Invoke-NetboxQuery -Uri "virtualization/clusters/" -Method 'POST' -Body @{
name = $scClusterName
type = $clusterType
group = $clusterGroup
site = $site
}
# Update device cluster ID
Invoke-NetboxQuery -Uri "dcim/devices/$($nbDevice.id)/" -Method 'PATCH' -Body @{
cluster = ($nbCluster.id)
}