hey guys, struggling to understand why this is happening.
I’ve written a script to go out to our Azure ServiceBus, check the message count in the message queues, then call up a function to scale the instances if that count is greater or less than specified thresholds. However, I’ve noticed that the first set of If/Elseif/Else works fine, however, the second set does not and it appears to be due to the comparison statements. Here’s more script:
When I run this right now, Client comes back with 0 messages and write-host tells me “in”. That’s good since the IN trigger is 25.
However, when it gets to server, the count is currently 541 which is less than or equal to the IN trigger, however, that comparison comes back false, moves on to the out trigger and comes back true, thus write-host returns “out”.
My understanding is that $messagecount -ge $servertriggerout should return a boolean value of $true if $messagecount (541) is greater than or equal to $servertriggerout (2000) and $false if not.
So what am I missing here?
Also, if you know of a better way to format the message count value so that its just the numerical value without any headers or spaces please let me know.
Cause I don’t know any better obviously ;-).
I’ve only been in powershell for about a year so by all means if you see something not quite right let me know.
Yeah, I’d drop the quotes. When you ask PowerShell to compare strings, in terms of less or greater, it thinks about them in terms of their lengths. It isn’t going to implicitly make those into numeric values. But if you define them as numbers you should get the results you’re expecting.
Also, if you know of a better way to format the message count value so that its just the numerical value without any headers or spaces please let me know.
Write-Host isn’t how you want to create output in PowerShell. It kills puppies. You probably want to construct an object and output that to the pipeline. For example:
That will let PowerShell engage its formatting system to display the output on-screen. I’ve a lovely couple of books about all this if you’re interested ;).
That makes more sense. So I’ve removed the double quotes from the trigger variables and added [int] before $messagecount so I wouldn’t have to rearrange things and their now returning the expected results.
oh also, the script won’t actually use write-host, i’m just using it to verify that the if/else statements are working properly. I’ve commented out the function calls so that they won’t trigger while i’m testing. a $true will call a function to either spin up a new instance or remove one using a function that i did not include here.
Write-Verbose would be a suitable replacement for the way you’re using Write-Host.
Output from a PowerShell script/function/whatever should be objects. Objects are basically a data structure, where you create properties to contain bits of information. PowerShell’s formatting commands - like Format-Table, ConvertTo-HTML, etc - all understand objects. So if you get your output into that kind of data structure, it becomes a lot easier to consume that output in various ways, format it in various ways, etc. As opposed to just dumping out numbers or strings. This is, in fact, the entire reason PowerShell exists (object-based output) and is what MS has patents on. It’s what makes PowerShell not Bash.