diff --git a/Bluecat.Address.Manager.psm1 b/Bluecat.Address.Manager.psm1 index 6394dd2..a51fcc7 100644 --- a/Bluecat.Address.Manager.psm1 +++ b/Bluecat.Address.Manager.psm1 @@ -1,19 +1,23 @@ function Convert-BamProperty { param ( [parameter( - mandatory = $true, valueFromPipeline = $true )] [psobject] $bamObject ) begin {} process { - $prop = $bamObject.properties.trim("|") -split "\|" - foreach ($p in $prop) { - $member = $p -split "=" - $bamObject | Add-Member -NotePropertyName $member[0] -NotePropertyValue $member[1] + if ($bamObject.properties) { + $newObject = $bamObject | select -property * -excludeproperty properties + $prop = $bamObject.properties.trim("|") -split "\|" + foreach ($p in $prop) { + $member = $p -split "=" + $newObject | Add-Member -NotePropertyName "_$($member[0])" -NotePropertyValue $member[1] + } + $newObject + } else { + $bamObject } - $bamObject | select -property * -excludeproperty properties } end {} } @@ -50,7 +54,7 @@ function Get-BamDnsZone { $ZoneName, $bam = $defaultBam ) - $cfg = get-BamConfiguration $ConfigurationName + $cfg = get-BamConfiguration $ConfigurationName $bam $bam.getZonesByHint($cfg.id, 0, 10, "hint=$ZoneName") } @@ -69,13 +73,31 @@ function Get-BamServer { $ConfigurationName, $bam = $defaultBam ) - $cfg = get-BamConfiguration $ConfigurationName + $cfg = get-BamConfiguration $ConfigurationName $bam $bam.getentities($cfg.id, "Server", 0, [int32]::MaxValue) } +function Get-BamDevice { + param( + $ConfigurationName, + $bam = $defaultBam + ) + $cfg = get-BamConfiguration $ConfigurationName $bam + $bam.getentities($cfg.id, "Device", 0, [int32]::MaxValue) +} + +function Get-BamMac { + param( + $ConfigurationName, + $bam = $defaultBam + ) + $cfg = get-BamConfiguration $ConfigurationName $bam + $bam.getentities($cfg.id, "MACAddress", 0, [int32]::MaxValue) +} + function Get-BamDnsRecord { param( - $Zone, + $ZoneId, [ValidateSet( "HostRecord", "AliasRecord", @@ -89,27 +111,216 @@ function Get-BamDnsRecord { $type, $bam = $defaultBam ) - $bam.getEntities($zone.id, $type, 0, [int32]::MaxValue) + $bam.getEntities($zoneId, $type, 0, [int32]::MaxValue) } function Add-BamDnsHostRecord { param( - $view, - $fqdn, + $viewId, + $name, + $zoneName, $ip, $ttl, + $prop = "reverseRecord=true|parentZoneName=$zoneName", $bam = $defaultBam ) - $bam.addHostRecord($view.id, $fqdn, $ip, $ttl, "reverseRecord=true") + try { + $bam.addHostRecord($viewId, "$name.$zoneName", $ip, $ttl, $prop) + } catch { + $err = $_.exception.message + switch -regex ($err) { + "IP Address doesn't belong to a Network" { + Add-BamDnsGenericRecord -viewId $viewId ` + -name $name ` + -zonename $zoneName ` + -type "A" ` + -rdata $ip ` + -ttl $ttl ` + -bam $bam + break + } + "Duplicate of another item" { + $zone = $bam.getZonesByHint($viewid, 0, 1, "hint=$zonename") + $record = $bam.getEntityByName($zone.id,$name,'HostRecord') + $r = $record | Convert-BamProperty + if ($ip -notin $r._addresses) { + $record.properties = $record.properties -replace "addresses=", "addresses=$ip," + try { + $bam.update($record) + } catch { + $err = $_.exception.message + switch -regex ($err) { + "Some of the specified addresses are reserved" { + write-host "cannot update $name because $ip is reserved" + break + } + default { + write-host "$name.$zoneName" + write-error $err + } + } + } + } + $record.id + break + } + "Some of the specified addresses are reserved" { + write-host "cannot add $name because $ip is reserved" + break + } + default { + write-host "$name.$zoneName" + write-error $err + } + } + } } function Add-BamDnsAliasRecord { param( - $view, - $fqdn, + $viewId, + $name, + $zoneName, $targetFqdn, $ttl, + $prop = "parentZoneName=$zoneName", + $bam = $defaultBam + ) + try { + $bam.addAliasRecord($viewId, "$name.$zoneName", $targetFqdn, $ttl, $prop) + } catch { + $err = $_.exception.message + switch -regex ($err) { + "Object was not found" { + if ($targetFqdn -notmatch $zoneName) { + Add-BamDnsExternalRecord $viewId $targetFqdn $bam + Add-BamDnsAliasRecord $viewId $name $zonename $targetFqdn $ttl $bam + } + break + } + default { + "$name.$zoneName" + write-error $err + } + } + } +} + +function Add-BamDnsExternalRecord { + param( + $viewId, + $fqdn, + $prop = '', $bam = $defaultBam ) - $bam.addAliasRecord($view.id, $fqdn, $targetFqdn, $ttl, "") + $bam.addExternalHostRecord($viewId, $fqdn, $prop) +} + +function Add-BamDnsSrvRecord { + param( + $viewId, + $name, + $zoneName, + $priority, + $port, + $weight, + $linkedRecordName, + $ttl, + $prop = "parentZoneName=$zoneName", + $bam = $defaultBam + ) + try { + $bam.addSrvRecord($viewId, "$name.$zoneName", $priority, $port, $weight, $linkedRecordName, $ttl, $prop) + } catch { + $err = $_.exception.message + switch -regex ($err) { + "Duplicate of another item" { + $zone = $bam.getZonesByHint($viewid, 0, 1, "hint=$zonename") + $record = $bam.getEntityByName($zone.id,$name,'SRVRecord') + $record.id + break + } + default { + "$name.$zoneName" + write-error $err + } + } + } +} + +function Add-BamDnsGenericRecord { + param( + $viewId, + $name, + $zoneName, + [ValidateSet( + "A", "A6", "AAAA", "AFSDB", "APL", "CAA", "CERT", "DHCID", "DNAME", + "DNSKEY", "DS", "ISDN", "KEY", "KX", "LOC", "MB", "MG", "MINFO", "MR", + "NS", "NSAP", "PX", "RP", "RT", "SINK", "SSHFP", "TLSA", "WKS", "X25" + )] + $type, + $rdata, + $ttl, + $prop = "parentZoneName=$zoneName", + $bam = $defaultBam + ) + try { + $bam.addGenericRecord($viewId, "$name.$zoneName", $type, $rdata, $ttl, $prop) + } catch { + $err = $_.exception.message + switch -regex ($err) { + "Duplicate of another item" { + $zone = $bam.getZonesByHint($viewid, 0, 1, "hint=$zonename") + $record = $bam.getEntityByName($zone.id,$name,'GenericRecord') + $record.id + break + } + default { + "$name.$zoneName" + write-error $err + } + } + } +} + +Function Get-BamIPv4Block { + param( + [parameter( + mandatory = $true, + ValueFromPipelineByPropertyName, + helpmessage = 'Container ID or container object' + )] + $id, + [switch] + $recurse = $false, + $bam = $defaultBam + ) + begin {} + process { + $subBlock = $bam.getEntities($id, 'IP4Block', 0, [int32]::MaxValue) + $subBlock + if ($recurse) { $subBlock | Get-BamIPv4Block -recurse -bam $bam } + } + end {} +} + +Function Get-BamIPv4Network { + param( + [parameter( + mandatory = $true, + ValueFromPipelineByPropertyName, + helpmessage = 'Container ID or container object' + )] + $id, + $bam = $defaultBam + ) + begin {} + process { + try { + $bam.getEntities($id, 'IP4Network', 0, [int32]::MaxValue) + } catch { + write-verbose "Failed to get network in block $id" + } + } + end {} }