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 *