PoshCode Archive  Artifact [dea0fa1112]

Artifact dea0fa11123f97343047201a0543ead247bf151ebf3b0f9fb8ba3398dd310260:

  • File Infoblox-Module.ps1 — part of check-in [04b203845b] at 2018-06-10 13:49:14 on branch trunk — I’ve just started to work on a PowerShell module for the Infoblox Trinzic DDI appliance, this is a VERY early release with some functions that can manage DNS-records. (user: DollarUnderscore size: 13939)

# encoding: ascii
# api: powershell
# title: Infoblox Module
# description: I’ve just started to work on a PowerShell module for the Infoblox Trinzic DDI appliance, this is a VERY early release with some functions that can manage DNS-records.
# version: 1.2.1/record
# type: function
# author: DollarUnderscore
# license: CC0
# function: Get-IBResourceRecord
# x-poshcode-id: 5044
# x-archived: 2016-02-21T01:34:59
# x-published: 2016-04-02T20:58:00
#
# I would love some help with developing a proper module for this product which seems to be quite common.
# Do you want to help?
# Go to my blog at:
# http://dollarunderscore.azurewebsites.net/?p=2201
# ...and you will find some information about this module and how to contribute!
# Thanks in advance!
#
function Get-IBResourceRecord
{

    <#
    .SYNOPSIS
    Retrieves resource records from a Infoblox Gridserver

    .DESCRIPTION
    Specify an attribute to search for, for example hostname and retrieve the object from the Gridserver

    .EXAMPLE
    Get-InfoBloxRecord -RecordType host -RecordName MyServer -GridServer myinfoblox.mydomain.com -Credential $Credential

    .EXAMPLE
    Get-InfoBloxRecord -RecordType network -RecordName 1.0.0.0/8 -GridServer myinfoblox.mydomain.com -Credential $Credential -Passthrough

    .PARAMETER RecordType
    Specify the type of record, for example host or network.

    .PARAMETER SearchField
    The field where the RecordValue is. Default is "Name".

    .PARAMETER RecordValue
    The value to search for.

    .PARAMETER GridServer
    The name of the infoblox appliance.

    .PARAMETER Properties
    What properties should be included?

    .PARAMETER Credential
    Add a Powershell credential object (created with for example Get-Credential).

    .PARAMETER Passthrough
    Includes credentials and gridserver in the object sent down the pipeline so you don't need to add them in the next cmdlet.

    #>

    [CmdletBinding()]

    param(
    [Parameter(Mandatory=$True)]
    [ValidateSet("A","AAAA","CName","DName","DNSKEY","DS","Host","LBDN","MX","NAPTR","NS","NSEC","NSEC3","NSEC3PARAM","PTR","RRSIG","SRV","TXT")]
    [string] $RecordType,
    [Parameter(Mandatory=$false)]
    $SearchField = 'name',
    [Parameter(Mandatory=$True)]
    $RecordValue,
    [Parameter(Mandatory=$True)]
    $GridServer,
    [Parameter(Mandatory=$false)]
    $Properties,
    [switch] $Passthrough,
    [Parameter(Mandatory=$True)]
    $Credential)

    BEGIN { }

    PROCESS {

        Write-Verbose "Building resource record search query..."
        $InfobloxURI = "https://$GridServer/wapi/v1.2.1/record:$($RecordType.ToLower())`?$($SearchField.ToLower())~=$RecordValue"

        if ($Properties -ne $null) {
            Write-Verbose "Adding return fields/properties..."
            $InfobloxURI = $InfobloxURI + "&_return_fields=$(($Properties -join "," -replace " ").ToLower())"
        }

        Write-Verbose "Initiating webrequest to API..."

        $WebRequest = Invoke-WebRequest -Uri $InfobloxURI -Credential $Credential

        Write-Verbose "Checking status code..."

        if ($WebRequest.StatusCode -eq 200) {
            Write-Verbose "Statuscode OK. Converting from Json..."
            $RecordObj = $WebRequest.Content | ConvertFrom-Json -ErrorAction Stop
        }
        else {
            Write-Error "Request to Infoblox failed (response code is not 200). See error above for details."
            Write-Debug "Request just failed (response not 200 or Json version failed). Please debug."
            return
        }


        Write-Verbose "Looping through returned objects..."

        foreach ($Record in $RecordObj) {

            $returnObject = $null
            $returnObject = $Record

            if ($Passthrough -eq $true) {
                Write-Verbose "Adding credentials/gridserver to the pipeline..."
                $returnObject | Add-Member -Type NoteProperty -Name Credential -Value $Credential
                $returnObject | Add-Member -Type NoteProperty -Name GridServer -Value $GridServer
            }

            Write-Verbose "Sending object to the pipeline..."
            Write-Output $returnObject
        }
    }

    END {
        Write-Verbose "Finished."
    }

}

