Initial commit
This commit is contained in:
129
functions/public/Get-PSIPAMSubnet.ps1
Normal file
129
functions/public/Get-PSIPAMSubnet.ps1
Normal file
@@ -0,0 +1,129 @@
|
||||
function Get-PSIPAMSubnet {
|
||||
[CmdletBinding(DefaultParameterSetName="ByID")]
|
||||
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 {
|
||||
[string[]]$visiblePropertiesList = @('Id','Subnet','Mask','Description')
|
||||
$visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList)
|
||||
|
||||
$_params = @{
|
||||
Controller = "subnets"
|
||||
Method = "GET"
|
||||
}
|
||||
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-PSIPAMSection -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-PSIPAMSection -Name $SectionName).id }
|
||||
|
||||
$_identifiers = @($_vlanId,"subnets")
|
||||
|
||||
if ($_sectionId) { $_identifiers += $_sectionId }
|
||||
}
|
||||
"ByVlanNumber" {
|
||||
$_params.Item("Controller") = "vlan"
|
||||
$_vlans = Get-PSIPAMVlan -Number $VlanNumber
|
||||
if ($VlanDomain) { $_vlans = $_vlans | Where-Object {$_.domainId -eq $VlanDomain} }
|
||||
if ($SectionId) { $_sectionId = $SectionId }
|
||||
if ($SectionName){ $_sectionId = (Get-PSIPAMSection -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-PSIPAMRequest @_params | `
|
||||
Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru
|
||||
}
|
||||
}
|
||||
Export-ModuleMember Get-PSIPAMSubnet
|
||||
Reference in New Issue
Block a user