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