3

I wanted to write a script that prompts the user to enter a time in minutes for to start shutdown timer. I got it to work but had no input validation. This is what I have.

DO{
try{

$numOk= $true
[int] $minutes= Read-Host "Enter the amount in minutes until a shut down (0 to cancel)"
#$minutes= [int]$minutes

}
catch{

#if($minutes -isnot [int]){}
$numOk= $false
Write-Host "Input is not an integer!!!!!"
}

} while ($numOk = $false)


[int] $seconds= $minutes*60

if($seconds -eq 0){
shutdown -a
}
else{
shutdown -s -t $seconds
}

I am getting a very weird value when I type a letter in.

PS C:\Users\USER\Desktop\shut down> .\shutdownTimer.ps1
Enter the amount in minutes until a shut down (0 to cancel): a
Input is not an integer!!!!!
Cannot convert value "555555555555555555555555555555555555555555555555555555555555" to type "System.Int32". Error: "Value was either 
too large or too small for an Int32."
At C:\Users\USER\Desktop\shut down\shutdownTimer.ps1:25 char:1
+ [int] $seconds= $minutes*60
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

shutdown : Unable to abort the system shutdown because no shutdown was in progress.(1116)
At C:\Users\USER\Desktop\shut down\shutdownTimer.ps1:29 char:1
+ shutdown -a
+ ~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Unable to abort...progress.(1116):String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Not quite sure where all those fives come from.

2 Answers 2

9

Your code has an error at while ($numOk = $false). In powershell, = is the assignment operator. The comparison operators are shell-style: -eq, -ne, -gt, -gte, -lt, -lte, -like etc. See here

You may want to use [int]::TryParse to test the input, something like this:

$inputValue = 0
do {
    $inputValid = [int]::TryParse((Read-Host 'gimme a number'), [ref]$inputValue)
    if (-not $inputValid) {
        Write-Host "your input was not an integer..."
    }
} while (-not $inputValid)
Sign up to request clarification or add additional context in comments.

1 Comment

Simply changing that line to -eq allowed for the loop to properly work. If I enter in a letter now, my Write-Host statement shows up and asks for the user to enter input again. Thanks!
2

If you typecast a variable as int, PowerShell automatically checks the input. a cannot be assigned to $minutes as it cannot be converted to int. I'm sure your variable was already assigned in your PowerShell session with a as string. 'a' * '60' is 60 times a which produces your error. Cleanup your variables and better yet rewrite your code. There is no need for Read-Host and all the input validation. PowerShell does all that for you.

param(
    # Enter the amount in minutes until a shut down
    [int]$Minutes = 1
)
$Seconds = $Minutes * 60
Start-Sleep -Seconds $Seconds
Stop-Computer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.