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.