Solved

Powershell script strange behavior

Posted on 2014-03-31
3
480 Views
Last Modified: 2014-04-01
I'm experiencing strange behavior with a simple powershell script.

$number = read-host "enter 50"
$guess = read-host "enter 100"

if ($guess -lt $number){
write-host "$guess is less than $number"}
else{
write-host "$guess is greater than $number"}

This script should go to the else statement when entering in 50 and 100 as directed but it doesn't and I can't figure out why. Any input would be much appreciated.

On a side note. After running the script I can in type in powershell
$guess -lt $number and get false which is as expected.
0
Comment
Question by:lvpropc
3 Comments
 
LVL 14

Accepted Solution

by:
Justin Yeung earned 500 total points
ID: 39967658
because that is considered as [string]
if you place

[int]$number = read-host "enter 50"
[int]$guess = read-host "enter 100"

if ($guess -lt $number){
write-host "$guess is less than $number"}
else{
write-host "$guess is greater than $number"}
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39967979
"On a side note. After running the script I can in type in powershell
$guess -lt $number and get false which is as expected."


That's not true, as the result is still $true when using the read-host results. However, if you assign numeric literals 50 and 100, the condition is $false now.
0
 

Author Comment

by:lvpropc
ID: 39969152
You're right Qlemo in my powershell session I had previously assigned the INT's to the variables in order to get the false result. After restarting my session it displayed the incorrect result. The irony is the book I'm reading on powershell has this script in it as a working model. Someone needs to fire the author and editor.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
A brief introduction to what I consider to be the best editor for PowerShell.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now