Bluecat.Address.Manager.ps1
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] } $bamObject | select -property * -excludeproperty properties } end {} } function Connect-Bam { param( [Parameter(Mandatory = $true)] [string] $uri, [Parameter(Mandatory = $true)] [pscredential] $credential ) [Net.ServicePointManager]::SecurityProtocol = 'Tls12' $proxy = New-WebServiceProxy -uri $uri $proxy.CookieContainer = New-Object System.Net.CookieContainer $proxy.login($Credential.UserName, ($Credential.GetNetworkCredential()).Password) $Global:defaultBam = $proxy return $proxy } function Get-BamConfiguration { param( $name, $bam = $defaultBam ) $bam.getentitybyname(0,$name,"Configuration") } function Get-BamDnsZone { param( $ConfigurationName, $ZoneName, $bam = $defaultBam ) $cfg = get-BamConfiguration $ConfigurationName $bam $bam.getZonesByHint($cfg.id, 0, 10, "hint=$ZoneName") } function Get-BamDnsView { param( $ConfigurationName, $ViewName, $bam = $defaultBam ) $cfg = get-BamConfiguration $ConfigurationName $bam.getentitybyname($cfg.id,$ViewName,"View") } function Get-BamServer { param( $ConfigurationName, $bam = $defaultBam ) $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( $ZoneId, [ValidateSet( "HostRecord", "AliasRecord", "MXRecord", "TXTRecord", "SRVRecord", "GenericRecord", "HINFORecord", "NAPTRRecord" )] $type, $bam = $defaultBam ) $bam.getEntities($zoneId, $type, 0, [int32]::MaxValue) } function Add-BamDnsHostRecord { param( $viewId, $fqdn, $ip, $ttl, $prop = 'reverseRecord=true', $bam = $defaultBam ) try { $bam.addHostRecord($viewId, $fqdn, $ip, $ttl, $prop) } catch { $err = $_.exception.message switch -regex ($err) { "IP Address doesn't belong to a Network" { Add-BamDnsGenericRecord -viewId $viewId $fqdn "A" $ip $ttl $bam } "Duplicate of another item" { $name = $fqdn.split('.')[0] $zoneName = $fqdn -replace "$name.", '' $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," $bam.update($record) } $record.id } default { $fqdn write-error $err } } } } function Add-BamDnsAliasRecord { param( $viewId, $fqdn, $targetFqdn, $ttl, $prop = '', $bam = $defaultBam ) try { $bam.addAliasRecord($viewId, $fqdn, $targetFqdn, $ttl, $prop) } catch { $err = $_.exception.message switch -regex ($err) { "Object was not found" { $name1 = $fqdn.split('.')[0] $zoneName1 = $fqdn -replace "$name1.", '' $name2 = $targetFqdn.split('.')[0] $zoneName2 = $targetfqdn -replace "$name2.", '' if ($zoneName1 -ne $zoneName2) { Add-BamDnsExternalRecord $viewId $targetFqdn $bam Add-BamDnsAliasRecord $viewId $fqdn $targetFqdn $ttl $bam } } default { $fqdn write-error $err } } } } function Add-BamDnsExternalRecord { param( $viewId, $fqdn, $prop = '', $bam = $defaultBam ) $bam.addExternalHostRecord($viewId, $fqdn, $prop) } function Add-BamDnsSrvRecord { param( $viewId, $fqdn, $priority, $port, $weight, $linkedRecordName, $ttl, $prop = '', $bam = $defaultBam ) $bam.addSrvRecord($viewId, $fqdn, $priority, $port, $weight, $linkedRecordName, $ttl, $prop) } function Add-BamDnsGenericRecord { param( $viewId, $fqdn, [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 = '', $bam = $defaultBam ) try { $bam.addGenericRecord($viewId, $fqdn, $type, $rdata, $ttl, $prop) } catch { $err = $_.exception.message switch -regex ($err) { "Duplicate of another item" { $name = $fqdn.split('.')[0] $zoneName = $fqdn -replace "$name.", '' $zone = $bam.getZonesByHint($viewid, 0, 1, "hint=$zonename") $record = $bam.getEntityByName($zone.id,$name,'GenericRecord') $record.id } default { $fqdn throw $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 {} } |