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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With