Need assistance with powershell script - powershell

See code below
##If user inputs %100 or a100 or non-numeric value for temp, I need to display "Error: You must enter a numeric Value!" Write-host $("Error: You must enter a numeric Value!")
## then goes back to start
##im guessing if ($value isnot [int]) or something like that for it to work. thank you.
##Error I get without proper coding......What is the temperature in Fahrenheit: a111
##Cannot convert value "a111" to type "System. Single". Error: "Input string was not in a ##correct format."
Write-Host("="*31)
Write-Host $("Fahrenheit to Celsius Converter")
Write-Host $("Script Written By Jesse ")
Write-Host("="*31)
$value = (Read-Host("What is the temperature in Fahrenheit"))
$fahr = (($value -32) /9) *5
Write-Host $("Fahrenheit", $value, "is equal to Celsius:", [Math]::Round($fahr,3))
$input = (Read-Host("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
Write-Host ("="*31)
while ($input)
{
if ($input -eq 1)
{
Write-Host $("Fahrenheit to Celsius Converter")
Write-Host $("Script Written By Jesse Nieto ")
Write-Host ("="*31)
$value = (Read-Host("What is the temperature in Fahrenheit"))
$fahr = (($value -32) /9) *5
Write-Host $("Fahrenheit", $value ,"is equal to Celsius:" ,[Math]::Round($fahr,3))
$input = (Read-Host("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
Write-Host("="*31)
}
elseif ($input -eq 0)
{
Write-Host $("Thank You. Bye! ")
break
}
else
{
Write-Host $("Please enter a valid option! ")
$input = (Read-Host ("Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"))
Write-Host ("="*31)
}
}

There is much redundant code on your script which could be simplified, the biggest issue is the use of $input which is an automatic variable and should not be assigned manually. As for the formula to convert from Fahrenheit to Celsius, you could use a function for that, as aside, according to Google, the formula should be (X − 32) × 5 / 9 though I'm not an expert on this, feel free to change for whatever you like.
function ConvertTo-Celsius {
param([int]$Value)
($value - 32) * 5 / 9
}
:outer while($true) {
$value = Read-Host "What is the temperature in Fahrenheit"
try {
$celsius = ConvertTo-Celsius $value
}
catch {
# If input was invalid restart the loop
Write-Host "Invalid input, only integers allowed!"
continue
}
Write-Host "Fahrenheit $value°F is equal to $([math]::Round($celsius, 3))°C"
do {
$shouldExit = Read-Host "Do you want to convert another Fahrenheit value? (1 = yes, 0 = no)?"
switch($shouldExit) {
0 {
Write-Host "Thank You. Bye!"
break outer
}
1 { continue }
Default { Write-Host "Please enter a valid option!" }
}
} until($shouldExit -eq 1)
}

Related

Color Guessing Game in Powershell [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm working on writing a script in PowerShell for a Color guessing game. The computer randomly picks a color then the player tries to guess the color. I had it working up until I switched some lines of code in the script in an attempt to get these two variables to display correctly. Now, I can't get the code to run past the point where a player declares if they want to play the game or not. My current error is with a do loop, where the console doesn't see that I have a while loop, and so throws an error and won't run the rest of the code.
I managed to get the formatting fixed with Visual Studio Code's format document feature, but I still can't get this one while loop problem figured out.
Write-Host ''; 'Hello again my friend!'; ''
$name = Read-Host "What is your name?"
Write-Host ''; "It's good to see you again, $name! Would you like to guess my favorite color?"; ''
$command = Read-Host #'
"How do you answer? (Yes or No?)"
1. Yes (y)
2. No (n)
3. Quit (q)
Enter Choice
'#
switch -Wildcard ($command) {
'Y' { 'Yes!'; '' }
{ $_ -eq 'y' -or $_ -like 'Ye*' } {
Write-Host "This will be fun! Let us begin!"
Break
}
'N' { 'No!'; '' }
{ $_ -eq 'n' -or $_ -like 'No*' } {
Write-Host "That's too bad, perhaps another day!"
Exit
}
'Q' { 'Quit'; '' }
{ $_ -eq 'q' -or $_ -like 'qu*' } {
Write-Host 'So long!'
Exit
}
default {
'Invalid Command, Please start over.'
Exit
}
}
[string]$playagain = 'y'
[int]$playerwins = 0
[int]$compwins = 0
[int]$totalguesses = 0
[int]$playergames = 0
[int]$compgames = 0
[int]$round = 1
[int]$game = 1
$cpuchoice = $color
while ($playagain -eq 'y') {
Write-Host ''; "Game $game!"; ''
$cpuchoice = #([System.Enum]::GetValues([System.ConsoleColor])) | Get-Random -Count 1
do {
Write-Host "Round $round! What is my favorite color?"; ''
$listcolor = Read-Host "Would you like to see a list of available colors? Choose 'y' for yes, and 'n' for no."
if ($listcolor -eq 'y') {
[System.Enum]::GetValues([System.ConsoleColor])
}
elseif ($listcolor -eq 'n') {
Write-Host "Suit yourself, let's start."
}
else {
Write-Host "Your choice was invalid. Please choose 'y' for yes, or 'n' for no."
}
do {
$playerchoice = Read-host "Enter your guess"
} while (([System.Enum]::GetValues([System.ConsoleColor])) -notcontains $playerchoice) {
if ($playerchoice -eq $cpuchoice ) {
Write-Host "You win, my favorite color is $cpuchoice." -ForegroundColor $cpuchoice; ''
$playerwins = $playerwins + 1
$totalguesses = $totalguesses + 1
}
elseif ($playerchoice -ne $cpuchoice ) {
Write-Host "You lose, try again."; ''
$playerguesses += $playerchoice
$playerguesses = $playerguesses.Split(',')
$totalguesses = $totalguesses + 1
Write-Host "Here are your guesses so far: "
$playerguesses
''
}
$round = $round + 1
}
until($playerwins -eq 1) {
$playergames = $playergames + 1
Write-Host "You've won this round and have won $playergames games." -ForegroundColor Green
Write-Host "Your total guesses: $totalguesses."
Write-Host "Your wins - $playergames" -ForegroundColor Yellow
Write-Host "Computer wins - $compgames" -ForegroundColor Yellow
''
}
$playagain = Read-Host "I enjoyed this game. Would you like to challenge again, $name? Y or N"
while (("y", "n") -notcontains $playagain) {
if ($playagain -eq "y") {
Write-Host "I look forward to our next battle!"; ''
$playerwins = 0
$compwins = 0
$game = $game + 1
}
elseif ($playagain -eq "n") {
Write-Host "Thank you for playing!"
exit
}
}
}
}
The do loop that causes the error is the one that starts with "Write-Host "Round $round!" after the first while statement.
Any help is appreciated! Thanks!
Your program is being parsed like this:
while ($playagain -eq 'y')
{
# do loop #1
do
{
# do loop #2
do {
}
while (([System.Enum]::GetValues([System.ConsoleColor])) -notcontains $playerchoice)
# floating script block #1
# (doesn't execute the scriptblock, but it gets sent to the output stream instead)
{
if ($playerchoice -eq $cpuchoice ) {
... etc ...
}
# try to invoke a cmdlet "until" with 2 parameters
# i.e. ($playerwins -eq 1) and { ... }
until ($playerwins -eq 1) {
$playergames = $playergames + 1
... etc ...
}
# while loop #1
while (("y", "n") -notcontains $playagain) {
...
}
}
}
The error is telling you the first do (do loop #1) doesn't have a trailing while or until.
There's no clear and simple fix I can offer to make your code run short of a significant rework because there's a number of issues (e.g. the floating script block #1, the dangling until and the while \ until-less do), but that's what the current error is saying anyway...

User input validation in PowerShell

I have this piece of code in PowerShell. I need user input validation to integer with writing output saying what is wrong with the input and expecting correct input.
$FromObj = "Please input object number"
$FromInput = Read-Host $FromObj
while(($FromInput -isnot [int])) {
#I also want to put in the while that the input has to be an integer, -gt 0, and -lt 800
Write-Output "Your input has to be a number."
$FromInput = Read-Host $FromObj
}
if ($FromInput -le 0) {
Write-Output "Your input has to be a number greater than 0!"
$FromInput = Read-Host $FromObj
}
elseif ($FromInput -ge 800) {
Write-Output "Your input has to be a number less than 800!"
$FromInput = Read-Host $FromObj
}
else {
$FromInput = $FromInput -as [int]
}
However, this is not working for me. Could you help me with validating it with writing outputs as the ones displayed above?
I think Nicks's link in comment already provides different right answers to your question i.e.: using regex to match \d+ (digits) or using [int]::TryParse(...) but to explain why your code is not working:
Read-Host will store user input as string unless you manipulate it which is what you were already doing on your last condition (the else {...} statement).
This is why you never get past the $FromInput -isnot [int] condition.
To fix the code you could simply attempt to store user input -as [int] since the beginning and since you have 3 Read-Host on your code you could save the user input attempt -as [int] in a ScriptBlock which then can be easily executed as many times as needed:
$FromObj = "Please input object number"
$giveMeNumber = { (Read-Host $FromObj) -as [int] }
$FromInput = & $giveMeNumber
while($FromInput -isnot [int]) {
Write-Output "Your input has to be a number."
$FromInput = & $giveMeNumber
}
if ($FromInput -le 0) {
Write-Output "Your input has to be a number greater than 0!"
$FromInput = & $giveMeNumber
}
elseif ($FromInput -ge 800) {
Write-Output "Your input has to be a number less than 800!"
$FromInput = & $giveMeNumber
}
Note, even though this works, the statement is not entirely correct since you're breaking out of the while loop after the input is [int] the user could force an incorrect input.
Try this instead which will execute indefinitely until right input:
Clear-Host
$ErrorActionPreference = 'Stop'
$FromObj = "Please input object number"
$scriptBlock = {
try
{
$FromInput = [int](Read-Host $FromObj)
# Note I'm using Write-Host instead of Write-Ouput, this is because
# we don't want to store the invalid inputs messages in the
# $userInput variable.
if ($FromInput -le 0) {
Write-Host "Your input has to be a number greater than 0!"
& $scriptBlock
}
elseif ($FromInput -ge 800) {
Write-Host "Your input has to be a number less than 800!"
& $scriptBlock
}
else {
$FromInput
}
}
catch
{
Write-Host "Your input has to be a number."
& $scriptBlock
}
}
$userInput = & $scriptBlock

How can I increase the maximum number of characters read by Read-Host?

I need to get a very long string input (around 9,000 characters), but Read-Host will truncate after around 8,000 characters. How can I extend this limit?
The following are possible workarounds.
Workaround 1 has the advantage that it will work with PowerShell background jobs that require keyboard input. Note that if you are trying to paste clipboard content containing new lines, Read-HostLine will only read the first line, but Read-Host has this same behavior.
Workaround 1:
<#
.SYNOPSIS
Read a line of input from the host.
.DESCRIPTION
Read a line of input from the host.
.EXAMPLE
$s = Read-HostLine -prompt "Enter something"
.NOTES
Read-Host has a limitation of 1022 characters.
This approach is safe to use with background jobs that require input.
If pasting content with embedded newlines, only the first line will be read.
A downside to the ReadKey approach is that it is not possible to easily edit the input string before pressing Enter as with Read-Host.
#>
function Read-HostLine ($prompt = $null) {
if ($prompt) {
"${prompt}: " | Write-Host
}
$str = ""
while ($true) {
$key = $host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown");
# Paste the clipboard on CTRL-V
if (($key.VirtualKeyCode -eq 0x56) -and # 0x56 is V
(([int]$key.ControlKeyState -band [System.Management.Automation.Host.ControlKeyStates]::LeftCtrlPressed) -or
([int]$key.ControlKeyState -band [System.Management.Automation.Host.ControlKeyStates]::RightCtrlPressed))) {
$clipboard = Get-Clipboard
$str += $clipboard
Write-Host $clipboard -NoNewline
continue
}
elseif ($key.VirtualKeyCode -eq 0x08) { # 0x08 is Backspace
if ($str.Length -gt 0) {
$str = $str.Substring(0, $str.Length - 1)
Write-Host "`b `b" -NoNewline
}
}
elseif ($key.VirtualKeyCode -eq 13) { # 13 is Enter
Write-Host
break
}
elseif ($key.Character -ne 0) {
$str += $key.Character
Write-Host $key.Character -NoNewline
}
}
return $str
}
Workaround 2:
$maxLength = 65536
[System.Console]::SetIn([System.IO.StreamReader]::new([System.Console]::OpenStandardInput($maxLength), [System.Console]::InputEncoding, $false, $maxLength))
$s = [System.Console]::ReadLine()
Workaround 3:
function Read-Line($maxLength = 65536) {
$str = ""
$inputStream = [System.Console]::OpenStandardInput($maxLength);
$bytes = [byte[]]::new($maxLength);
while ($true) {
$len = $inputStream.Read($bytes, 0, $maxLength);
$str += [string]::new($bytes, 0, $len)
if ($str.EndsWith("`r`n")) {
$str = $str.Substring(0, $str.Length - 2)
return $str
}
}
}
$s = Read-Line
More discussion here:
Console.ReadLine() max length?
Why does Console.Readline() have a limit on the length of text it allows?
https://github.com/PowerShell/PowerShell/issues/16555

Read-Host in While loop with if statement

This should be really simple, but I cannot make it work. I'm new to the PowerShell so my mistake is probably obvious. I'm trying to ask a user for an input using a while loop, and prompting the user to try again if what was entered was not numeric. In bash it would look like this:
while read -p "What is the first number? " first; do
if [[ $first = *[[:digit:]]* ]]; then
break # if $first is numeric, break loop
else
echo "$first is not numeric. Enter a numeric value. "
fi
done
Here's what I have for the PS:
While ($first = (Read-Host "What is the first number?") -as [double]) {
if ($first -eq $null) { Write-Host "Please enter a numeric value." }
else { break }
}
In the example above, I can break the loop fine when a number is entered, but I can't see the Write-Host statement if I type a string.
Looking at the While line:
While ($first = (Read-Host "What is the first number?") -as [double])
This will only enter/continue the loop body when the input is already a double, because it rolls up the cast to double as part of the loop condition. You want to enter the loop body on any input, and only check if it's a double afterwards. At least, that's what the bash code does:
While ($first = (Read-Host "What is the first number?")) {
if ( ($first -as [double]) -eq $null) { Write-Host "Please enter a numeric value." }
else { break }
}
Or you could continue using the cast as part of the condition by negating the whole expression, and thus avoid the need for the awkward break:
While (-not ($first = (Read-Host "What is the first number?") -as [double])) {
Write-Host "Please enter a numeric value."
}
You could use this to keep prompting for a valid number input as you are looking for.
Do {
"Please enter a numeric value." | Out-Host
$first = (Read-Host "What is the first number?") -as [double]
} While($null -eq $first)

Check if user input is a number and then select from switch to print

Code:
[int]$s = Read-Host "Enter number from 1-3"
switch ($s) {
1 { $s = 'Apple' }
2 { $s = 'Melon' }
3 { $s = 'Mango' }
}
$s
Output:
Cannot convert value "Apple" to type "System.Int32". Error
input was not in correct format.
So my question is: How do I check if my input is a number and at the same time select from my switch?
The problem with your code is that after you define $s as an integer, you then later try to assign a string value to it.
You could instead do this:
[int]$s = Read-Host "Enter number from 1-3"
$result = switch ($s) {
1 { 'Apple' }
2 { 'Melon' }
3 { 'Mango' }
}
$result
Note that i'm also simplifying your code here by returning the result of the switch to $result instead of assigning it inside each condition.
This works because $result is an undefined variable that becomes a string when you assign one to it.
If you want to validate that the input is an integer, you could also consider doing something like this:
$input = Read-Host "Enter number from 1-3"
if (($input -isnot [int])) { Throw 'You did not provide a number as input' }
$result = switch ($input) {
1 { 'Apple' }
2 { 'Melon' }
3 { 'Mango' }
}
$result
You don't need to use the integer to declare the variable:
$s = Read-Host "Enter number from 1-3"
switch ($s) {
1 { $s = 'Apple' }
2 { $s = 'Melon' }
3 { $s = 'Mango' }
}
$s
And if you check the type of variable:
When you have a predefined set of options like this, consider using a multiple-choice menu, like this:
$title = "Select Fruit"
$prompt = "Which fruit is your favorite?"
$apple = New-Object System.Management.Automation.Host.ChoiceDescription "&Apple","Apple"
$melon= New-Object System.Management.Automation.Host.ChoiceDescription "&Melon","Melon"
$mango= New-Object System.Management.Automation.Host.ChoiceDescription "Man&go", "Mango"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($apple, $melon, $mango)
$selectedFruit = $host.ui.PromptForChoice($title, $prompt, $options, 0)
switch($selectedFruit)
{
0 {Write-Host "You chose Apple"}
1 {Write-Host "You chose Melon"}
2 {Write-Host "You chose Mango"}
}
In the ISE, the user will see a GUI prompt, with buttons to click on, and at the console, a menu with specific allowed letters to select (A, M and G in this case).
This method has the benefit that it looks like a typical prompt from PowerShell and will check and re-prompt if the user enters an invalid value. You can add a 'quit' option, so the user has an easy way to skip all options.
How do I check if my input is a number and at the same time select from my switch?
How do you check if your input is a number? - I think you are already doing that since you are declaring you variable $s as [int] at the very beginning -
[int]$s = Read-Host "Enter number from 1-3"
Selecting from your switch - The error which you are getting is due to the type mismatch of your input to the read-host and the variable $s. $s is clearly an integer where as you are assigning it a string. Hence, the error You have to type-cast that again in order to correct that -
[int]$s = Read-Host "Enter number from 1-3"
switch ($s) {
1 { [string]$s = 'Apple' }
2 { [string]$s = 'Melon' }
3 { [string]$s = 'Mango' }
}
$s
If you enter any number other than 1, 2 or 3, then you $s will store that number in it. For example if you enter 5, $s will store 5 in it since the switch statement hadn't been executed.
function Verify-InputInteger {
Param (
$Question="Saisir un nombre",
$Min=0,
$Max=100
)
try {
[int]$string_Input = Read-Host $Question;
if($string_Input -LT $Min)
{
Throw "ErrorMin"
}
if($string_Input -GT $Max)
{
Throw "ErrorMax"
}
return $string_Input;
}
catch {
if($_.FullyQualifiedErrorId -EQ 'InvalidCastFromStringToInteger')
{
Write-Host 'You did not provide a number as input' -ForegroundColor Red;
Verify-InputInteger -Question $Question -Min $Min -Max $Max;
}
if($_.Exception.Message -EQ 'ErrorMin')
{
Write-Host 'You have entered a number lesser than the minimum limit' -ForegroundColor Red;
Verify-InputInteger -Question $Question -Min $Min -Max $Max;
}
if($_.Exception.Message -EQ 'ErrorMax')
{
Write-Host 'You have entered a number greater than the maximum limit' -ForegroundColor Red;
Verify-InputInteger -Question $Question -Min $Min -Max $Max;
}
}
}