Posted On September 30, 2021

PowerShell: Remove A-record in Bluecat

kimconnect 0 comments
blog.KimConnect.com >> Codes , Networking >> PowerShell: Remove A-record in Bluecat
$bluecatUri='https://bluecat.kimconnect.com/Services/API'
$bluecatUsername='bluecat-service-api'
$bluecatPassword='SOMECOMPLEXPASSWORD'
$hostRecord='testrecord.kimconnect.com'

function loginBluecat{
    param(
        $username,
        $password,
        $uri
    )
    $proxy = New-WebServiceProxy -Uri "$($uri)?wsdl"
    $proxy.url = $uri
    $cookieContainer = New-Object System.Net.CookieContainer
    $proxy.CookieContainer = $cookieContainer
    $proxy.login($username, $password)
    return $proxy
} 

function confirmation($content,$testValue="I confirm",$maxAttempts=3){
  $confirmed=$false
  $attempts=0
  clear-host  
  write-host $($content|out-string).trim()
  write-host "`r`nPlease review this content for accuracy.`r`n"
  while ($attempts -le $maxAttempts){
      if($attempts++ -ge $maxAttempts){
          write-host "A maximum number of attempts have reached. No confirmations received!`r`n"
          break;
          }
      $userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm. Input CANCEL to skip this item";
      if ($userInput.ToLower() -eq $testValue.ToLower()){
          $confirmed=$true;
          write-host "Confirmed!`r`n";
          break;                
      }elseif($userInput -like 'cancel'){
          write-host 'Cancel command received.'
          $confirmed=$false
          break
      }else{
          clear-host
          $content|write-host
          write-host "Attempt number $attempts of $maxAttempts`: $userInput does not match $testValue. Try again or Input CANCEL to skip this item`r`n"
          }
      }
  return $confirmed
}

function pickItem($list){
  do {
      try {
          $flag = $true
          clear-host
          for($i=0;$i -lt $list.count; $i++){
            write-host "$i`: $(($list[$i]|out-string))"
          }
          [int]$pick=Read-Host -Prompt "`n--------------------------------------------------------`nPlease type the number corresponding to the desired item`n--------------------------------------------------------"
      }catch{
        $flag = $false
      }
    }
  until ($pick -lt $list.count -and $pick -ge 0 -and $flag)
  $pickIndex=$pick
  $pickedItem=$list[$pickIndex]
  clear-host
  write-host "Selected item:`n--------------------------------------------------------`n$($pickedItem|out-string)"
  return $pickedItem
}

function removeAhostRecordInBluecat{
  param(
    $proxy,
    $hostRecord
  )
  $confirmed=$false
  try{
    $property=$proxy.searchByCategory($hostRecord,'RESOURCE_RECORD',0,10)
    $deleteItem=if($property.count -gt 1){
      pickItem $property
    }else{
      $property
    }
    $null=clear-host
    $confirmed=confirmation "Delete this record?`r`n`r`n$(($deleteItem|out-string).trim())"
    if($confirmed){
      $proxy.delete($deleteItem.id)
      return 0
    }else{
      write-host "User cancelled operation. Record '$($deleteItem.name)' NOT removed."
      return 1
    } 
  }catch{
    write-warning $_
    return -1
  }   
}

$proxy=loginBluecat -Uri $bluecatUri -Username $bluecatUsername -Password $bluecatPassword
removeAhostRecordInBluecat $proxy $hostRecord

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: How To Add Additional Sub-Directories to an Existing SMB Share

Business Use-Case: There's an existing logon script or Group Policy that maps users toward a…

Windows 2012: How To Install DotNet Framework 2.0 & 3.0

If there're HTTPS/TLS issues, run this function: https://blog.kimconnect.com/powershell-enable-tls-1-2-on-legacy-servers/ Toggle WSUS registry key to off prior…

How To Format HTML Content For Email

Major email service providers such as Google Gmail and Microsoft Office 365 have been supporting…
Other project: DragonCoin.com