Files
ps.ipam/functions/public/New-Address.ps1

235 lines
6.5 KiB
PowerShell

function New-Address {
<#
.SYNOPSIS
Creates address object.
.PARAMETER SubnetId
Id of subnet address belongs to.
.PARAMETER Ip
IP address.
.PARAMETER Gateway
Defines if address is presented as gateway.
.PARAMETER Description
Address description.
.PARAMETER Hostname
Address hostname.
.PARAMETER MAC
Mac address.
.PARAMETER Owner
Address owner.
.PARAMETER TagId
Id of subnet address belongs to.
.PARAMETER PTRIgnore
Controls if PTR should not be created.
.PARAMETER PTRId
Id of PowerDNS PTR record.
.PARAMETER Note
Note.
.PARAMETER ExcludePing
Exclude this address from status update scans (ping).
.PARAMETER DeviceId
Id of device address belongs to.
.PARAMETER Port
Port.
#>
[OutputType([PS.IPAM.address])]
[CmdletBinding(DefaultParameterSetName="BySubnetId")]
param (
[parameter(
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0,
ParameterSetName="BySubnetId"
)]
[ValidateNotNullOrEmpty()]
[int]
$SubnetId,
[Parameter(
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0,
ParameterSetName="BySubnetObject"
)]
[ValidateNotNullOrEmpty()]
[ps.ipam.subnetwork]
$SubnetObject,
[parameter(
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=1)]
[ValidateScript({[ipaddress] $_ })]
[ValidateNotNullOrEmpty()]
[string]
$Ip,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=2)]
[switch]
$Gateway,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=3)]
[ValidateNotNullOrEmpty()]
[string]
$Description,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=4)]
[ValidateNotNullOrEmpty()]
[string]
$Hostname,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=5)]
[ValidateScript({ $_.Replace(":","") -match "^$('([A-F0-9]{2})' * 6)$" })]
[ValidateNotNullOrEmpty()]
[string]
$MAC,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=6)]
[ValidateNotNullOrEmpty()]
[string]
$Owner,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=7)]
[ValidateNotNullOrEmpty()]
[int]
$TagId,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=8)]
[switch]
$PTRIgnore,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=7)]
[ValidateNotNullOrEmpty()]
[int]
$PTRId,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=10)]
[ValidateNotNullOrEmpty()]
[string]
$Note,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=11)]
[switch]
$ExcludePing,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=12)]
[ValidateNotNullOrEmpty()]
[int]
$DeviceId,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=13)]
[ValidateNotNullOrEmpty()]
[string]
$Port,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=14
)]
[ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })]
$CustomFields
)
process {
$_params = @{
Controller = [PS.IPAM.controllers]::addresses
Method = "POST"
}
switch ($PSCmdlet.ParameterSetName) {
"BySubnetId" {
$_subnetId = $SubnetId
break
}
"BySubnetObject" {
$_subnetId = $SubnetObject.id
break
}
}
$_body = @{
subnetId = $_subnetId
ip = $Ip
}
if ($Gateway) { $_body.Add("is_gateway", "1") }
if ($Description) { $_body.Add("description", $Description) }
if ($Hostname) { $_body.Add("hostname", $Hostname) }
if ($MAC) { $_body.Add("mac", $MAC) }
if ($Owner) { $_body.Add("owner", $Owner) }
if ($TagId) { $_body.Add("tag", $TagId) }
if ($PTRIgnore) { $_body.Add("PTRignore", "1") }
if ($PTRId) { $_body.add("PTR", $PTRId)}
if ($Note) { $_body.Add("note", $Note) }
if ($ExcludePing) { $_body.Add("excludePing", "1") }
if ($DeviceId) { $_body.Add("deviceId", $DeviceId) }
if ($Port) { $_body.Add("port", $Port) }
if ($CustomFields) {
if ($CustomFields -is [PSCustomObject]) {
$_customFields = ConvertTo-Hashtable -InputObject $CustomFields
} else { $_customFields = $CustomFields }
$_body = $_body + $_customFields
}
$_params.Add("Params",$_body)
$_result = Invoke-Request @_params
if ($_result) {
Get-Address -SubnetId $SubnetId -Ip $Ip
}
}
}
Export-ModuleMember -Function New-Address