Files
ps.ipam/functions/public/New-Subnet.ps1
2022-12-17 14:36:41 +03:00

264 lines
7.5 KiB
PowerShell

function New-Subnet {
<#
.SYNOPSIS
Creates new subnetwork object.
.PARAMETER CIDR
CIDR of subnet in dotted format (e.g 10.10.10.0/24).
.PARAMETER SectionId
Section identifier.
.PARAMETER Description
Subnet description.
.PARAMETER VlanId
Assigns subnet to VLAN.
.PARAMETER VrfId
Assigns subnet to VRF.
.PARAMETER MasterSubnetId
Master subnet id for nested subnet.
.PARAMETER NameserverId
Id of nameserver to attach to subnet.
.PARAMETER ShowName
Controls weather subnet is displayed as IP address or Name in subnets menu.
.PARAMETER DNSRecursive
Controls if PTR records should be created for subnet.
.PARAMETER DNSRecords
Controls weather hostname DNS records are displayed.
.PARAMETER AllowRequests
Controls if IP requests are allowed for subnet.
.PARAMETER ScanAgentId
Controls which scanagent to use for subnet (default id 1).
.PARAMETER DiscoverSubnet
Controls if new hosts should be discovered for new host scans.
.PARAMETER IsFull
Marks subnet as used.
.PARAMETER TagId
Assignes state (tag) to subnet (default: 1 Used).
.PARAMETER Threshold
Subnet threshold.
.PARAMETER LocationId
Location index.
#>
[OutputType([PS.IPAM.Subnetwork])]
[CmdletBinding()]
param (
[parameter(
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0
)]
[ValidateScript({[ipaddress] $_.Split("/")[0] -and $_.Split("/")[1] -match "\d{1,2}"})]
[ValidateNotNullOrEmpty()]
[string]
$CIDR,
[parameter(
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=1
)]
[ValidateNotNullOrEmpty()]
[int]
$SectionId,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=2
)]
[ValidateNotNullOrEmpty()]
[string]
$Description,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=3
)]
[ValidateNotNullOrEmpty()]
[int]
$VlanId,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=4
)]
[ValidateNotNullOrEmpty()]
[int]
$VrfId,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=5
)]
[ValidateNotNullOrEmpty()]
[int]
$MasterSubnetId,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=6
)]
[ValidateNotNullOrEmpty()]
[int]
$NameserverId,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=7
)]
[switch]
$ShowName,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=8
)]
[switch]
$DNSRecursive,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=9
)]
[switch]
$DNSRecords,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=10
)]
[switch]
$AllowRequests,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=11
)]
[ValidateNotNullOrEmpty()]
[int]
$ScanAgentId,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=12
)]
[switch]
$DiscoverSubnet,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=12
)]
[switch]
$IsFull,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=12
)]
[ValidateNotNullOrEmpty()]
[int]
$TagId,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=13
)]
[ValidateScript({ $_ -le 100 -and $_ -ge 1 })]
[ValidateNotNullOrEmpty()]
[int]
$Threshold,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=14
)]
[ValidateNotNullOrEmpty()]
[int]
$LocationId,
[parameter(
Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=15
)]
[ValidateScript({ $_ -is [Hashtable] -or $_ -is [PSCustomObject] })]
[ValidateNotNullOrEmpty()]
$CustomFields
)
process {
$_params = @{
Controller = [PS.IPAM.controllers]::subnets
Method = "POST"
}
$_body = @{
subnet = $CIDR.Split('/')[0]
mask = $CIDR.Split('/')[1]
sectionId = $SectionId
}
if ($Description) { $_body.Add("description", $Description) }
if ($VlanId) { $_body.Add("vlanId", $VlanId) }
if ($VrfId) { $_body.Add("vrfId", $VrfId) }
if ($MasterSubnetId) { $_body.Add("masterSubnetId", $MasterSubnetId) }
if ($NameserverId) { $_body.Add("nameserverId", $NameserverId) }
if ($ShowName) { $_body.Add("showName", "1") }
if ($DNSRecursive) { $_body.Add("DNSrecursive", "1") }
if ($DNSRecords) { $_body.Add("DNSrecords", "1") }
if ($AllowRequests) { $_body.Add("allowRequests", "1") }
if ($ScanAgentId) { $_body.Add("scanAgent", $ScanAgentId) }
if ($DiscoverSubnet) { $_body.Add("discoverSubnet", "1") }
if ($IsFull) { $_body.Add("isFull", "1") }
if ($TagId) { $_body.Add("state", $TagId) }
if ($Threshold) { $_body.Add("threshold", $Threshold) }
if ($Location) { $_body.Add("location", $Location) }
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) {
return Get-Subnet -CIDR $_result
}
}
}
Export-ModuleMember -Function New-Subnet