Are you interested in our Early Access Program (EAP)? This program allows you to preview code, test in your lab and provide feedback prior to General Availability (GA) release of all Infoblox products. If so, please click the link here.

API & Integration, DevOps,NetOps,SecOps

Reply

Creating a hostrecord using powershell

[ Edited ]
Member
Posts: 2
741     0

Basically I am stuck at creating a next available host record via powershell.

 

This is how my script looks like:

 

if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
{
$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
}
[ServerCertificateValidationCallback]::Ignore()

##Authentication
$pwd = ConvertTo-SecureString "infoblox" -AsPlainText -Force
$creds = New-Object Management.Automation.PSCredential ('admin', $pwd)

##API variables
$Scope = ""
$Scope = "172.16.1.0&ip_address%3C=172.16.1.254"
$Allnetwork_ref = Invoke-RestMethod "https://192.168.80.5/wapi/v2.11.3/ipv4address?ip_address%3E=$Scope&_return_as_object=1" -Credential $creds -Method get | ConvertTo-Json
$Allnetwork_refObject = ConvertFrom-Json -InputObject $Allnetwork_ref
$Networkref = $Allnetwork_refObject.result | where {$_.status -eq 'Unused'} | Select-Object -First 1
$Networkref.ip_address
$Networkref._ref

 

---end first part - 

 

------ begin second part POST -------


$reservation_details = @{
ipv4addr = "$Networkref.ip_address"
name="test2.thusi.local"}

$body = $reservation_details | ConvertTo-Json
Invoke-RestMethod -Uri $uri -Method POST -Credential $creds -ContentType 'application/json' -Body $body

 

What I want to achieve is to create the next available address within the scope. I have tried many ways, but couldn't be able to get it working.

 

Thank you guys in advance.

Re: Creating a hostrecord using powershell

Member
Posts: 2
742     0

Never mind guys;

 

I have found it and gave some extra boost to the default script. 

 

Success guys with developing it further.

 

function Ignore-SelfSignedCerts
{
try
{
Write-Host "Adding TrustAllCertsPolicy type." -ForegroundColor White
Add-Type -TypeDefinition @"
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;
}
}
"@
Write-Host "TrustAllCertsPolicy type added." -ForegroundColor White
}
catch
{
Write-Host $_ -ForegroundColor "Yellow"
}
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
} Ignore-SelfSignedCerts

$username = read-host "Please enter your name:"
$password = read-host "Enter a Password:" -assecurestring
$credential = New-Object System.Management.Automation.PSCredential($username,$password)


##API variables
$Scope = ""
$Scope = "172.16.1.0&ip_address%3C=172.16.1.254"
$Allnetwork_ref = Invoke-RestMethod "https://i1.1.1.1/wapi/v2.6.1/ipv4address?ip_address%3E=$Scope&_return_as_object=1" -Credential $credential -Method get | ConvertTo-Json
$Allnetwork_refObject = ConvertFrom-Json -InputObject $Allnetwork_ref
$Networkref = $Allnetwork_refObject.result | where {$_.status -eq 'Unused'} | Select-Object -First 1
$Networkref.ip_address
$Networkref._ref

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Enter" -or $_.KeyCode -eq "Escape"){
$objForm.Close()
}
})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,10)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Create Host record:"
$objForm.Controls.Add($objLabel)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,30)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Please enter the FQDN below:"
$objForm.Controls.Add($objLabel)

$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,50)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)

$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(10,70)
$objLabel2.Size = New-Object System.Drawing.Size(280,20)
$objLabel2.Text = "Please enter the IP Address below:"
$objForm.Controls.Add($objLabel2)

$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(10,90)
$objTextBox2.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox2)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void]$objForm.ShowDialog()

$objTextBox.Text
$objTextBox2.Text

$host_details = @{
name= $objTextBox.Text
ipv4addrs=@(@{ipv4addr= $objTextBox2.Text})}

$url = "https://1.1.1.1/wapi/v2.6.1/record:host?_return_as_object=1"

$body = $host_details | ConvertTo-Json

Invoke-RestMethod -Uri $url -Method POST -Credential $credential -ContentType 'application/json' -Body $body

Re: Creating a hostrecord using powershell

Adviser
Posts: 179
742     0

Hi,

 

Have you tried the WAPI call to automatically get the next available IP while creating the host record like this:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic auth")
$headers.Add("Content-Type", "application/json")

$body = "{
`n	`"name`":`"wapi.info.com`",
`n	`"ipv4addrs`":
`n	[
`n		{
`n			`"ipv4addr`":
`n			{
`n				`"_object_function`":`"next_available_ip`",
`n				`"_parameters`":
`n				{
`n					`"exclude`":[`"10.10.10.1`",`"10.10.10.2`"]
`n				},
`n				`"_result_field`":`"ips`",
`n				`"_object`" : `"network`",
`n				`"_object_parameters`":
`n				{
`n					`"network`":`"10.10.10.0/24`"
`n				}
`n			}
`n		}
`n	]
`n}"

$response = Invoke-RestMethod 'https://grid-master/wapi/v2.11.3/record:host?_return_fields%2B=name,ipv4addrs' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

Regards,

Krishna Vasudevan

Showing results for 
Search instead for 
Did you mean: 

Recommended for You