127 lines
5.4 KiB
PowerShell
127 lines
5.4 KiB
PowerShell
function Get-Subnet {
|
|
[CmdletBinding(DefaultParameterSetName="NoParams")]
|
|
[OutputType([PS.IPAM.Subnetwork])]
|
|
param (
|
|
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByCIDR")]
|
|
[ValidateScript({[ipaddress] $_.Split("/")[0] -and $_.Split("/")[1] -match "\d{1,2}"})]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$CIDR,
|
|
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")]
|
|
[ValidateScript({ $_ -match "^\d+$" })]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$Id,
|
|
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySectionId")]
|
|
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2,ParameterSetName="ByVlanNumber")]
|
|
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1,ParameterSetName="ByVlanId")]
|
|
[ValidateScript({ $_ -match "^\d+$" })]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$SectionId,
|
|
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySectionName")]
|
|
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=3,ParameterSetName="ByVlanNumber")]
|
|
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2,ParameterSetName="ByVlanId")]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$SectionName,
|
|
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByVrfId")]
|
|
[ValidateScript({ $_ -match "^\d+$" })]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$VrfId,
|
|
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByVlanId")]
|
|
[ValidateScript({ $_ -match "^\d+$" })]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$VlanId,
|
|
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByVlanNumber")]
|
|
[ValidateScript({ $_ -match "^\d+$" })]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$VlanNumber,
|
|
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1,ParameterSetName="ByVlanNumber")]
|
|
[ValidateScript({ $_ -match "^\d+$" })]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$VlanDomain,
|
|
[parameter(Mandatory=$false,ParameterSetName="ByID")]
|
|
[switch]
|
|
$Slaves,
|
|
[parameter(Mandatory=$false,ParameterSetName="ByID")]
|
|
[switch]
|
|
$Recurse
|
|
)
|
|
process {
|
|
$_params = @{
|
|
Controller = [PS.IPAM.controllers]::subnets
|
|
Method = [PS.IPAM.methods]::GET
|
|
Type = [PS.IPAM.types]::Subnetwork
|
|
}
|
|
switch ($PSCmdlet.ParameterSetName) {
|
|
"ByCIDR" {
|
|
$_identifiers = @("cidr",$CIDR)
|
|
}
|
|
"ByID" {
|
|
$_identifiers = @($Id)
|
|
|
|
if ($Slaves) {
|
|
if ($Recurse) {
|
|
$_identifiers += "slaves_recursive"
|
|
} else {
|
|
$_identifiers += "slaves"
|
|
}
|
|
}
|
|
}
|
|
"BySectionId" {
|
|
$_params.Item("Controller") = "sections"
|
|
$_sectionId = $SectionId
|
|
|
|
$_identifiers = @($_sectionId,"subnets")
|
|
}
|
|
"BySectionName" {
|
|
$_params.Item("Controller") = "sections"
|
|
$_sectionId = (Get-Section -Name $SectionName).id
|
|
if (!$_sectionId) { throw "Cannot find section!" }
|
|
|
|
$_identifiers = @($_sectionId,"subnets")
|
|
}
|
|
"ByVrfId" {
|
|
$_params.Item("Controller") = "vrf"
|
|
$_vrfId = $VrfId
|
|
|
|
$_identifiers = @($_vrfId,"subnets")
|
|
}
|
|
"ByVlanId" {
|
|
$_params.Item("Controller") = "vlan"
|
|
$_vlanId = $VlanId
|
|
if ($SectionId) { $_sectionId = $SectionId }
|
|
if ($SectionName){ $_sectionId = (Get-Section -Name $SectionName).id }
|
|
|
|
$_identifiers = @($_vlanId,"subnets")
|
|
|
|
if ($_sectionId) { $_identifiers += $_sectionId }
|
|
}
|
|
"ByVlanNumber" {
|
|
$_params.Item("Controller") = "vlan"
|
|
$_vlans = Get-Vlan -Number $VlanNumber
|
|
if ($VlanDomain) { $_vlans = $_vlans | Where-Object {$_.domainId -eq $VlanDomain} }
|
|
if ($SectionId) { $_sectionId = $SectionId }
|
|
if ($SectionName){ $_sectionId = (Get-Section -Name $SectionName).id }
|
|
|
|
$_vlanId = $_vlans.vlanId
|
|
|
|
if ($_vlanid -is [System.Array]) { throw "More than one vLan with $VlanNumber number is present!" }
|
|
if (!$_vlanId) { throw "Cannot find Vlan!"}
|
|
|
|
$_identifiers = @($_vlanId,"subnets")
|
|
|
|
if ($_sectionId) { $_identifiers += $_sectionId }
|
|
}
|
|
}
|
|
$_params.Add("Identifiers",$_identifiers)
|
|
|
|
Invoke-Request @_params
|
|
}
|
|
}
|
|
Export-ModuleMember Get-Subnet |