3

I am new to powershell scripting. I am supposed to check to see if the first parameter passed is a string and second parameter is an int.

function positions { 
    param (
    [string] $inputstring,
    [int] $num )
     } 

$inputstring=read-host "Enter your name" 
if ( !$inputstring -eq "" ) {
    Write-Output "The first recieved parameter is: " $inputstring "and its type is a string"}
else { Write-Output "The first received parameter is:" $inputstring "and its type is not a string" }

$num=read-Host "Enter a number"
if ( $num -eq int ) {
    Write-Output "This second parameter is" $num "and its type is a integer"}
else { Write-Output "This second parameter is" $num "and its type is not a integer"}

I believe the if statement for the string is wrong because it give me right input only if i negate it with '!'

Also, for the int, if statement it is not reading 'int' after -eq.

I am extremely new to this so need help.

2 Answers 2

4

Firstly, when you take input from screen using read-host the input will be read and stored as a string, irrespective of what you enter. You can confirm this by running the following command and entering a number:

($checkInt = read-host).GetType().Name

This will output string, no matter what you enter. The equivalent would be to define the input variable like this:

$checkInt = "10"

$StringVariable = "This is a string"
$IntVariable = 10
$StringIntVariable = "10"

## Print out variable types
"String variable is type " + $StringVariable.GetType().Name
"Int variable is type " + $IntVariable.GetType().Name
"StringInt variable is type " + $StringIntVariable.GetType().Name

Which again, if you check the type of that variable will return string.

What you need to do is cast to an int and check if the value is null or check if the value is numeric:

## Checking if user input is alphanumeric
$stringIntVariable = Read-Host "Enter a number"
if (($stringIntVariable -as [int]) -ne $null) {
    "StringIntVariable is numeric"
}
else {
    "StringIntVariable is not numeric"
} 

With regards to your code, the below will work how you want it to:

$inputstring = read-host "Enter your name" 

if (($inputstring -as [int]) -eq $null) { ## Check if not castable to int
    Write-Output "The first recieved parameter is: " $inputstring "and its type is a string"
}
else { 
    Write-Output "The first received parameter is:" $inputstring "and its type is not a string" 
}

$num=read-Host "Enter a number"

## Checking if user input is numeric
if (($num -as [int]) -ne $null) {
    Write-Output "This second parameter is" $num "and its type is a integer"
}
else {
    Write-Output "This second parameter is" $num "and its type is not a integer"
}

As @TheMadTechnician pointed out using ($num -as [int]) -ne $null is more forgiving than using a regex match.

Sign up to request clarification or add additional context in comments.

4 Comments

You could also cast the second item as an int, and see if it is null. if (($num -as [int]) -ne $null){ 'Second param is an int' }else{ 'Second param is not an int' }. This is a little more forgiving, as it ignores some whitespace, but still only returns $true if PowerShell can interpret the input as an integer.
Have edited the answer. For some reason, using that regex returned true if the first characters before the first space were numeric. Your cast method did not.
Thank you everyone. I understand the part of Read-host always taking in the input as a string. I ran the code and when i enter a integer when read-host asks for a name, it says this is a string, rather than it going to this is not a string. Is this also because it will read input as a string. maybe i will need an else if statement as well
It becomes a bit more difficult with checking for the string as whatever you enter will be a string. I guess you could change the if statement to be if (($inputstring -as [int]) -eq $null) { which is just checking that the input is not castable to type int (See edit)
1

You can use GetType().

When checking for type equality put square brackets around the type ([string]):

if ( $inputstring.GetType() -eq [string] ) 
{
    # $inputstring is type String
}
else 
{
   ...
}

The value coming from Read-Host will always be a string, so one option to verify $num is an int is to cast it to an int. However, this will throw an exception if the input is not actually an integer, so instead of if else you could use try catch block:

$num = read-Host "Enter a number"
try {
    # cast to int
    $num = [int]$num
    # $num is type int (Int32)
}
catch
{
    # $num is not an int
}

6 Comments

wouldn't $InputString -is [string] & $Num -is [int] make more sense?
It would still return false anyway, he is getting the number input from user so it will return a string. he needs to check if the input is alphanumeric
@Lee_Dailey -is doesn't work on the version of powershell I'm using. @Owain yup, didn't think about that! It'll be a string so would need to cast to int first, I guess
If you use that logic your If ( $num.GetType() -eq [int]) test fails too.
@OwainEsau - arg! i missed that entirely ... thank you for the heads-up! [grin]
|

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.