0

I am getting very frustrated trying to write something out. Apparently my Write-Host or if I use Write-Output right after my Invoke-RestMethod is not working.

I am having no problems writing out the 'YYYYYYYYYYYYYYY' just before my rest call.

But the 'XXXXXXXXXXXXXXXXXXXXXXX' are skipped for some reason. If I debug I $messagesCount gets set with the amount of messages from, but seems like my function returns true regardless of the $messagesCount.

function IsQueueEmpty {
  param (
      [string]$url,
      [string]$user,
      [string]$pd
  )
  # Try to get queue details from RabbitMQ API
  try {
      Write-Host "Queue has YYYYYYYYYYYYYYYYYYYYYY messages remaining."
      # Make the API call to get queue information
      $response = Invoke-RestMethod -Uri $url -Credential (New-Object PSCredential ($user, (ConvertTo-SecureString $pd -AsPlainText -Force)))
      
      # Get the number of messages in the queue
      $messagesCount = $response.messages

      # Write out the remaining number of messages in the queue
      Write-Host "Queue has XXXXXXXXXXXXXXXXXXXXXXXX messages remaining." # NOT WORKING

      # Return true if the queue is empty, false otherwise
      return ($messagesCount -eq 0)  # Returns true if queue is empty
  } catch {
      Write-Output "An error occurred: $_"
      return $false
  }
}

This is my function, where I am trying to write out the count of messages from my rest call. Debugging return the correct amount of messages (26), but I cannot write it out and my return ($messagesCount -eq 0) returns true all the time.

function IsQueueEmpty {
  param (
      [string]$url,
      [string]$user,
      [string]$pd
  )
  # Try to get queue details from RabbitMQ API
  try {
      # Make the API call to get queue information
      $response = Invoke-RestMethod -Uri $url -Credential (New-Object PSCredential ($user, (ConvertTo-SecureString $pd -AsPlainText -Force)))

      # Get the number of messages in the queue
      $messagesCount = $response.messages

      # Write out the remaining number of messages in the queue
      Write-Host "Queue has $messagesCount messages remaining."

      # Return true if the queue is empty, false otherwise
      return ($messagesCount -eq 0)  # Returns true if queue is empty
  } catch {
      Write-Output "An error occurred: $_"
      return $false
  }
}

If I comment out the Write-Host and I use return ($response.messages -eq 0), then my function works fine.

Can someone explain why I am having problems within my PowerShell function?

I additionally tried casting to int:

$messagesCount = [int]$response.messages
Write-Host "Queue has $messagesCount messages remaining."

but it did not help either

1
  • Return doesn't do what you think it does. It's a common point of confusion. All output is automatically "returned". Commented Nov 9, 2024 at 15:32

1 Answer 1

0

Apparently it was my $pd parameter, that was a problem. After changing it to $pwd, everything worked.

I don't understand why my try catch did not capture anything going wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.