diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b061a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.dll +bin +obj \ No newline at end of file diff --git a/README.md b/README.md index ded5ff1..a8bce5c 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ [![License](https://img.shields.io/badge/license-GPL2+-blue.svg)](https://git.arnike.ru/Arnike/ps.ipam/src/branch/main/LICENSE) ![Powershell](https://img.shields.io/badge/powershell-v5.1+-blue.svg) +![PowerShell Gallery](https://img.shields.io/powershellgallery/dt/ps.ipam)

PS.IPAM

diff --git a/classlib/address.cs b/classlib/address.cs new file mode 100644 index 0000000..7f411fb --- /dev/null +++ b/classlib/address.cs @@ -0,0 +1,73 @@ +namespace PS.IPAM; +using System; + +public class Address { + public int Id { get; } + public int SubnetId { get; } + public string Ip { get; } + public bool IsGateway { get; } + public string Description { get; } + public string Hostname { get; } + public string MAC { get; } + public string Owner { get; } + public int TagId { get; } + public int DeviceId { get; } + public string Location { get; } + public int Port { get; } + public string Note { get; } + public DateTime? LastSeen { get; } + public bool ExcludePing { get; } + public int PTRignore { get; } + public int PTR { get; } + public string FirewallAddressObject { get; } + public DateTime? EditDate { get; } + public int CustomerId { get; } + + public Address( + int id, + int subnetId, + string ip, + bool is_gateway, + string description, + string hostname, + string mac, + string owner, + int tag, + int deviceId, + string location, + int port, + string note, + DateTime? lastSeen, + bool excludePing, + int PTRignore, + int PTR, + string firewallAddressObject, + DateTime? editDate, + int customer_id + ) { + this.Id = id; + this.SubnetId = subnetId; + this.Ip = ip; + this.IsGateway = is_gateway; + this.Description = description; + this.Hostname = hostname; + this.MAC = mac; + this.Owner = owner; + this.TagId = tag; + this.DeviceId = deviceId; + this.Location = location; + this.Port = port; + this.Note = note; + this.EditDate = lastSeen; + this.ExcludePing = excludePing; + this.PTRignore = PTRignore; + this.PTR = PTR; + this.FirewallAddressObject = firewallAddressObject; + this.EditDate = editDate; + this.CustomerId = customer_id; + } + + public override string ToString() { + return this.Ip; + } +} \ No newline at end of file diff --git a/classlib/classlib.csproj b/classlib/classlib.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/classlib/classlib.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/classlib/enum.cs b/classlib/enum.cs new file mode 100644 index 0000000..23d694b --- /dev/null +++ b/classlib/enum.cs @@ -0,0 +1,6 @@ +namespace PS.IPAM; +using System; + +enum Parameters { + IsGateway = "is_gateway"; +} \ No newline at end of file diff --git a/classlib/subnet.cs b/classlib/subnet.cs new file mode 100644 index 0000000..e3aca08 --- /dev/null +++ b/classlib/subnet.cs @@ -0,0 +1,95 @@ +namespace PS.IPAM; +using System; + +public class Subnetwork { + public int Id { get; } + public string Subnet { get; } + public int Mask { get; } + public int SectionId { get; } + public string Description { get; } + public string LinkedSubnet { get; } + public int FirewallAddressObject { get; } + public int VrfId { get; } + public int MasterSubnetId { get; } + public bool AllowRequests { get; } + public int VlanId { get; } + public bool ShowName { get; } + public int Device { get; } + public bool PingSubnet { get; } + public bool DiscoverSubnet { get; } + public bool ResolveDNS { get; } + public Subnetwork( + int id, + string subnet, + int mask, + int sectionId, + string description, + string linkedSubnet, + int firewallAddressObject, + int vrfId, + int masterSubnetId, + string permissions, + bool allowRequests, + int vlanId, + bool showName, + int device, + bool pingSubnet, + bool discoverSubnet, + bool resolveDNS + ) { + this.Id = id; + this.Subnet = subnet; + this.Mask = mask; + this.SectionId = sectionId; + this.Description = description; + this.LinkedSubnet = linkedSubnet; + this.FirewallAddressObject = firewallAddressObject; + this.VrfId = vrfId; + this.MasterSubnetId = masterSubnetId; + this.AllowRequests = allowRequests; + this.VlanId = vlanId; + this.ShowName = showName; + this.Device = device; + this.PingSubnet = pingSubnet; + this.DiscoverSubnet = discoverSubnet; + this.ResolveDNS = resolveDNS; + } + + public override string ToString() + { + return $"{this.Subnet}/{this.Mask}"; + } +} + +id : 1 +subnet : fd13:6d20:29dc:cf27:: +mask : 64 +sectionId : 2 +description : Private subnet 1 +linked_subnet : +firewallAddressObject : +vrfId : 0 +masterSubnetId : 0 +allowRequests : 1 +vlanId : 1 +showName : 1 +device : 0 +permissions : {"3":"1","2":"2"} +pingSubnet : 0 +discoverSubnet : 0 +resolveDNS : 0 +DNSrecursive : 0 +DNSrecords : 0 +nameserverId : 0 +scanAgent : +customer_id : +isFolder : 0 +isFull : 0 +isPool : 0 +tag : 2 +threshold : 0 +location : +editDate : +lastScan : +lastDiscovery : +calculation : @{Type=IPv6; Host address=/; Host address (uncompressed)=/; Subnet prefix=fd13:6d20:29dc:cf27::/64; Prefix length=64; Subnet Reverse DNS=7.2.f.c.c.d.9.2.0.2.d.6.3.1.d.f.ip6.arpa; Min host IP=fd13:6d20:29dc:cf27::; Max host IP=fd13:6d20:29dc:cf27:ffff:ffff:ffff:ffff; Number of hosts=18446744073709551616; Address type=NET_IPV6} \ No newline at end of file diff --git a/classlib/tag.cs b/classlib/tag.cs new file mode 100644 index 0000000..8b79ac0 --- /dev/null +++ b/classlib/tag.cs @@ -0,0 +1,43 @@ +namespace PS.IPAM; + +public class Tag { + public int Id { get; } + public string Type { get; } + public bool ShowTag { get; } + public string BGColor { get; } + public string FGColor { get; } + public bool Compress { get; } + public bool Locked { get; } + public bool UpdateTag { get; } + + public Tag( + int id, + string type, + bool showTag, + string BGColor, + string FGColor, + string compress, + string locked, + bool updateTag + ) { + this.Id = id; + this.Type = type; + this.ShowTag = showTag; + this.BGColor = BGColor; + this.FGColor = FGColor; + this.Compress = this.StringToBool(compress); + this.Locked = this.StringToBool(locked); + this.UpdateTag = updateTag; + } + + public override string ToString() { + return this.Type; + } + + private bool StringToBool(string str) { + if (str == "Yes") { + return true; + } + return false; + } +} \ No newline at end of file diff --git a/functions/private/Invoke-PSIPAMRequest.ps1 b/functions/private/Invoke-Request.ps1 similarity index 64% rename from functions/private/Invoke-PSIPAMRequest.ps1 rename to functions/private/Invoke-Request.ps1 index 00040bf..fd15398 100644 --- a/functions/private/Invoke-PSIPAMRequest.ps1 +++ b/functions/private/Invoke-Request.ps1 @@ -1,4 +1,4 @@ -function Invoke-PSIPAMRequest { +function Invoke-Request { [CmdletBinding()] param ( [parameter(Mandatory=$true)] @@ -10,6 +10,10 @@ function Invoke-PSIPAMRequest { [string] $Controller, [parameter(Mandatory=$false)] + [ValidateSet("address","tag","subnet")] + [string] + $Type, + [parameter(Mandatory=$false)] [ValidateSet("nameservers")] [string] $SubController, @@ -20,10 +24,10 @@ function Invoke-PSIPAMRequest { [array] $Identifiers ) - $_tokenStatus = Test-PSIPAMSession + $_tokenStatus = Test-Session if ($_tokenStatus -eq "NoToken") { throw "No session available!" } - if ($_tokenStatus -eq "Expired") { Update-PSIPAMSession } + if ($_tokenStatus -eq "Expired") { Update-Session } $Controller = $Controller.ToLower() @@ -34,7 +38,10 @@ function Invoke-PSIPAMRequest { $_headers = @{ "Accept" = "application/json" "Content-Type" = "application/json" - "token" = $script:ipamToken + } + switch ($script:ipamAuthType) { + "Credentials" { $_headers.Add("token", $script:ipamToken) } + "Token" { $_headers.Add("phpipam-token", $script:ipamToken) } } $_arguments = @{ @@ -57,7 +64,12 @@ function Invoke-PSIPAMRequest { $_response = Invoke-RestMethod @_arguments if ($_response.code -match "20\d") { - return $_response.data + switch ($Type) { + "address" { $_response.data | ForEach-Object { New-Object -TypeName "PS.IPAM.Address" -ArgumentList $_.psobject.properties.value } } + "subnet" { $_response.data | ForEach-Object { New-Object -TypeName "PS.IPAM.Subnetwork" -ArgumentList $_.psobject.properties.value } } + "tag" { $_response.data | ForEach-Object { New-Object -TypeName "PS.IPAM.Tag" -ArgumentList $_.psobject.properties.value } } + Default { $_response.data } + } } else { throw ("Error - $($_response.code)") } diff --git a/functions/private/Test-PSIPAMSession.ps1 b/functions/private/Test-PSIPAMSession.ps1 deleted file mode 100644 index 4c4cf77..0000000 --- a/functions/private/Test-PSIPAMSession.ps1 +++ /dev/null @@ -1,15 +0,0 @@ -function Test-PSIPAMSession { - [CmdletBinding()] - param ( - - ) - if ($script:ipamToken) { - if ($script:ipamExpires -lt (Get-Date)) { - return "Expired" - } else { - return "Valid" - } - } else { - return "NoToken" - } -} \ No newline at end of file diff --git a/functions/private/Test-Session.ps1 b/functions/private/Test-Session.ps1 new file mode 100644 index 0000000..a1ab7c8 --- /dev/null +++ b/functions/private/Test-Session.ps1 @@ -0,0 +1,19 @@ +function Test-Session { + [CmdletBinding()] + param ( + + ) + if ($script:ipamToken) { + if ($script:ipamExpires -eq "Never") { + return "Valid" + } else { + if ($script:ipamExpires -lt (Get-Date)) { + return "Expired" + } else { + return "Valid" + } + } + } else { + return "NoToken" + } +} \ No newline at end of file diff --git a/functions/private/Update-PSIPAMSession.ps1 b/functions/private/Update-Session.ps1 similarity index 58% rename from functions/private/Update-PSIPAMSession.ps1 rename to functions/private/Update-Session.ps1 index d8ade10..e583084 100644 --- a/functions/private/Update-PSIPAMSession.ps1 +++ b/functions/private/Update-Session.ps1 @@ -1,18 +1,18 @@ -function Update-PSIPAMSession { +function Update-Session { [CmdletBinding()] param ( [switch] $Force ) - $_tokenStatus = Test-PSIPAMSession + $_tokenStatus = Test-Session if ($_tokenStatus -eq "NoToken") { throw "No session available!" } if ($_tokenStatus -eq "Valid") { - return (Invoke-PSIPAMRequest -Method PATCH -Controller user).expires + return (Invoke-Request -Method PATCH -Controller user).expires } if ($_tokenStatus -eq "Expired" -or $Force) { - New-PSIPAMSession -URL $script:ipamURL -AppID $script:ipamAppID -Credentials $script:ipamCredentials + New-Session -URL $script:ipamURL -AppID $script:ipamAppID -Credentials $script:ipamCredentials return $script:ipamExpires } } \ No newline at end of file diff --git a/functions/public/Get-Address.ps1 b/functions/public/Get-Address.ps1 new file mode 100644 index 0000000..a3efc3c --- /dev/null +++ b/functions/public/Get-Address.ps1 @@ -0,0 +1,68 @@ +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 \ No newline at end of file diff --git a/functions/public/Get-PSIPAMFirstFreeIP.ps1 b/functions/public/Get-FirstFreeIP.ps1 similarity index 82% rename from functions/public/Get-PSIPAMFirstFreeIP.ps1 rename to functions/public/Get-FirstFreeIP.ps1 index 47215d2..c5702d8 100644 --- a/functions/public/Get-PSIPAMFirstFreeIP.ps1 +++ b/functions/public/Get-FirstFreeIP.ps1 @@ -1,4 +1,4 @@ -function Get-PSIPAMFirstFreeIP { +function Get-FirstFreeIP { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByCIDR")] @@ -20,14 +20,14 @@ function Get-PSIPAMFirstFreeIP { switch ($PSCmdlet.ParameterSetName) { "ByID" { $_subnetId = $Id } "ByCIDR" { - $_subnetId = (Get-PSIPAMSubnet -CIDR $CIDR).id + $_subnetId = (Get-Subnet -CIDR $CIDR).id if (!$_subnetId) { throw "Cannot find subnet!" } } } $_identifiers = @($_subnetId,"first_free") $_params.Add("Identifiers",$_identifiers) - return Invoke-PSIPAMRequest @_params | Select-Object @{n="Ip";e={$_}} + return Invoke-Request @_params | Select-Object @{n="Ip";e={$_}} } } -Export-ModuleMember -Function Get-PSIPAMFirstFreeIP \ No newline at end of file +Export-ModuleMember -Function Get-FirstFreeIP \ No newline at end of file diff --git a/functions/public/Get-PSIPAML2Domain.ps1 b/functions/public/Get-L2Domain.ps1 similarity index 87% rename from functions/public/Get-PSIPAML2Domain.ps1 rename to functions/public/Get-L2Domain.ps1 index 2a20537..d61bf48 100644 --- a/functions/public/Get-PSIPAML2Domain.ps1 +++ b/functions/public/Get-L2Domain.ps1 @@ -1,4 +1,4 @@ -function Get-PSIPAML2Domain { +function Get-L2Domain { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)] @@ -19,8 +19,8 @@ function Get-PSIPAML2Domain { $_params.Add("Identifiers",$_identifiers) - Invoke-PSIPAMRequest @_params | ` + Invoke-Request @_params | ` Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru } } -Export-ModuleMember -Function Get-PSIPAML2Domain \ No newline at end of file +Export-ModuleMember -Function Get-L2Domain \ No newline at end of file diff --git a/functions/public/Get-PSIPAMNameserver.ps1 b/functions/public/Get-Nameserver.ps1 similarity index 82% rename from functions/public/Get-PSIPAMNameserver.ps1 rename to functions/public/Get-Nameserver.ps1 index 8c6fa3b..f487108 100644 --- a/functions/public/Get-PSIPAMNameserver.ps1 +++ b/functions/public/Get-Nameserver.ps1 @@ -1,4 +1,4 @@ -function Get-PSIPAMNameserver { +function Get-Nameserver { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] @@ -22,8 +22,8 @@ function Get-PSIPAMNameserver { $_identifiers = @($_nameserverId) $_params.Add("Identifiers",$_identifiers) - Invoke-PSIPAMRequest @_params | Select-Object @{n="address";e={$_.namesrv1}},* | Select-Object -ExcludeProperty namesrv1 | ` + Invoke-Request @_params | Select-Object @{n="address";e={$_.namesrv1}},* | Select-Object -ExcludeProperty namesrv1 | ` Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru } } -Export-ModuleMember -Function Get-PSIPAMNameserver \ No newline at end of file +Export-ModuleMember -Function Get-Nameserver \ No newline at end of file diff --git a/functions/public/Get-PSIPAMTags.ps1 b/functions/public/Get-PSIPAMTags.ps1 deleted file mode 100644 index cd8f0ec..0000000 --- a/functions/public/Get-PSIPAMTags.ps1 +++ /dev/null @@ -1,26 +0,0 @@ -function Get-PSIPAMTags { - [CmdletBinding()] - param ( - [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)] - [ValidateScript({ $_ -match "^\d+$" })] - [ValidateNotNullOrEmpty()] - [string] - $Id - ) - process { - [string[]]$visiblePropertiesList = @('Id','Name') - $visibleProperties = [System.Management.Automation.PSPropertySet]::new('DefaultDisplayPropertySet',$visiblePropertiesList) - - $_params = @{ - Controller = "addresses" - Method = "GET" - } - $_identifiers = @("tags") - if ($Id) { $_identifiers += $Id } - $_params.Add("Identifiers",$_identifiers) - - Invoke-PSIPAMRequest @_params | ` - Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru - } -} -Export-ModuleMember -Function Get-PSIPAMTags \ No newline at end of file diff --git a/functions/public/Get-PSIPAMAddress.ps1 b/functions/public/Get-Permissions.ps1 similarity index 92% rename from functions/public/Get-PSIPAMAddress.ps1 rename to functions/public/Get-Permissions.ps1 index 252536c..d86b94c 100644 --- a/functions/public/Get-PSIPAMAddress.ps1 +++ b/functions/public/Get-Permissions.ps1 @@ -1,5 +1,5 @@ -function Get-PSIPAMAddress { - [CmdletBinding(DefaultParameterSetName="ByID")] +function Get-Permissions { + [CmdletBinding()] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] [ValidateScript({ $_ -match "^\d+$" })] @@ -55,7 +55,7 @@ function Get-PSIPAMAddress { } "BySubnetCIDR" { $_params.Item("Controller") = "subnets" - $_subnetId = (Get-PSIPAMSubnet -CIDR $SubnetCIDR).id + $_subnetId = (Get-Subnet -CIDR $SubnetCIDR).id if (!$_subnetId) { throw "Cannot find subnet!" } $_identifiers = ($_subnetId,"addresses") @@ -63,8 +63,8 @@ function Get-PSIPAMAddress { } $_params.Add("Identifiers",$_identifiers) - Invoke-PSIPAMRequest @_params | ` + Invoke-Request @_params | ` Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru } } -Export-ModuleMember -Function Get-PSIPAMAddress \ No newline at end of file +Export-ModuleMember -Function Get-Permissions \ No newline at end of file diff --git a/functions/public/Get-PSIPAMSection.ps1 b/functions/public/Get-Section.ps1 similarity index 90% rename from functions/public/Get-PSIPAMSection.ps1 rename to functions/public/Get-Section.ps1 index 0894b1d..30caca2 100644 --- a/functions/public/Get-PSIPAMSection.ps1 +++ b/functions/public/Get-Section.ps1 @@ -1,4 +1,4 @@ -function Get-PSIPAMSection { +function Get-Section { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,Position=0,ParameterSetName="ByID")] @@ -25,8 +25,8 @@ function Get-PSIPAMSection { } $_params.Add("Identifiers",$_identifiers) - Invoke-PSIPAMRequest @_params | ` + Invoke-Request @_params | ` Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru } } -Export-ModuleMember Get-PSIPAMSection \ No newline at end of file +Export-ModuleMember Get-Section \ No newline at end of file diff --git a/functions/public/Get-PSIPAMSubnet.ps1 b/functions/public/Get-Subnet.ps1 similarity index 90% rename from functions/public/Get-PSIPAMSubnet.ps1 rename to functions/public/Get-Subnet.ps1 index 0d22635..205a749 100644 --- a/functions/public/Get-PSIPAMSubnet.ps1 +++ b/functions/public/Get-Subnet.ps1 @@ -1,4 +1,4 @@ -function Get-PSIPAMSubnet { +function Get-Subnet { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByCIDR")] @@ -58,6 +58,7 @@ function Get-PSIPAMSubnet { $_params = @{ Controller = "subnets" Method = "GET" + Type = "subnet" } switch ($PSCmdlet.ParameterSetName) { "ByCIDR" { @@ -82,7 +83,7 @@ function Get-PSIPAMSubnet { } "BySectionName" { $_params.Item("Controller") = "sections" - $_sectionId = (Get-PSIPAMSection -Name $SectionName).id + $_sectionId = (Get-Section -Name $SectionName).id if (!$_sectionId) { throw "Cannot find section!" } $_identifiers = @($_sectionId,"subnets") @@ -97,7 +98,7 @@ function Get-PSIPAMSubnet { $_params.Item("Controller") = "vlan" $_vlanId = $VlanId if ($SectionId) { $_sectionId = $SectionId } - if ($SectionName){ $_sectionId = (Get-PSIPAMSection -Name $SectionName).id } + if ($SectionName){ $_sectionId = (Get-Section -Name $SectionName).id } $_identifiers = @($_vlanId,"subnets") @@ -105,10 +106,10 @@ function Get-PSIPAMSubnet { } "ByVlanNumber" { $_params.Item("Controller") = "vlan" - $_vlans = Get-PSIPAMVlan -Number $VlanNumber + $_vlans = Get-Vlan -Number $VlanNumber if ($VlanDomain) { $_vlans = $_vlans | Where-Object {$_.domainId -eq $VlanDomain} } if ($SectionId) { $_sectionId = $SectionId } - if ($SectionName){ $_sectionId = (Get-PSIPAMSection -Name $SectionName).id } + if ($SectionName){ $_sectionId = (Get-Section -Name $SectionName).id } $_vlanId = $_vlans.vlanId @@ -122,8 +123,7 @@ function Get-PSIPAMSubnet { } $_params.Add("Identifiers",$_identifiers) - Invoke-PSIPAMRequest @_params | ` - Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru + Invoke-Request @_params } } -Export-ModuleMember Get-PSIPAMSubnet \ No newline at end of file +Export-ModuleMember Get-Subnet \ No newline at end of file diff --git a/functions/public/Get-PSIPAMSubnetUsage.ps1 b/functions/public/Get-SubnetUsage.ps1 similarity index 84% rename from functions/public/Get-PSIPAMSubnetUsage.ps1 rename to functions/public/Get-SubnetUsage.ps1 index 7b8b550..4cf1cbf 100644 --- a/functions/public/Get-PSIPAMSubnetUsage.ps1 +++ b/functions/public/Get-SubnetUsage.ps1 @@ -1,4 +1,4 @@ -function Get-PSIPAMSubnetUsage { +function Get-SubnetUsage { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByCIDR")] @@ -19,7 +19,7 @@ function Get-PSIPAMSubnetUsage { } switch ($PSCmdlet.ParameterSetName) { "ByCIDR" { - $_subnetId = (Get-PSIPAMSubnet -CIDR $CIDR).id + $_subnetId = (Get-Subnet -CIDR $CIDR).id if (!$_subnetId) { throw "Cannot find subnet!" } } "ByID" { $_subnetId = $Id } @@ -27,7 +27,7 @@ function Get-PSIPAMSubnetUsage { $_identifiers = @($_subnetId,"usage") $_params.Add("Identifiers",$_identifiers) - return Invoke-PSIPAMRequest @_params + return Invoke-Request @_params } } -Export-ModuleMember -Function Get-PSIPAMSubnetUsage \ No newline at end of file +Export-ModuleMember -Function Get-SubnetUsage \ No newline at end of file diff --git a/functions/public/Get-Tag.ps1 b/functions/public/Get-Tag.ps1 new file mode 100644 index 0000000..02cec76 --- /dev/null +++ b/functions/public/Get-Tag.ps1 @@ -0,0 +1,37 @@ +function Get-Tag { + [CmdletBinding(DefaultParameterSetName="NoParams")] + [OutputType('tag')] + param ( + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] + [ValidateScript({ $_ -match "^\d+$" })] + [ValidateNotNullOrEmpty()] + [string] + $Id, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByAddressObject")] + [ValidateNotNullOrEmpty()] + [ps.ipam.address] + $InputObject + ) + process { + $_params = @{ + Controller = "addresses" + Method = "GET" + Type = "tag" + } + $_identifiers = @("tags") + switch ($PSCmdlet.ParameterSetName) { + "ByID" { $_identifiers += $Id } + "ByAddressObject" { + if ($InputObject.TagId) { + $_identifiers += $InputObject.TagId + } else { + return $null + } + } + } + $_params.Add("Identifiers",$_identifiers) + + Invoke-Request @_params + } +} +Export-ModuleMember -Function Get-Tag \ No newline at end of file diff --git a/functions/public/Get-PSIPAMVlan.ps1 b/functions/public/Get-Vlan.ps1 similarity index 89% rename from functions/public/Get-PSIPAMVlan.ps1 rename to functions/public/Get-Vlan.ps1 index 60634b2..10565a0 100644 --- a/functions/public/Get-PSIPAMVlan.ps1 +++ b/functions/public/Get-Vlan.ps1 @@ -1,4 +1,4 @@ -function Get-PSIPAMVlan { +function Get-Vlan { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] @@ -38,8 +38,8 @@ function Get-PSIPAMVlan { } $_params.Add("Identifiers",$_identifiers) - Invoke-PSIPAMRequest @_params | Select-Object @{n="id";e={$_.vlanId}},* | Select-Object -ExcludeProperty vlanId | ` + Invoke-Request @_params | Select-Object @{n="id";e={$_.vlanId}},* | Select-Object -ExcludeProperty vlanId | ` Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru } } -Export-ModuleMember -Function Get-PSIPAMVlan \ No newline at end of file +Export-ModuleMember -Function Get-Vlan \ No newline at end of file diff --git a/functions/public/Get-PSIPAMVrf.ps1 b/functions/public/Get-Vrf.ps1 similarity index 81% rename from functions/public/Get-PSIPAMVrf.ps1 rename to functions/public/Get-Vrf.ps1 index 6846a4c..7ab658b 100644 --- a/functions/public/Get-PSIPAMVrf.ps1 +++ b/functions/public/Get-Vrf.ps1 @@ -1,4 +1,4 @@ -function Get-PSIPAMVrf { +function Get-Vrf { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0,ParameterSetName="ByID")] @@ -19,8 +19,8 @@ function Get-PSIPAMVrf { $_params.Add("Identifiers",$_identifiers) - Invoke-PSIPAMRequest @_params | Select-Object @{n="id";e={$_.vrfId}},* | Select-Object -ExcludeProperty vrfId | ` + Invoke-Request @_params | Select-Object @{n="id";e={$_.vrfId}},* | Select-Object -ExcludeProperty vrfId | ` Add-Member -MemberType MemberSet -Name PSStandardMembers -Value $visibleProperties -PassThru } } -Export-ModuleMember -Function Get-PSIPAMVrf \ No newline at end of file +Export-ModuleMember -Function Get-Vrf \ No newline at end of file diff --git a/functions/public/New-PSIPAMAddress.ps1 b/functions/public/New-Address.ps1 similarity index 97% rename from functions/public/New-PSIPAMAddress.ps1 rename to functions/public/New-Address.ps1 index 0a70bfa..b805b86 100644 --- a/functions/public/New-PSIPAMAddress.ps1 +++ b/functions/public/New-Address.ps1 @@ -1,4 +1,4 @@ -function New-PSIPAMAddress { +function New-Address { [CmdletBinding()] param ( [parameter( @@ -172,11 +172,11 @@ function New-PSIPAMAddress { $_params.Add("Params",$_body) try { - Invoke-PSIPAMRequest @_params + Invoke-Request @_params } finally { - Get-PSIPAMAddress -SubnetId $SubnetId -Ip $Ip + Get-Address -SubnetId $SubnetId -Ip $Ip } } } -Export-ModuleMember -Function New-PSIPAMAddress \ No newline at end of file +Export-ModuleMember -Function New-Address \ No newline at end of file diff --git a/functions/public/New-PSIPAMFirstFreeIP.ps1 b/functions/public/New-FirstFreeIP.ps1 similarity index 96% rename from functions/public/New-PSIPAMFirstFreeIP.ps1 rename to functions/public/New-FirstFreeIP.ps1 index fe377dc..39c2b8b 100644 --- a/functions/public/New-PSIPAMFirstFreeIP.ps1 +++ b/functions/public/New-FirstFreeIP.ps1 @@ -1,4 +1,4 @@ -function New-PSIPAMFirstFreeIP { +function New-FirstFreeIP { [CmdletBinding()] param ( [parameter( @@ -163,10 +163,10 @@ function New-PSIPAMFirstFreeIP { $_params.Add("Params",$_body) - $_result = Invoke-PSIPAMRequest @_params + $_result = Invoke-Request @_params if ($_result) { - Get-PSIPAMAddress -SubnetId $SubnetId -IP $_result + Get-Address -SubnetId $SubnetId -IP $_result } } } -Export-ModuleMember -Function New-PSIPAMFirstFreeIP \ No newline at end of file +Export-ModuleMember -Function New-FirstFreeIP \ No newline at end of file diff --git a/functions/public/New-PSIPAMSession.ps1 b/functions/public/New-PSIPAMSession.ps1 deleted file mode 100644 index 119a59e..0000000 --- a/functions/public/New-PSIPAMSession.ps1 +++ /dev/null @@ -1,37 +0,0 @@ -function New-PSIPAMSession { - [CmdletBinding()] - param ( - [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)] - [ValidateNotNullOrEmpty()] - [string]$URL, - [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1)] - [ValidateNotNullOrEmpty()] - [string]$AppID, - [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2)] - [ValidateNotNullOrEmpty()] - [pscredential]$Credentials - ) - $_bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credentials.Password) - $_password = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($_bstr) - $_uri = "$URL/api/$AppID/user" - $_auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($Credentials.UserName):$_password")) - $_headers = @{ - "Accept" = "application/json" - "Content-Type" = "application/json" - "Authorization" = "Basic $_auth" - } - - $_response = Invoke-RestMethod -Method Post -Uri $_uri -Headers $_headers -ErrorAction SilentlyContinue - - if ($_response.success -eq $true) { - $script:ipamAuth = $true - $script:ipamToken = $_response.data.token - $script:ipamAppID = $AppID - $script:ipamURL = $URL - $script:ipamCredentials = $Credentials - $script:ipamExpires = Get-Date $_response.data.expires - } else { - $_response.error - } -} -Export-ModuleMember -Function New-PSIPAMSession \ No newline at end of file diff --git a/functions/public/New-Session.ps1 b/functions/public/New-Session.ps1 new file mode 100644 index 0000000..560594e --- /dev/null +++ b/functions/public/New-Session.ps1 @@ -0,0 +1,53 @@ +function New-Session { + [CmdletBinding(DefaultParameterSetName="Credentials")] + param ( + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)] + [ValidateNotNullOrEmpty()] + [string]$URL, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=1)] + [ValidateNotNullOrEmpty()] + [string]$AppID, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2,ParameterSetName="Credentials")] + [ValidateNotNullOrEmpty()] + [pscredential]$Credentials, + [parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=2,ParameterSetName="Token")] + [ValidateNotNullOrEmpty()] + [string]$Token, + [parameter(Mandatory=$false,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=3)] + [switch]$IgnoreSSL = $false + ) + switch ($PSCmdlet.ParameterSetName) { + "Credentials" { + $_bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credentials.Password) + $_password = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($_bstr) + $_uri = "$URL/api/$AppID/user" + $_auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($Credentials.UserName):$_password")) + $_headers = @{ + "Accept" = "application/json" + "Content-Type" = "application/json" + "Authorization" = "Basic $_auth" + } + + $_response = Invoke-RestMethod -Method Post -Uri $_uri -Headers $_headers -ErrorAction SilentlyContinue + + if ($_response.success -ne $true) { return $_response.error } + + $script:ipamAuthType = "Credentials" + $script:ipamAuth = $true + $script:ipamToken = $_response.data.token + $script:ipamAppID = $AppID + $script:ipamURL = $URL + $script:ipamCredentials = $Credentials + $script:ipamExpires = Get-Date $_response.data.expires + } + "Token" { + $script:ipamAuthType = "Token" + $script:ipamAuth = $true + $script:ipamToken = $Token + $script:ipamAppID = $AppID + $script:ipamURL = $URL + $script:ipamExpires = "Never" + } + } +} +Export-ModuleMember -Function New-Session \ No newline at end of file diff --git a/functions/public/New-PSIPAMSubnet.ps1 b/functions/public/New-Subnet.ps1 similarity index 97% rename from functions/public/New-PSIPAMSubnet.ps1 rename to functions/public/New-Subnet.ps1 index d655d34..72db734 100644 --- a/functions/public/New-PSIPAMSubnet.ps1 +++ b/functions/public/New-Subnet.ps1 @@ -1,4 +1,4 @@ -function New-PSIPAMSubnet { +function New-Subnet { [CmdletBinding()] param ( [parameter( @@ -193,10 +193,10 @@ function New-PSIPAMSubnet { $_params.Add("Params",$_body) - $_result = Invoke-PSIPAMRequest @_params + $_result = Invoke-Request @_params if ($_result) { - return Get-PSIPAMSubnet -CIDR $_result + return Get-Subnet -CIDR $_result } } } -Export-ModuleMember -Function New-PSIPAMSubnet \ No newline at end of file +Export-ModuleMember -Function New-Subnet \ No newline at end of file diff --git a/functions/public/Remove-PSIPAMAddress.ps1 b/functions/public/Remove-Address.ps1 similarity index 90% rename from functions/public/Remove-PSIPAMAddress.ps1 rename to functions/public/Remove-Address.ps1 index 49d31b0..fba0892 100644 --- a/functions/public/Remove-PSIPAMAddress.ps1 +++ b/functions/public/Remove-Address.ps1 @@ -1,4 +1,4 @@ -function Remove-PSIPAMAddress { +function Remove-Address { [CmdletBinding(DefaultParameterSetName="ByID")] param ( [parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0,ParameterSetName="ByID")] @@ -29,7 +29,7 @@ function Remove-PSIPAMAddress { } $_params.Add("Identifiers",$_identifiers) - Invoke-PSIPAMRequest @_params + Invoke-Request @_params } } -Export-ModuleMember Remove-PSIPAMAddress \ No newline at end of file +Export-ModuleMember Remove-Address \ No newline at end of file diff --git a/functions/public/Set-PSIPAMAddress.ps1 b/functions/public/Set-Address.ps1 similarity index 97% rename from functions/public/Set-PSIPAMAddress.ps1 rename to functions/public/Set-Address.ps1 index df04f00..a9c0693 100644 --- a/functions/public/Set-PSIPAMAddress.ps1 +++ b/functions/public/Set-Address.ps1 @@ -1,4 +1,4 @@ -function Set-PSIPAMAddress { +function Set-Address { [CmdletBinding()] param ( [parameter( @@ -162,11 +162,11 @@ function Set-PSIPAMAddress { $_params.Add("Params",$_body) try { - Invoke-PSIPAMRequest @_params + Invoke-Request @_params } finally { - Get-PSIPAMAddress -Id $Id + Get-Address -Id $Id } } } -Export-ModuleMember -Function Set-PSIPAMAddress \ No newline at end of file +Export-ModuleMember -Function Set-Address \ No newline at end of file diff --git a/functions/public/Set-PSIPAMSubnet.ps1 b/functions/public/Set-Subnet.ps1 similarity index 100% rename from functions/public/Set-PSIPAMSubnet.ps1 rename to functions/public/Set-Subnet.ps1 diff --git a/old/address.ps1 b/old/address.ps1 new file mode 100644 index 0000000..8c0f9c6 --- /dev/null +++ b/old/address.ps1 @@ -0,0 +1,75 @@ +Add-Type @' +using System; +namespace ps.ipam { + public class address { + public int Id { get; set; } + public int SubnetId { get; set; } + public string Ip { get; set; } + public bool IsGateway { get; set; } + public string Description { get; set; } + public string Hostname { get; set; } + public string MAC { get; set; } + public string Owner { get; set; } + public int TagId { get; set; } + public int DeviceId { get; set; } + public string Location { get; set; } + public int Port { get; set; } + public string Note { get; set; } + public DateTime? LastSeen { get; set; } + public bool ExcludePing { get; set; } + public int PTRignore { get; set; } + public int PTR { get; set; } + public string FirewallAddressObject { get; set; } + public DateTime? EditDate { get; set; } + public int CustomerId { get; set; } + + public address( + int id, + int subnetId, + string ip, + bool is_gateway, + string description, + string hostname, + string mac, + string owner, + int tag, + int deviceId, + string location, + int port, + string note, + DateTime? lastSeen, + bool excludePing, + int PTRignore, + int PTR, + string firewallAddressObject, + DateTime? editDate, + int customer_id + ) { + this.Id = id; + this.SubnetId = subnetId; + this.Ip = ip; + this.IsGateway = is_gateway; + this.Description = description; + this.Hostname = hostname; + this.MAC = mac; + this.Owner = owner; + this.TagId = tag; + this.DeviceId = deviceId; + this.Location = location; + this.Port = port; + this.Note = note; + this.EditDate = lastSeen; + this.ExcludePing = excludePing; + this.PTRignore = PTRignore; + this.PTR = PTR; + this.FirewallAddressObject = firewallAddressObject; + this.EditDate = editDate; + this.CustomerId = customer_id; + } + + public override string ToString() { + return this.Ip; + } + } +} +'@ \ No newline at end of file diff --git a/old/tag.ps1 b/old/tag.ps1 new file mode 100644 index 0000000..5d9d06e --- /dev/null +++ b/old/tag.ps1 @@ -0,0 +1,33 @@ +class tag { + [int]$id + [string]$Type + [bool]$ShowTag + [System.Drawing.Color]$BGColor + [System.Drawing.Color]$FGColor + [bool]$Compress + [bool]$Locked + [bool]$UpdateTag + + tag ([int]$id,[string]$type,[bool]$showTag,[string]$BGColor,[string]$FGColor,[string]$compress,[string]$locked,[bool]$updateTag) { + $this.id = $id + $this.Type = $type + $this.ShowTag = $showTag + $this.BGColor = $BGColor + $this.FGColor = $FGColor + $this.Compress = $this.ToBool($compress) + $this.Locked = $this.ToBool($locked) + $this.UpdateTag = $updateTag + } + + [string] ToString() { + return $this.Type + } + + hidden [bool]ToBool([string]$str) { + if ($str -eq "Yes") { + return $true + } else { + return $false + } + } +} \ No newline at end of file diff --git a/ps.ipam.psd1 b/ps.ipam.psd1 index 24b4119..a267af2 100644 Binary files a/ps.ipam.psd1 and b/ps.ipam.psd1 differ diff --git a/types/types.ps1xml b/types/types.ps1xml new file mode 100644 index 0000000..d2f7654 --- /dev/null +++ b/types/types.ps1xml @@ -0,0 +1,51 @@ + + + + ps.ipam.address + + + PSStandardMembers + + + DefaultDisplayPropertySet + + Id + Ip + Hostname + Description + + + + DefaultKeyPropertySet + + Id + + + + + + + + ps.ipam.tag + + + PSStandardMembers + + + DefaultDisplayPropertySet + + Id + Type + + + + DefaultKeyPropertySet + + Id + + + + + + + \ No newline at end of file