$bluecatUri='http://bluecat.kimconnect.com/Services/API'
$bluecatUsername='svc-bluecat-api'
$bluecatPassword='PASSWORD'
$configId=17
$ipv4Address='10.10.162.54'
$marker='toBeDeleted-'

function confirmation($content,$testValue="I confirm",$maxAttempts=3){
  $confirmed=$false;
  $attempts=0;        
  $content|write-host
  write-host "Please 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{
          cls;
          $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 loginBluecat{
  param(
    [Parameter(Mandatory=$true)]$uri,
    [Parameter(Mandatory=$true)]$username,
    [Parameter(Mandatory=$true)]$password
  )
  $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 removeIpv4Assignment{
  param(
    [Parameter(Mandatory=$true)]$proxy,
    [Parameter(Mandatory=$true)]$configId,
    [Parameter(Mandatory=$true)]$ipV4Address,
    [string]$marker='toBeDeleted-'
  )
  
  $erroractionpreference='stop'
  try{    
    $record=$proxy.getIP4Address($configId,$ipV4Address)
    if($record.id -eq 0){
      write-host "IP Address $ipv4Address does not exist in config ID $configId"
      return -1
    }else{
      $markedRecord=$marker+$record.name
      $record.Name=$markedRecord
      $proxy.update($record)
      $property=$proxy.searchByObjectTypes($markedRecord, "IP4Address", 0, 1)
      $confirmed=confirmation "Delete this record:`r`n$(($property|out-string).trim())"
      if($confirmed){
        $proxy.delete($property.id)
        return 0
      }else{
        write-host "User cancelled operation. IP Address $ipv4Address NOT removed."
        return 1
      }      
    }
  }catch{
    write-warning $_
    return 1
  }  
}

$bluecatProxy=loginBluecat -Uri $bluecatUri -Username $bluecatUsername -Password $bluecatPassword
removeIpv4Assignment -proxy $bluecatProxy -configId $configId -ipv4Address $ipV4Address