Solved

Powershell script strange behavior

Posted on 2014-03-31
3
494 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

691 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