-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSInfoblox.psm1
269 lines (218 loc) · 7.24 KB
/
PSInfoblox.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<#
.NOTES
--------------------------------------------------------------------------------
Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.138
Generated on: 10/04/2017 18:23
Generated by: vmoitrot
--------------------------------------------------------------------------------
.DESCRIPTION
Script generated by PowerShell Studio 2017
#>
<#
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.138
Created on: 10/04/2017 17:57
Created by: vmoitrot
Organization:
Filename: PSInfoblox.psm1
-------------------------------------------------------------------------
Module Name: PSInfoblox
===========================================================================
#>
function Get-IBNextFreeIPs
{
<#
.SYNOPSIS
Return X free ips in a network
.DESCRIPTION
Return X free ips in a network, 5 by default
.PARAMETER APIusername
API Username
.PARAMETER APIuserpassword
API Password
.PARAMETER IBurl
Url to infoblox appliance
.PARAMETER IBapiversion
API version
.PARAMETER IPamount
How many IPs should be returned
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
[OutputType([array])]
param
(
[Parameter(Mandatory = $true,
Position = 1)]
[string]$APIusername,
[Parameter(Mandatory = $true,
Position = 2)]
[string]$APIuserpassword,
[Parameter(Mandatory = $true,
HelpMessage = 'Url to your infoblox appliance like https://myinfoblox.company.com')]
[string]$IBurl,
[Parameter(HelpMessage = 'API version like 1.6. Default is 2.0.')]
[string]$IBapiversion = "2.0",
[Parameter(Position = 3)]
[uint16]$IPamount = 5
)
DynamicParam
{
$IBapiurl = $IBurl.ToString() + "/wapi/v" + $IBapiversion
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $IBapiuser, $IBapipwd)))
$Headers = @{ Authorization = ("Basic {0}" -f $base64AuthInfo) }
# Set the dynamic parameters' name
$ParameterName = 'network'
# Create the dictionary
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Create the collection of attributes
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Create and set the parameters' attributes
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $true
$ParameterAttribute.Position = 1
# Add the attributes to the attributes collection
$AttributeCollection.Add($ParameterAttribute)
# Generate and set the ValidateSet
$IBapiurl = $IBurl + "/wapi/v" + $IBapiversion
$arrSet = (Invoke-RestMethod -Method Get -Uri "$IBapiurl/network" -Headers $Headers).network
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
# Add the ValidateSet to the attributes collection
$AttributeCollection.Add($ValidateSetAttribute)
# Create and return the dynamic parameter
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary
}
begin
{
# Bind the parameter to a friendly variable
$network = $PsBoundParameters[$ParameterName]
}
process
{
$IBapiurl = $IBurl + "/wapi/v" + $IBapiversion
$data = @{
"num" = $IPamount
}
$networkdetails = Invoke-RestMethod -Method Get -Uri "$IBapiurl/network?network=$network" -Headers $Headers
$networkref = $networkdetails._ref.replace("/default", "")
(Invoke-RestMethod -Method post -Uri "$IBapiurl/$($networkref)?_function=next_available_ip" -body $($data | ConvertTo-Json) -ContentType "application/json" -UseDefaultCredentials).ips
}
}
function New-IBRecord
{
<#
.SYNOPSIS
Create new record
.DESCRIPTION
Can create new host, cname and A records
.PARAMETER APIusername
API Username
.PARAMETER APIuserpassword
API Password
.PARAMETER recordtype
A description of the recordtype parameter.
.PARAMETER IBurl
Url to infoblox appliance
.PARAMETER IBapiversion
API version
.PARAMETER FQDN
Full Qualified Domain Name for the record
.PARAMETER ipaddress
IP Address for the record
.PARAMETER aliases
Aliases for the host record
.PARAMETER macaddress
Mac Address for the record
.PARAMETER dhcp
Enable DHCP lease on this record
.PARAMETER comment
Add comment on this record
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]$APIusername,
[Parameter(Mandatory = $true)]
[string]$APIuserpassword,
[Parameter(Mandatory = $true)]
[ValidateSet('A', 'HOST', 'FixedAddress')]
[string]$recordtype,
[Parameter(HelpMessage = 'Url to your infoblox appliance like https://myinfoblox.company.com')]
[uri]$IBurl,
[Parameter(HelpMessage = 'API version like 1.6. Default is 2.0.')]
[string]$IBapiversion = "2.0",
[Parameter(ParameterSetName = 'A',
Mandatory = $true)]
[Parameter(ParameterSetName = 'Host')]
[string]$FQDN,
[Parameter(ParameterSetName = 'FixedAddress',
Mandatory = $true)]
[Parameter(ParameterSetName = 'A')]
[Parameter(ParameterSetName = 'Host')]
[ipaddress]$ipaddress,
[Parameter(ParameterSetName = 'Host')]
[array]$aliases,
[Parameter(ParameterSetName = 'FixedAddress')]
[Parameter(ParameterSetName = 'Host')]
[string]$macaddress,
[Parameter(ParameterSetName = 'Host')]
[boolean]$dhcp,
[string]$comment
)
$IBapiurl = $IBurl.ToString() + "/wapi/v" + $IBapiversion
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $IBapiuser, $IBapipwd)))
$Headers = @{ Authorization = ("Basic {0}" -f $base64AuthInfo) }
$hostrequest = Invoke-RestMethod -Method Get -Uri "$IBapiurl/record:host?name=$FQDN" -Headers $Headers
if (!$hostrequest)
{
switch ($recordtype)
{
'A' {
$uri = "$IBapiurl/record:a"
$databody = @{
ipv4addr = $ipaddress.IPAddressToString
name = $FQDN
}
}
'FixedAddress' {
$uri = "$IBapiurl/fixedaddress"
$databody = @{
ipv4addr = $ipaddress.IPAddressToString
mac = $macaddress
}
}
'HOST' {
$uri = "$IBapiurl/record:host"
$ipv4addrs = @()
$ipv4addrs += @{
ipv4addr = $ipaddress.IPAddressToString
mac = $macaddress
configure_for_dhcp = $dhcp
}
$databody = @{
ipv4addrs = $ipv4addrs
name = $FQDN
}
if ($aliases)
{
$databody.Add("aliases", $aliases)
}
}
}
if ($comment)
{ $databody.Add('comment', $comment) }
Invoke-RestMethod -Method post -Uri $uri -body $( ,$databody | ConvertTo-Json -Depth 3) -ContentType "application/json" -Headers $Headers
}
else
{
Write-Error "$FQDN already exit"
Write-Error $($hostrequest | ft | Out-String)
}
}
Export-ModuleMember -Function New-IBRecord