Sree Ram
Sree Ram

Reputation: 61

Powershell command to check if the data type of variable is integer, if it has characters it should respond with a message "Enter Only number"

[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

Upvotes: 1

Views: 2875

Answers (5)

js2010
js2010

Reputation: 27443

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

Upvotes: 0

Dennis
Dennis

Reputation: 1782

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...

Upvotes: -1

novice programmer
novice programmer

Reputation: 103

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

Upvotes: 2

Nico Nekoru
Nico Nekoru

Reputation: 3112

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"

Upvotes: 1

deralbert
deralbert

Reputation: 1053

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.

Upvotes: 0

Related Questions