October 7, 2011 at 12:52 pm
Please let me preface this with: I am a powershell rookie... not a developer at all, just learning.
I'm writing some code which goes out to a table and pulls back some valuse, but when I do a test, the reusult is not what I expect...
here's the code:
#go to the database and pull out the value - which I know to be 0
$specialmondays=Invoke-sqlcmd -ServerInstance "MYSERVER" -Database MYDB -Query "Select specialmondays from msdb.dbo.serverdescriptions where servername='$instance' "
#change that one record table result to a string and store it in a variable
$specialmondaysvalue=$specialmondays[0]|out-string
"mondaysvariable is $specialmondaysvalue" #this is here just for debugging and outputs 0 as expected
#test
if ( $specialmondaysvalue -eq 0)
{
"test returned true" #this is here just for debugging
}
else
{"testisfalse"}
my results look like:
mondaysvariable is 0
testisfalse
can someone explain what I'm doing wrong? I've looked up all the conditional and I thought this test should be true...
Also, there must be a better way to work with the result of my query than "$specialmondaysvalue=$specialmondays[0]|out-string"
but anything else I do seems to call it System.Data.Row (or something very close to that)...
Any and all assistance is greatly appreciated.
October 7, 2011 at 3:01 pm
I tried the following and got the correct results. Perhaps others can chime in on why this behaves like it does:
Change:
if ( $specialmondaysvalue -eq 0)
To:
if ( $specialmondaysvalue.Trim() -eq 0)
The whitespace is causing the comparison to not be truly equal. Another route would be to change
$specialmondaysvalue=$specialmondays[0]|out-string
to
$specialmondaysvalue=$specialmondays[0].ToString()
- Jeff
October 7, 2011 at 7:32 pm
Thank you!
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy