1
[int]$name = Read-Host Enter the KB number
If ($name -is [int]) { 
    wusa /uninstall /kb:$name
    write-Host "This will uninstall windows update KB$name"
} else {
    write-Host "Enter only the number"
}

Here in this PowerShell scripts, whenever a characters is typed is returns an error instead of message "Enter only the number".

PS C:\Users\User\Desktop> .\Test.ps1
45454
Enter the KB number: asfs
Cannot convert value "asfs" to type "System.Int32". Error: "Input string was not in a correct format."
At C:\Users\User\Desktop\Test.ps1:5 char:1
+ [int]$name = Read-Host Enter the KB number
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

This will uninstall windows update KB0

5
  • Your issue is that in your definition of the variable, you are already defining it as an integer and if you enter a string, there will be a conversion error. Eitehr silence the error by defining $erroractionpreference = "silentlycontinue" before definition, or define the variable as an int later. Commented Jun 6, 2020 at 17:47
  • To test this, run the definition command ([int]$name = Read-Host Enter the KB number) in powershell console and enter a string. The same error will occur Commented Jun 6, 2020 at 17:49
  • if any answer helps you? If so, you should accept the answer so that the question doesn't keep popping up forever, looking for an answer. Alternatively, you can provide and accept your own answer. Commented Jun 6, 2020 at 18:42
  • @NekoMusume Thanks for your response. The error message returns the value "0" which is integer. So If we use the syntax $erroractionpreference = "silentlycontinue" before the variable, the code doesnt go to the else statement. It returns with a message stating that "This will uninstall KB0", kindly help. Commented Jun 7, 2020 at 10:42
  • @SreeRam On my second answer, I don't see what could cause that since it checks if $name is [int], however, on my first answer if you silently continue, the $? may not work as intended but both should work. Commented Jun 7, 2020 at 15:14

5 Answers 5

2

I don't know if there is any command present to test if the value is integer or not. But you can use regular expression to check specific type of data. Here I used regex to match only integer type value. You can use the below code

$i = 0
do {
    If ( $i -ne '0' ) { Write-Host 'Enter only Number' } 
$i = 1
$name = Read-Host 'Enter the KB Number'
} until ( $name -match '\b\d+\b')
wusa /uninstall /kb:$name
Sign up to request clarification or add additional context in comments.

1 Comment

The test you can do is demonstrated in the question at the second row.
1

The error you are facing is cause by this:

[int]$name = Read-Host Enter the KB number
^^^^^

By defining the int variable type, any non-int input will cause an error. For example:

PS C:\Users\Neko> [int]$test = read-host
ABC
Cannot convert value "ABC" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:1
+ [int]$test = read-host
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : MetadataError: (:) [], ArgumentTransformationMetadataException
    + FullyQualifiedErrorId : RuntimeException

Powershell tries to convert the string input to the type System.Int32 but since it is not possible as the input is a string, this error is caused which also causes the variable to not be defined whatsoever. If you are going to define it as a int, do it after the variable was initially defined like so:

:loop while ($name = read-host Enter the KB number)
    {
    $name -as [int] | findstr $name
    if ($?)
        {[int]$name = $name; break loop}
    else
        {Write-Output "Invalid Input"}
    } 
wusa /uninstall /kb:$name
write-Host "This will uninstall windows update KB$name"
}

or alternatively you can do:

:loop while ($name = read-host Enter the KB number){
    $ErrorActionPreference = 'SilentlyContinue'
    [int]$name = $name
    if (!($name -is [int]))
    {echo "Invalid input"} 
    else 
    {break loop}
    }
wusa /uninstall /kb:$name
write-Host "This will uninstall windows update KB$name"

Comments

0

It's because you declared $name as a variable that can only accept numerical values. Try this code:

$name = Read-Host Enter the KB number
[int]$number = $null
if ([int32]::TryParse($name, [ref]$number)) # test if is possible to cast and put parsed value in reference variable
{
    wusa /uninstall /kb:$name
    Write-Host "This will uninstall windows update KB$name"
}
else
{
    Write-Host "Enter only the number"
}

I choosed this way to convert the input value to the numerical value accoding to this answer.

Comments

0

Using -as works as a test. You don't have to convert it.

$name = Read-Host Enter the KB number
If ($name -as [int]) { 
    wusa /uninstall /kb:$name
    write-Host "This will uninstall windows update KB$name"
} else {
    write-Host "Enter only the number"
}


Enter the KB number: a
Enter only the number


Enter the KB number: 1
This will uninstall windows update KB1

Comments

-1

Just use a try-catch instead not to have the script end :)

$Name = Read-Host Enter the KB number
try {
  $Name = [int]$Name
  write-Host "This will uninstall windows update KB$Name"
  wusa.exe /uninstall /kb:$Name
}
catch {
  Write-Error $Error[0]
}


    

Edit: Changed my answer to use try - catch instead...

2 Comments

This doesn't work.
Fixed the answer.

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.