Solved

Powershell script strange behavior

Posted on 2014-03-31
3
475 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

22 Experts available now in Live!

Get 1:1 Help Now