68 lines
2.9 KiB
PowerShell
68 lines
2.9 KiB
PowerShell
function Get-Address {
|
|
[CmdletBinding(DefaultParameterSetName="ByID")]
|
|
[OutputType('address')]
|
|
param (
|
|
[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="ByIP")]
|
|
[parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1,ParameterSetName="BySubnetId")]
|
|
[ValidateScript({[ipaddress] $_})]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$IP,
|
|
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByHostName")]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$HostName,
|
|
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByTag")]
|
|
[ValidateScript({ $_ -match "^\d+$" })]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$TagId,
|
|
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetId")]
|
|
[ValidateScript({ $_ -match "^\d+$" })]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$SubnetId,
|
|
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="BySubnetCIDR")]
|
|
[ValidateScript({[ipaddress] $_.Split("/")[0] -and $_.Split("/")[1] -match "\d{1,2}"})]
|
|
[ValidateNotNullOrEmpty()]
|
|
[string]
|
|
$SubnetCIDR
|
|
)
|
|
process {
|
|
$_params = @{
|
|
Controller = "addresses"
|
|
Method = "GET"
|
|
Type = "address"
|
|
}
|
|
switch ($PSCmdlet.ParameterSetName) {
|
|
"ByID" { $_identifiers = @($id) }
|
|
"ByIP" { $_identifiers = ("search",$IP) }
|
|
"ByHostName" { $_identifiers = ("search_hostname",$HostName) }
|
|
"ByTag" { $_identifiers = ("tags",$TagId,"addresses") }
|
|
"BySubnetId" {
|
|
if ($IP) {
|
|
$_identifiers = ($IP,$SubnetId)
|
|
} else {
|
|
$_params.Item("Controller") = "subnets"
|
|
$_identifiers = ($SubnetId,"addresses")
|
|
}
|
|
}
|
|
"BySubnetCIDR" {
|
|
$_params.Item("Controller") = "subnets"
|
|
$_subnetId = (Get-Subnet -CIDR $SubnetCIDR).id
|
|
if (!$_subnetId) { throw "Cannot find subnet!" }
|
|
|
|
$_identifiers = ($_subnetId,"addresses")
|
|
}
|
|
}
|
|
$_params.Add("Identifiers",$_identifiers)
|
|
|
|
Invoke-Request @_params
|
|
}
|
|
}
|
|
Export-ModuleMember -Function Get-Address |