function Add-IBResourceRecordHost
{

    <#
    .SYNOPSIS
    Add a host record on the Infoblox Gridserver

    .DESCRIPTION
    This cmdlet creates a host object on the Infoblox Gridserver.

    .EXAMPLE
    Add-InfoBloxHostRecord -IPv4Address 1.2.3.4 -HostName myserver.mydomain.com -GridServer myinfoblox.mydomain.com -Credential $Credential

    .PARAMETER IPv4Address
    The IPv4 address for the host record. Allows pipeline input.

    .PARAMETER HostName
    The hostname for the host record. Allows pipeline input.

    .PARAMETER GridServer
    The name of the infoblox appliance. Allows pipeline input.

    .PARAMETER Credential
    Add a Powershell credential object (created with for example Get-Credential). Allows pipeline input.

    #>

    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    $IPv4Address,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('name')]
    $HostName,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [string] $GridServer,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [System.Management.Automation.PSCredential] $Credential)

    BEGIN { }

    PROCESS {

        $InfobloxURI = "https://$GridServer/wapi/v1.2.1/record:host"

        $Data = "{`"ipv4addrs`":[{`"ipv4addr`":'$IPv4Address'}],`"name`":'$HostName'}" | ConvertFrom-Json | ConvertTo-Json

        $WebReqeust = Invoke-WebRequest -Uri $InfobloxURI -Method Post -Body $Data -ContentType "application/json" -Credential $Credential

        if ($WebReqeust.StatusCode -ne 201) {
            Write-Error "Request to Infoblox failed for record $HostName!"
            return
        }
    }

    END { }
}


function Add-IBResourceRecordCName
{

    <#
    .SYNOPSIS
    Adds a CNAME record on the Infoblox Gridserver

    .DESCRIPTION
    This cmdlet creates a CName record on the Infoblox Gridserver.

    .EXAMPLE
    Add-IBResourceRecordCName -IPv4Address 1.2.3.4 -HostName myserver.mydomain.com -GridServer myinfoblox.mydomain.com -Credential $Credential

    .PARAMETER IPv4Address
    The IPv4 address for the host record. Allows pipeline input.

    .PARAMETER HostName
    The hostname for the host record. Allows pipeline input.

    .PARAMETER GridServer
    The name of the infoblox appliance. Allows pipeline input.

    .PARAMETER Credential
    Add a Powershell credential object (created with for example Get-Credential). Allows pipeline input.

    #>

    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('name')]
    $HostName,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    $Canonical,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [string] $GridServer,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [System.Management.Automation.PSCredential] $Credential)

    BEGIN { }

    PROCESS {

        $InfobloxURI = "https://$GridServer/wapi/v1.2.1/record:cname"

        $Data = "{`"name`":'$HostName',`"canonical`":'$Canonical'}" | ConvertFrom-Json | ConvertTo-Json

        $WebReqeust = Invoke-WebRequest -Uri $InfobloxURI -Method Post -Body $Data -ContentType "application/json" -Credential $Credential

        if ($WebReqeust.StatusCode -ne 201) {
            Write-Error "Request to Infoblox failed for record $HostName!"
            return
        }
    }

    END { }
}

