Solved

Powershell script strange behavior

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

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Suggested Solutions

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

820 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