THE GAME HAS CHANGED

Introducing Infoblox Universal DDI ManagementTM

Watch the launch to discover the new era of management for critical network services. Watch Now

API & Integration, DevOps,NetOps,SecOps

Reply

Creating a hostrecord using powershell

[ Edited ]
New Member
Posts: 2
11009     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

New Member
Posts: 2
11009     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: 181
11009     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

Re: Creating a hostrecord using powershell

New Member
Posts: 4
11010     0

I'm trying to pull the next available IP with creating a reservation or record. Is there a way to achieve this? I'd like to validate that the IP is not in use by doing a ping and nslookup first before creating the reservation.

Re: Creating a hostrecord using powershell

New Member
Posts: 4
11010     0

Typo... I meant to write "pull the IP without creating the reservation"

Re: Creating a hostrecord using powershell

Moderator
Moderator
Posts: 315
11010     0

Yes you can do this by using the next_available_ip function against the parent network.

Re: Creating a hostrecord using powershell

New Member
Posts: 1
11010     0

I'm trying to run this code that @kvasudevan posted, but i keep getting an error back saying

"The action is not allowed. A parent was not found." 
We only use Infoblox at this point for IPAM and not for DNS or DHCP.  I tried to add configure_for_dns:false to the request, but no dice.
Any help is greatly appreciated.
 
Thanks

Re: Creating a hostrecord using powershell

Superuser
Posts: 65
11010     0

This line indicates a DNS record:

`n	`"name`":`"wapi.info.com`",

 Try a name that is not an fqdn, for example:

`n	`"name`":`"wapi`",

Re: Creating a hostrecord using powershell

New Member
Posts: 3
11010     0

Trying to get this to work with my powershell. 

 

I am able to get the IP address that I want to create a record for however my Post request never works to create the record.

 

$host_details = @{
name = $servername
ipv4addrs=@(@{ipv4addr= $Networkref.ip_address})}

$url = "https://1.1.1.1/wapi/v2.11.3/record:host?_return_fields%2B=name,ipv4addrs"
$body = $host_details | ConvertTo-Json

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

What am I missing?

Re: Creating a hostrecord using powershell

[ Edited ]
Superuser
Posts: 65
11010     0

Assuming $servername and $Networkref.ip_address hold appropriate values, this should work for you:

$host_details = @"
{
	`"name`": `"$servername`",
	`"ipv4addrs`":
		[{
			`"ipv4addr`": `"$Networkref.ip_address`"
		}]
}
"@

$url = "https://1.1.1.1/wapi/v2.11.3/record:host?_return_fields%2B=name,ipv4addrs"

Invoke-RestMethod -Uri $url -Method POST -Credential $IBCreds -ContentType 'application/json' -Body $host_details 

 

Re: Creating a hostrecord using powershell

New Member
Posts: 3
11010     0

Well getting a different error now. I know my user account has rights to create.

 

Invoke-RestMethod : The remote server returned an error: (403) Forbidden.

Re: Creating a hostrecord using powershell

New Member
Posts: 3
11010     0

Does it matter if we have a required zone?

Re: Creating a hostrecord using powershell

Superuser
Posts: 65
11010     0

The sample call posted earlier runs fine on my Grid. Are you certain the values in your variables for name and IP address are correct and for objects you have write access to?

Re: Creating a hostrecord using powershell

New Member
Posts: 1
11010     0

tried to run this:

 

$host_details = @"
{
`"name`": `"$servername`",
`"ipv4addrs`":
[{
`"ipv4addr`": `"$SetIP`"
}]
}
"@
$url = "https://x.x.x.x/wapi/v2.11.3/record:host?_return_fields%2B=name,ipv4addrs"
Invoke-RestMethod -Uri $url -Method POST -ContentType 'application/json' -Body $host_details -Credential $credential3
 
Getting Error:
Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
 
Please help in this...

Re: Creating a hostrecord using powershell

New Member
Posts: 1
11010     0
                        $host_details = @{
                        name="$HostName"
                        ipv4addrs=@(@{ipv4addr="$IPAddress"})
                        ddns_protected=$true
                        comment="$Ticket"}
                        $body = $host_details| ConvertTo-Json
                        $getDNSRec = Invoke-RestMethod -Uri $url -Method POST -Credential $cred -ContentType 'application/json' -Body $body -UseBasicParsing
                        
Showing results for 
Search instead for 
Did you mean: 

Recommended for You