function Add-IBResourceRecordA
{

    <#
    .SYNOPSIS
    Adds a A record on the Infoblox Gridserver

    .DESCRIPTION
    This cmdlet creates a CName record on the Infoblox Gridserver.

    .EXAMPLE
    Add-IBResourceRecordCName -IPv4Address 1.2.3.4 -HostName myserver.mydomain.com -GridServer myinfoblox.mydomain.com -Credential $Credential

    .PARAMETER IPv4Address
    The IPv4 address for the host record. Allows pipeline input.

    .PARAMETER HostName
    The hostname for the host record. Allows pipeline input.

    .PARAMETER GridServer
    The name of the infoblox appliance. Allows pipeline input.

    .PARAMETER Credential
    Add a Powershell credential object (created with for example Get-Credential). Allows pipeline input.

    #>

    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    $IPv4Address,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('name')]
    $HostName,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [string] $GridServer,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [System.Management.Automation.PSCredential] $Credential)

    BEGIN { }

    PROCESS {

        $InfobloxURI = "https://$GridServer/wapi/v1.2.1/record:a"

        $Data = "{`"ipv4addr`":'$IPv4Address',`"name`":'$HostName'}" | ConvertFrom-Json | ConvertTo-Json

        $WebReqeust = Invoke-WebRequest -Uri $InfobloxURI -Method Post -Body $Data -ContentType "application/json" -Credential $Credential

        if ($WebReqeust.StatusCode -ne 201) {
            Write-Error "Request to Infoblox failed for record $HostName!"
            return
        }
    }

    END { }
}

function Remove-IBResourceRecord
{

    <#
    .SYNOPSIS
    Removes a host record from the Infoblox Gridserver

    .DESCRIPTION
    This cmdlet removes a host object from the Infoblox Gridserver.

    .EXAMPLE
    Get-InfoBloxRecord -RecordType host -RecordName MyHost -GridServer myinfoblox.mydomain.com -Credential $Credential -Passthrough | Remove-InfoBloxRecord

    .PARAMETER Reference
    The object reference for the host record. Allows pipeline input.

    .PARAMETER GridServer
    The name of the infoblox appliance. Allows pipeline input.

    .PARAMETER Credential
    Add a Powershell credential object (created with for example Get-Credential). Allows pipeline input.

    #>

    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('_ref')]
    [string] $Reference,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [string] $GridServer,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [System.Management.Automation.PSCredential] $Credential)

    BEGIN { }

    PROCESS {
        $InfobloxURI = "https://$GridServer/wapi/v1.2.1/$Reference"

        $WebReqeust = Invoke-WebRequest -Uri $InfobloxURI -Method Delete -Credential $Credential

        if ($WebReqeust.StatusCode -eq 200) {
            $RecordObj = $WebReqeust.Content | ConvertFrom-Json
        }
        else {
            Write-Error "Request to Infoblox failed!"
            return
        }
    }

    END { }

}


function Set-IBResourceRecord
{

    <#
    .SYNOPSIS
    Changes a host record on the Infoblox Gridserver

    .DESCRIPTION
    This cmdlet changes a host object on the Infoblox Gridserver.

    .EXAMPLE
    Set-InfoBloxRecord -Reference Reference -IPv4Address 1.2.3.4 -HostName myhost.mydomain.com -GridServer myinfoblox.mydomain.com -Credential $Credential

    .EXAMPLE
    Get-InfoBloxRecord -RecordType host -RecordName MyHost -GridServer myinfoblox.mydomain.com -Credential $Credential -Passthrough | Set-InfoBloxRecord -IPv4Address 2.3.4.5

    .PARAMETER IPv4Address
    The new IPv4 address for the host record.

    .PARAMETER HostName
    The new HostName for the host record.

    .PARAMETER Reference
    The object reference for the host record. Allows pipeline input.

    .PARAMETER GridServer
    The name of the infoblox appliance. Allows pipeline input.

    .PARAMETER Credential
    Add a Powershell credential object (created with for example Get-Credential). Allows pipeline input.

    #>

    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('_ref')]
    [string] $Reference,
    [Parameter(Mandatory=$True)]
    [string] $IPv4Address,
    [Parameter(Mandatory=$False)]
    [string] $HostName,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [string] $GridServer,
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [System.Management.Automation.PSCredential] $Credential)

    BEGIN { }

    PROCESS {
        $InfobloxURI = "https://$GridServer/wapi/v1.2.1/$Reference"

        $Data = "{`"ipv4addrs`":[{`"ipv4addr`":'$IPv4Address'}] }" | ConvertFrom-Json | ConvertTo-Json

        $WebReqeust = Invoke-WebRequest -Uri $InfobloxURI -Method Put -Body $Data -Credential $Credential

        if ($WebReqeust.StatusCode -eq 200) {
            $RecordObj = $WebReqeust.Content | ConvertFrom-Json
        }
        else {
            Write-Error "Request to Infoblox failed!"
            return
        }
    }

    END { }

}


Export-ModuleMember *